winspool: Document the monitor functions.
[wine/dcerpc.git] / server / directory.c
blob21dec3ab312bbc837bd1948814c8061983673862
1 /*
2 * Server-side directory object management
4 * Copyright (C) 2005 Vitaliy Margolen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sys/types.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "winternl.h"
34 #include "ddk/wdm.h"
36 #include "handle.h"
37 #include "request.h"
38 #include "process.h"
39 #include "object.h"
40 #include "unicode.h"
42 #define HASH_SIZE 7 /* default hash size */
44 struct directory
46 struct object obj; /* object header */
47 struct namespace *entries; /* directory's name space */
50 static void directory_dump( struct object *obj, int verbose );
51 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
52 unsigned int attr );
53 static void directory_destroy( struct object *obj );
55 static const struct object_ops directory_ops =
57 sizeof(struct directory), /* size */
58 directory_dump, /* dump */
59 no_add_queue, /* add_queue */
60 NULL, /* remove_queue */
61 NULL, /* signaled */
62 NULL, /* satisfied */
63 no_signal, /* signal */
64 no_get_fd, /* get_fd */
65 directory_lookup_name, /* lookup_name */
66 no_close_handle, /* close_handle */
67 directory_destroy /* destroy */
70 static struct directory *root_directory;
73 static void directory_dump( struct object *obj, int verbose )
75 assert( obj->ops == &directory_ops );
77 fputs( "Directory ", stderr );
78 dump_object_name( obj );
79 fputc( '\n', stderr );
82 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
83 unsigned int attr )
85 struct directory *dir = (struct directory *)obj;
86 struct object *found;
87 struct unicode_str tmp;
88 const WCHAR *p;
90 assert( obj->ops == &directory_ops );
92 if (!(p = memchrW( name->str, '\\', name->len / sizeof(WCHAR) )))
93 /* Last element in the path name */
94 tmp.len = name->len;
95 else
96 tmp.len = (p - name->str) * sizeof(WCHAR);
98 tmp.str = name->str;
99 if ((found = find_object( dir->entries, &tmp, attr )))
101 /* Skip trailing \\ */
102 if (p)
104 p++;
105 tmp.len += sizeof(WCHAR);
107 /* Move to the next element*/
108 name->str = p;
109 name->len -= tmp.len;
110 return found;
113 if (name->str)
115 if (tmp.len == 0) /* Double backslash */
116 set_error( STATUS_OBJECT_NAME_INVALID );
117 else if (p) /* Path still has backslashes */
118 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
120 return NULL;
123 static void directory_destroy( struct object *obj )
125 struct directory *dir = (struct directory *)obj;
126 assert( obj->ops == &directory_ops );
127 free( dir->entries );
130 static struct directory *create_directory( struct directory *root, const struct unicode_str *name,
131 unsigned int attr, unsigned int hash_size )
133 struct directory *dir;
135 if ((dir = create_named_object_dir( root, name, attr, &directory_ops )) &&
136 get_error() != STATUS_OBJECT_NAME_EXISTS)
138 if (!(dir->entries = create_namespace( hash_size )))
140 release_object( dir );
141 dir = NULL;
144 return dir;
147 struct directory *get_directory_obj( struct process *process, obj_handle_t handle, unsigned int access )
149 return (struct directory *)get_handle_obj( process, handle, access, &directory_ops );
152 /******************************************************************************
153 * Find an object by its name in a given root object
155 * PARAMS
156 * root [I] directory to start search from or NULL to start from \\
157 * name [I] object name to search for
158 * attr [I] OBJECT_ATTRIBUTES.Attributes
159 * name_left [O] [optional] leftover name if object is not found
161 * RETURNS
162 * NULL: If params are invalid
163 * Found: If object with exact name is found returns that object
164 * (name_left->len == 0). Object's refcount is incremented
165 * Not found: The last matched parent. (name_left->len > 0)
166 * Parent's refcount is incremented.
168 struct object *find_object_dir( struct directory *root, const struct unicode_str *name,
169 unsigned int attr, struct unicode_str *name_left )
171 struct object *obj, *parent;
172 struct unicode_str name_tmp;
174 if (name) name_tmp = *name;
175 else name_tmp.len = 0;
177 /* Arguments check:
178 * - Either rootdir or name have to be specified
179 * - If root is specified path shouldn't start with backslash */
180 if (root)
182 if (name_tmp.len && name_tmp.str[0] == '\\')
184 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
185 return NULL;
187 parent = grab_object( root );
189 else
191 if (!name_tmp.len || name_tmp.str[0] != '\\')
193 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
194 return NULL;
196 parent = grab_object( &root_directory->obj );
197 /* skip leading backslash */
198 name_tmp.str++;
199 name_tmp.len -= sizeof(WCHAR);
202 /* Special case for opening RootDirectory */
203 if (!name_tmp.len) goto done;
205 while ((obj = parent->ops->lookup_name( parent, &name_tmp, attr )))
207 /* move to the next element */
208 release_object ( parent );
209 parent = obj;
211 if (get_error())
213 release_object( parent );
214 return NULL;
217 done:
218 if (name_left) *name_left = name_tmp;
219 return parent;
222 /* create a named (if name is present) or unnamed object. */
223 void *create_named_object_dir( struct directory *root, const struct unicode_str *name,
224 unsigned int attributes, const struct object_ops *ops )
226 struct object *obj, *new_obj = NULL;
227 struct unicode_str new_name;
229 if (!name || !name->len) return alloc_object( ops );
231 if (!(obj = find_object_dir( root, name, attributes, &new_name ))) return NULL;
232 if (!new_name.len)
234 if (attributes & OBJ_OPENIF && obj->ops == ops)
235 set_error( STATUS_OBJECT_NAME_EXISTS );
236 else
238 release_object( obj );
239 obj = NULL;
240 if (attributes & OBJ_OPENIF)
241 set_error( STATUS_OBJECT_TYPE_MISMATCH );
242 else
243 set_error( STATUS_OBJECT_NAME_COLLISION );
245 return obj;
248 /* ATM we can't insert objects into anything else but directories */
249 if (obj->ops != &directory_ops)
250 set_error( STATUS_OBJECT_TYPE_MISMATCH );
251 else
253 struct directory *dir = (struct directory *)obj;
254 if ((new_obj = create_object( dir->entries, ops, &new_name, &dir->obj )))
255 clear_error();
258 release_object( obj );
259 return new_obj;
262 /* open a new handle to an existing object */
263 void *open_object_dir( struct directory *root, const struct unicode_str *name,
264 unsigned int attr, const struct object_ops *ops )
266 struct unicode_str name_left;
267 struct object *obj;
269 if ((obj = find_object_dir( root, name, attr, &name_left )))
271 if (name_left.len) /* not fully parsed */
272 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
273 else if (ops && obj->ops != ops)
274 set_error( STATUS_OBJECT_TYPE_MISMATCH );
275 else
276 return obj;
278 release_object( obj );
280 return NULL;
284 /* Global initialization */
286 static struct directory *dir_driver, *dir_device;
287 static struct symlink *link_dosdev, *link_global1, *link_global2, *link_local;
288 static struct named_pipe_device *dev_named_pipe;
289 static struct mailslot_device *dev_mailslot;
291 void init_directories(void)
293 /* Directories */
294 static const WCHAR dir_globalW[] = {'\\','?','?'};
295 static const WCHAR dir_driverW[] = {'D','r','i','v','e','r'};
296 static const WCHAR dir_deviceW[] = {'D','e','v','i','c','e'};
297 static const WCHAR dir_basenamedW[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
298 static const struct unicode_str dir_global_str = {dir_globalW, sizeof(dir_globalW)};
299 static const struct unicode_str dir_driver_str = {dir_driverW, sizeof(dir_driverW)};
300 static const struct unicode_str dir_device_str = {dir_deviceW, sizeof(dir_deviceW)};
301 static const struct unicode_str dir_basenamed_str = {dir_basenamedW, sizeof(dir_basenamedW)};
303 /* symlinks */
304 static const WCHAR link_dosdevW[] = {'D','o','s','D','e','v','i','c','e','s'};
305 static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
306 static const WCHAR link_localW[] = {'L','o','c','a','l'};
307 static const struct unicode_str link_dosdev_str = {link_dosdevW, sizeof(link_dosdevW)};
308 static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
309 static const struct unicode_str link_local_str = {link_localW, sizeof(link_localW)};
311 /* devices */
312 static const WCHAR pipeW[] = {'P','I','P','E'};
313 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T'};
314 static const struct unicode_str pipe_str = {pipeW, sizeof(pipeW)};
315 static const struct unicode_str mailslot_str = {mailslotW, sizeof(mailslotW)};
317 struct directory *dir_global, *dir_basenamed;
319 root_directory = create_directory( NULL, NULL, 0, HASH_SIZE );
320 dir_driver = create_directory( root_directory, &dir_driver_str, 0, HASH_SIZE );
321 dir_device = create_directory( root_directory, &dir_device_str, 0, HASH_SIZE );
323 dir_global = create_directory( NULL, &dir_global_str, 0, HASH_SIZE );
324 /* use a larger hash table for this one since it can contain a lot of objects */
325 dir_basenamed = create_directory( NULL, &dir_basenamed_str, 0, 37 );
327 /* symlinks */
328 link_dosdev = create_symlink( root_directory, &link_dosdev_str, 0, &dir_global_str );
329 link_global1 = create_symlink( dir_global, &link_global_str, 0, &dir_global_str );
330 link_global2 = create_symlink( dir_basenamed, &link_global_str, 0, &dir_basenamed_str );
331 link_local = create_symlink( dir_basenamed, &link_local_str, 0, &dir_basenamed_str );
333 /* devices */
334 dev_named_pipe = create_named_pipe_device( dir_global, &pipe_str );
335 dev_mailslot = create_mailslot_device( dir_global, &mailslot_str );
337 /* the symlinks or devices hold references so we can release these */
338 release_object( dir_global );
339 release_object( dir_basenamed );
342 void close_directories(void)
344 release_object( dev_named_pipe );
345 release_object( dev_mailslot );
347 release_object( link_dosdev );
348 release_object( link_global1 );
349 release_object( link_global2 );
350 release_object( link_local );
352 release_object( dir_device );
353 release_object( dir_driver );
354 release_object( root_directory );
358 /* create a directory object */
359 DECL_HANDLER(create_directory)
361 struct unicode_str name;
362 struct directory *dir, *root = NULL;
364 reply->handle = 0;
365 get_req_unicode_str( &name );
366 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
367 return;
369 if ((dir = create_directory( root, &name, req->attributes, HASH_SIZE )))
371 reply->handle = alloc_handle( current->process, dir, req->access,
372 req->attributes & OBJ_INHERIT );
373 release_object( dir );
376 if (root) release_object( root );
379 /* open a directory object */
380 DECL_HANDLER(open_directory)
382 struct unicode_str name;
383 struct directory *dir, *root = NULL;
385 get_req_unicode_str( &name );
386 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
387 return;
389 if ((dir = open_object_dir( root, &name, req->attributes, &directory_ops )))
391 reply->handle = alloc_handle( current->process, &dir->obj, req->access,
392 req->attributes & OBJ_INHERIT );
393 release_object( dir );
396 if (root) release_object( root );