~ubuntu-branches/ubuntu/natty/steam/natty

« back to all changes in this revision

Viewing changes to server/kernel/orb.pike

  • Committer: Bazaar Package Importer
  • Author(s): Alain Schroeder
  • Date: 2005-05-14 16:33:35 UTC
  • Revision ID: james.westby@ubuntu.com-20050514163335-5v7lbxibmlww15dx
Tags: upstream-1.6.3
ImportĀ upstreamĀ versionĀ 1.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
 
2
 *
 
3
 *  This program is free software; you can redistribute it and/or modify
 
4
 *  it under the terms of the GNU General Public License as published by
 
5
 *  the Free Software Foundation; either version 2 of the License, or
 
6
 *  (at your option) any later version.
 
7
 *
 
8
 *  This program is distributed in the hope that it will be useful,
 
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *  GNU General Public License for more details.
 
12
 *
 
13
 *  You should have received a copy of the GNU General Public License
 
14
 *  along with this program; if not, write to the Free Software
 
15
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
16
 * 
 
17
 * $Id: orb.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $
 
18
 */
 
19
 
 
20
constant cvs_version="$Id: orb.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $";
 
21
 
 
22
#include <classes.h>
 
23
#include <database.h>
 
24
#include <attributes.h>
 
25
#include <access.h>
 
26
#include <macros.h>
 
27
 
 
28
object resolve_path(object uid, string path);
 
29
object path_to_object(string path, void|bool reslv);
 
30
string object_to_path(object obj);
 
31
object path_to_environment(string url);
 
32
 
 
33
 
 
34
/**
 
35
 * get detailed information about a file
 
36
 *  
 
37
 * @param url - pathname or object-pointer for an object
 
38
 * @return file information for the object
 
39
 * @author Thomas Bopp (astra@upb.de) 
 
40
 */
 
41
array(mixed) identify_file(string|object url)
 
42
{
 
43
    string path, dtype;
 
44
    int  last_modified;
 
45
    object         obj;
 
46
    array(mixed)   res;
 
47
 
 
48
    if ( !objectp(url) ) {
 
49
        obj = path_to_object(url);
 
50
        if ( !objectp(obj) ) 
 
51
            return 0;
 
52
    }
 
53
    else {
 
54
        LOG("identify_file("+url+")");
 
55
        obj = url;
 
56
        url = object_to_path(obj);
 
57
    }
 
58
    
 
59
    if ( obj->get_object_class() & CLASS_DOCUMENT )
 
60
    {
 
61
        dtype = obj->query_attribute(DOC_MIME_TYPE);
 
62
        LOG("Mime-type is " + dtype);
 
63
        if ( !stringp(dtype) ) {
 
64
            string doctype;
 
65
            
 
66
            doctype = obj->query_attribute(DOC_TYPE);
 
67
            if ( stringp(doctype) )
 
68
                dtype = _TYPES->query_mime_type(doctype);
 
69
            else
 
70
                dtype = "text/html";
 
71
        }
 
72
        last_modified = obj->query_attribute(DOC_LAST_MODIFIED);
 
73
        if ( last_modified == 0 )
 
74
            last_modified = time();
 
75
    }
 
76
    else {
 
77
        dtype = "text/html";
 
78
        last_modified = time();
 
79
    }
 
80
    
 
81
    res = ({ obj,
 
82
             url,
 
83
             dtype, // type of document
 
84
             obj->get_content_size(), // size
 
85
             last_modified, // last modified
 
86
             obj->get_object_class(), // the class type of document 
 
87
             obj->stat(), 
 
88
             obj->query_sanction(_WORLDUSER)
 
89
    });
 
90
    LOG("Identified object=#"+obj->get_object_id()+ " from url="+url + 
 
91
        ",size="+obj->get_content_size()+" array-size="+sizeof(res));
 
92
    LOG("res="+sprintf("%O", res));
 
93
    return res;
 
94
}
 
95
 
 
96
/**
 
97
 * Get stats of some file identified by 'f'
 
98
 *  
 
99
 * @param string f - the filename
 
100
 * @return file_stat information (usually an array like Stdio.File->stat())
 
101
 */
 
102
mixed stat_file(string f, void|object id)
 
103
{
 
104
    object obj = path_to_object(f, true);
 
105
    if ( objectp(obj) )
 
106
        return obj->stat();
 
107
    return 0; // not found
 
108
}
 
109
 
 
110
/**
 
111
 * Get the mimetype for a given url.
 
112
 *  
 
113
 * @param string f - the filename
 
114
 * @return mimetype like text/plain
 
115
 */
 
116
string get_mimetype(string f)
 
117
{
 
118
    object obj = path_to_object(f);
 
119
    if ( objectp(obj) )
 
120
        return obj->stat()[7];
 
121
    return "application/x-unknown";
 
122
}
 
123
 
 
124
/**
 
125
 * create a new directory with name "name" in "path"
 
126
 *  
 
127
 * @param path - the path to create the directory
 
128
 * @param name - the name for the new container
 
129
 * @return the container-object
 
130
 * @author Thomas Bopp (astra@upb.de) 
 
131
 */
 
132
object make_directory(string path, string name)
 
133
{
 
134
    object obj, env, factory;
 
135
 
 
136
    env = path_to_object(path);
 
137
    if ( !objectp(env) )
 
138
        return null;
 
139
    obj = resolve_path(env, name);
 
140
    if ( objectp(obj) )
 
141
        return obj;
 
142
 
 
143
    factory = _Server->get_factory(CLASS_CONTAINER);
 
144
    obj = factory->execute((["name":name,]));
 
145
    if ( !objectp(obj) )
 
146
        return 0;
 
147
    // now move the container in the appropriate place
 
148
    obj->move(env);
 
149
    return obj;
 
150
}
 
151
 
 
152
array(string) get_directory(string dir)
 
153
{
 
154
    array directory = ({ });
 
155
    object cont = path_to_object(dir, true);
 
156
    if ( objectp(cont) ) {
 
157
        array inv = cont->get_inventory();
 
158
        if ( !arrayp(inv) )
 
159
          return directory;
 
160
        
 
161
        foreach(inv, object obj) {
 
162
            if ( objectp(obj) && obj->status() >= 0 && 
 
163
                 !(obj->get_object_class() & CLASS_USER) )
 
164
            {
 
165
                int cl = obj->get_object_class();
 
166
                if ( !stringp(obj->get_identifier()) )
 
167
                  continue;
 
168
                if ( cl & CLASS_EXIT || cl & CLASS_DOCUMENT || 
 
169
                     cl & CLASS_CONTAINER )
 
170
                  directory += ({ obj->get_identifier() });
 
171
            }
 
172
        }
 
173
    }
 
174
    return directory;
 
175
        
 
176
}
 
177
 
 
178
/**
 
179
 * return the result of a directory query as a mapping filename:file_stat
 
180
 *  
 
181
 * @param url - the container to get the directory
 
182
 * @return the directory for the container
 
183
 * @author Thomas Bopp (astra@upb.de) 
 
184
 */
 
185
mapping(string:array(int)) query_directory(string|object url)
 
186
{
 
187
    object                    cont;
 
188
    array(object)              inv;
 
189
    mapping(string:array(int)) res;
 
190
    int                      i, sz;
 
191
 
 
192
    LOG("query_directory...");
 
193
    res = ([ ]);
 
194
    if ( stringp(url) )
 
195
        cont = path_to_object(url, true);
 
196
    else
 
197
        cont = url;
 
198
    inv = cont->get_inventory();
 
199
    for ( i = 0, sz = sizeof(inv); i < sz; i++ ) {
 
200
      int cl = inv[i]->get_object_class();
 
201
        if ( objectp(inv[i]) && 
 
202
             stringp(inv[i]->get_identifier()) && 
 
203
             strlen(inv[i]->get_identifier()) > 0 &&
 
204
             !(cl & CLASS_USER) &&
 
205
             (cl & CLASS_EXIT || cl & CLASS_DOCUMENT || cl & CLASS_CONTAINER ))
 
206
        {
 
207
            res[inv[i]->get_identifier()] =  inv[i]->stat();
 
208
        }
 
209
    }
 
210
    LOG("mapping:size="+sizeof(indices(res)));
 
211
    return res;
 
212
}
 
213
 
 
214
/**
 
215
 * get the filename and path of a given path
 
216
 *  
 
217
 * @param fname - the file name to process
 
218
 * @return path and filename of a file
 
219
 * @author Thomas Bopp (astra@upb.de) 
 
220
 */
 
221
array(string) get_filename(string fname)
 
222
{
 
223
    int                   sz;
 
224
    array(string)     tokens;
 
225
    
 
226
    tokens = fname / "/";
 
227
    if ( !arrayp(tokens) || (sz=sizeof(tokens)) <= 1 ) 
 
228
        return ({ fname, "" });
 
229
    return ({ tokens[sz-1], (tokens[..sz-2] * "/") + "/"});
 
230
}
 
231
 
 
232
/**
 
233
 * Get a path of objects (array) for a given url.
 
234
 *  
 
235
 * @param string url - the given url
 
236
 * @return array of objects
 
237
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
 
238
 */
 
239
array(object) get_path(object obj)
 
240
{
 
241
    array(object) path = ({ });
 
242
    while ( objectp(obj) ) {
 
243
        path = ({ obj }) + path;
 
244
        obj = obj->get_environment();
 
245
    }
 
246
    return path;
 
247
}
 
248