1 /* Copyright (C) 2000-2005 Thomas Bopp, Thorsten Hampel, Ludger Merkens
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.
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.
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
17 * $Id: orb.pike,v 1.3 2010/08/20 20:42:25 astra Exp $
21 #include <attributes.h>
30 object resolve_path(object|string uid, string path);
31 object path_to_object(string path, void|bool reslv);
32 string object_to_path(object obj);
33 object path_to_environment(string url);
35 mapping mVirtualPath = ([ ]);
37 void add_virtual_path(string path, object module)
39 if ( !_Server->is_module(module) )
40 steam_error("Invalid module in filepath: add_virtual_path() !");
41 mVirtualPath[module] = path;
45 * get detailed information about a file
47 * @param url - pathname or object-pointer for an object
48 * @return file information for the object
50 array identify_file(string|object url)
57 if ( !objectp(url) ) {
58 obj = path_to_object(url);
63 LOG("identify_file("+url+")");
65 url = object_to_path(obj);
68 if ( obj->get_object_class() & CLASS_DOCUMENT )
70 dtype = obj->query_attribute(DOC_MIME_TYPE);
71 LOG("Mime-type is " + dtype);
72 if ( !stringp(dtype) ) {
75 doctype = obj->query_attribute(DOC_TYPE);
76 if ( stringp(doctype) )
77 dtype = _TYPES->query_mime_type(doctype);
81 last_modified = obj->query_attribute(DOC_LAST_MODIFIED);
82 if ( last_modified == 0 )
83 last_modified = time();
87 last_modified = time();
92 dtype, // type of document
93 obj->get_content_size(), // size
94 last_modified, // last modified
95 obj->get_object_class(), // the class type of document
97 obj->query_sanction(_WORLDUSER)
99 LOG("Identified object=#"+obj->get_object_id()+ " from url="+url +
100 ",size="+obj->get_content_size()+" array-size="+sizeof(res));
101 LOG("res="+sprintf("%O", res));
106 * Get stats of some file identified by 'f'
108 * @param string f - the filename
109 * @return file_stat information (usually an array like Stdio.File->stat())
111 mixed stat_file(string f, void|object id)
113 object obj = path_to_object(f, true);
116 return 0; // not found
120 * Get the mimetype for a given url.
122 * @param string f - the filename
123 * @return mimetype like text/plain
125 string get_mimetype(string f)
127 object obj = path_to_object(f);
129 return obj->stat()[7];
130 return "application/x-unknown";
134 * create a new directory with name "name" in "path"
136 * @param path - the path to create the directory
137 * @param name - the name for the new container
138 * @return the container-object
140 object make_directory(string path, string name)
142 object obj, env, factory;
143 env = path_to_object(path, true);
146 obj = resolve_path(path, name);
150 if (path == "/home") {
151 steam_error("Cannot create objects in /home!");
153 else if ( _Server->query_config("default_container_type") == "room" )
154 factory = _Server->get_factory(CLASS_ROOM);
156 factory = _Server->get_factory(CLASS_CONTAINER);
157 obj = factory->execute((["name":name,]));
160 // now move the container in the appropriate place
165 array get_directory(string dir)
167 array directory = ({ });
168 object cont = path_to_object(dir, true);
169 if ( objectp(cont) ) {
170 array inv = cont->get_inventory();
174 foreach(inv, object obj) {
175 if ( objectp(obj) && obj->status() >= 0 &&
176 !(obj->get_object_class() & CLASS_USER) )
178 int cl = obj->get_object_class();
179 if ( !stringp(obj->get_identifier()) )
181 if ( cl & CLASS_EXIT || cl & CLASS_DOCUMENT ||
182 cl & CLASS_CONTAINER || cl & CLASS_LINK )
183 directory += ({ obj->get_identifier() });
192 * return the result of a directory query as a mapping filename:file_stat
194 * @param url - the container to get the directory
195 * @return the directory for the container
197 mapping(string:array) query_directory(string|object url)
201 mapping(string:array) res;
204 LOG("query_directory...");
207 cont = path_to_object(url, true);
210 inv = cont->get_inventory();
211 for ( i = 0, sz = sizeof(inv); i < sz; i++ ) {
212 int cl = inv[i]->get_object_class();
213 if ( objectp(inv[i]) &&
214 stringp(inv[i]->get_identifier()) &&
215 strlen(inv[i]->get_identifier()) > 0 &&
216 !(cl & CLASS_USER) &&
217 (cl & CLASS_EXIT || cl & CLASS_DOCUMENT || cl & CLASS_CONTAINER ))
219 res[inv[i]->get_identifier()] = inv[i]->stat();
222 LOG("mapping:size="+sizeof(indices(res)));
227 * get the filename and path of a given path
229 * @param fname - the file name to process
230 * @return path and filename of a file
232 array get_filename(string fname)
237 tokens = fname / "/";
238 if ( !arrayp(tokens) || (sz=sizeof(tokens)) <= 1 )
239 return ({ fname, "" });
240 return ({ tokens[sz-1], (tokens[..sz-2] * "/") + "/"});
244 * Get a path of objects (array) for a given url.
246 * @param string url - the given url
247 * @return array of objects
249 array get_path(object obj)
252 while ( objectp(obj) ) {
253 path = ({ obj }) + path;
254 obj = obj->get_environment();