po: Update Norwegian translation.
[wine.git] / server / directory.c
blob7ffaedc462314cc4bd588cb69efa640a1d39a692
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "file.h"
40 #include "unicode.h"
42 #define HASH_SIZE 7 /* default hash size */
44 struct object_type
46 struct object obj; /* object header */
49 static void object_type_dump( struct object *obj, int verbose );
50 static struct object_type *object_type_get_type( struct object *obj );
52 static const struct object_ops object_type_ops =
54 sizeof(struct object_type), /* size */
55 object_type_dump, /* dump */
56 object_type_get_type, /* get_type */
57 no_add_queue, /* add_queue */
58 NULL, /* remove_queue */
59 NULL, /* signaled */
60 NULL, /* satisfied */
61 no_signal, /* signal */
62 no_get_fd, /* get_fd */
63 no_map_access, /* map_access */
64 default_get_sd, /* get_sd */
65 default_set_sd, /* set_sd */
66 no_lookup_name, /* lookup_name */
67 directory_link_name, /* link_name */
68 default_unlink_name, /* unlink_name */
69 no_open_file, /* open_file */
70 no_close_handle, /* close_handle */
71 no_destroy /* destroy */
75 struct directory
77 struct object obj; /* object header */
78 struct namespace *entries; /* directory's name space */
81 static void directory_dump( struct object *obj, int verbose );
82 static struct object_type *directory_get_type( struct object *obj );
83 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
84 unsigned int attr );
85 static void directory_destroy( struct object *obj );
87 static const struct object_ops directory_ops =
89 sizeof(struct directory), /* size */
90 directory_dump, /* dump */
91 directory_get_type, /* get_type */
92 no_add_queue, /* add_queue */
93 NULL, /* remove_queue */
94 NULL, /* signaled */
95 NULL, /* satisfied */
96 no_signal, /* signal */
97 no_get_fd, /* get_fd */
98 default_fd_map_access, /* map_access */
99 default_get_sd, /* get_sd */
100 default_set_sd, /* set_sd */
101 directory_lookup_name, /* lookup_name */
102 directory_link_name, /* link_name */
103 default_unlink_name, /* unlink_name */
104 no_open_file, /* open_file */
105 no_close_handle, /* close_handle */
106 directory_destroy /* destroy */
109 static struct directory *root_directory;
110 static struct directory *dir_objtype;
113 static void object_type_dump( struct object *obj, int verbose )
115 fputs( "Object type\n", stderr );
118 static struct object_type *object_type_get_type( struct object *obj )
120 static const WCHAR name[] = {'O','b','j','e','c','t','T','y','p','e'};
121 static const struct unicode_str str = { name, sizeof(name) };
122 return get_object_type( &str );
125 static void directory_dump( struct object *obj, int verbose )
127 fputs( "Directory\n", stderr );
130 static struct object_type *directory_get_type( struct object *obj )
132 static const WCHAR name[] = {'D','i','r','e','c','t','o','r','y'};
133 static const struct unicode_str str = { name, sizeof(name) };
134 return get_object_type( &str );
137 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
138 unsigned int attr )
140 struct directory *dir = (struct directory *)obj;
141 struct object *found;
142 struct unicode_str tmp;
143 const WCHAR *p;
145 assert( obj->ops == &directory_ops );
147 if (!name) return NULL; /* open the directory itself */
149 if (!(p = memchrW( name->str, '\\', name->len / sizeof(WCHAR) )))
150 /* Last element in the path name */
151 tmp.len = name->len;
152 else
153 tmp.len = (p - name->str) * sizeof(WCHAR);
155 tmp.str = name->str;
156 if ((found = find_object( dir->entries, &tmp, attr )))
158 /* Skip trailing \\ */
159 if (p)
161 p++;
162 tmp.len += sizeof(WCHAR);
164 /* Move to the next element*/
165 name->str = p;
166 name->len -= tmp.len;
167 return found;
170 if (name->str) /* not the last element */
172 if (tmp.len == 0) /* Double backslash */
173 set_error( STATUS_OBJECT_NAME_INVALID );
174 else if (p) /* Path still has backslashes */
175 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
177 return NULL;
180 int directory_link_name( struct object *obj, struct object_name *name, struct object *parent )
182 struct directory *dir = (struct directory *)parent;
184 if (parent->ops != &directory_ops)
186 set_error( STATUS_OBJECT_TYPE_MISMATCH );
187 return 0;
189 namespace_add( dir->entries, name );
190 name->parent = grab_object( parent );
191 return 1;
194 static void directory_destroy( struct object *obj )
196 struct directory *dir = (struct directory *)obj;
197 assert( obj->ops == &directory_ops );
198 free( dir->entries );
201 static struct directory *create_directory( struct object *root, const struct unicode_str *name,
202 unsigned int attr, unsigned int hash_size,
203 const struct security_descriptor *sd )
205 struct directory *dir;
207 if ((dir = create_named_object( root, &directory_ops, name, attr, sd )) &&
208 get_error() != STATUS_OBJECT_NAME_EXISTS)
210 if (!(dir->entries = create_namespace( hash_size )))
212 release_object( dir );
213 return NULL;
216 return dir;
219 struct object *get_root_directory(void)
221 return grab_object( root_directory );
224 /* return a directory object for creating/opening some object; no access rights are required */
225 struct object *get_directory_obj( struct process *process, obj_handle_t handle )
227 return get_handle_obj( process, handle, 0, &directory_ops );
230 /* retrieve an object type, creating it if needed */
231 struct object_type *get_object_type( const struct unicode_str *name )
233 struct object_type *type;
235 if ((type = create_named_object( &dir_objtype->obj, &object_type_ops, name, OBJ_OPENIF, NULL )))
237 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
239 grab_object( type );
240 make_object_static( &type->obj );
242 clear_error();
244 return type;
247 /* Global initialization */
249 static void create_session( unsigned int id )
251 /* directories */
252 static const WCHAR dir_sessionsW[] = {'S','e','s','s','i','o','n','s'};
253 static const WCHAR dir_bnolinksW[] = {'B','N','O','L','I','N','K','S'};
254 static const WCHAR dir_bnoW[] = {'B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
255 static const WCHAR dir_dosdevicesW[] = {'D','o','s','D','e','v','i','c','e','s'};
256 static const WCHAR dir_windowsW[] = {'W','i','n','d','o','w','s'};
257 static const WCHAR dir_winstationsW[] = {'W','i','n','d','o','w','S','t','a','t','i','o','n','s'};
258 static const struct unicode_str dir_sessions_str = {dir_sessionsW, sizeof(dir_sessionsW)};
259 static const struct unicode_str dir_bnolinks_str = {dir_bnolinksW, sizeof(dir_bnolinksW)};
260 static const struct unicode_str dir_bno_str = {dir_bnoW, sizeof(dir_bnoW)};
261 static const struct unicode_str dir_dosdevices_str = {dir_dosdevicesW, sizeof(dir_dosdevicesW)};
262 static const struct unicode_str dir_windows_str = {dir_windowsW, sizeof(dir_windowsW)};
263 static const struct unicode_str dir_winstations_str = {dir_winstationsW, sizeof(dir_winstationsW)};
265 /* symlinks */
266 static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
267 static const WCHAR link_localW[] = {'L','o','c','a','l'};
268 static const WCHAR link_sessionW[] = {'S','e','s','s','i','o','n'};
269 static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
270 static const struct unicode_str link_local_str = {link_localW, sizeof(link_localW)};
271 static const struct unicode_str link_session_str = {link_sessionW, sizeof(link_sessionW)};
273 static const WCHAR fmt_u[] = {'%','u',0};
274 static struct directory *dir_bno_global, *dir_sessions, *dir_bnolinks;
275 struct directory *dir_id, *dir_bno, *dir_dosdevices, *dir_windows, *dir_winstation;
276 struct object *link_global, *link_local, *link_session, *link_bno, *link_windows;
277 struct unicode_str id_str;
278 WCHAR id_strW[10];
280 if (!id)
282 dir_bno_global = create_directory( &root_directory->obj, &dir_bno_str, 0, HASH_SIZE, NULL );
283 dir_sessions = create_directory( &root_directory->obj, &dir_sessions_str, 0, HASH_SIZE, NULL );
284 dir_bnolinks = create_directory( &dir_sessions->obj, &dir_bnolinks_str, 0, HASH_SIZE, NULL );
285 make_object_static( (struct object *)dir_bno_global );
286 make_object_static( (struct object *)dir_bnolinks );
287 make_object_static( (struct object *)dir_sessions );
290 sprintfW( id_strW, fmt_u, id );
291 id_str.str = id_strW;
292 id_str.len = strlenW( id_strW ) * sizeof(WCHAR);
293 dir_id = create_directory( &dir_sessions->obj, &id_str, 0, HASH_SIZE, NULL );
294 dir_dosdevices = create_directory( &dir_id->obj, &dir_dosdevices_str, 0, HASH_SIZE, NULL );
296 /* for session 0, directories are created under the root */
297 if (!id)
299 dir_bno = (struct directory *)grab_object( dir_bno_global );
300 dir_windows = create_directory( &root_directory->obj, &dir_windows_str, 0, HASH_SIZE, NULL );
301 link_bno = create_obj_symlink( &dir_id->obj, &dir_bno_str, 0, &dir_bno->obj, NULL );
302 link_windows = create_obj_symlink( &dir_id->obj, &dir_windows_str, 0, &dir_windows->obj, NULL );
303 make_object_static( link_bno );
304 make_object_static( link_windows );
306 else
308 /* use a larger hash table for this one since it can contain a lot of objects */
309 dir_bno = create_directory( &dir_id->obj, &dir_bno_str, 0, 37, NULL );
310 dir_windows = create_directory( &dir_id->obj, &dir_windows_str, 0, HASH_SIZE, NULL );
312 dir_winstation = create_directory( &dir_windows->obj, &dir_winstations_str, 0, HASH_SIZE, NULL );
314 link_global = create_obj_symlink( &dir_bno->obj, &link_global_str, 0, &dir_bno_global->obj, NULL );
315 link_local = create_obj_symlink( &dir_bno->obj, &link_local_str, 0, &dir_bno->obj, NULL );
316 link_session = create_obj_symlink( &dir_bno->obj, &link_session_str, 0, &dir_bnolinks->obj, NULL );
317 link_bno = create_obj_symlink( &dir_bnolinks->obj, &id_str, 0, &dir_bno->obj, NULL );
318 make_object_static( link_global );
319 make_object_static( link_local );
320 make_object_static( link_session );
321 make_object_static( link_bno );
323 make_object_static( &dir_dosdevices->obj );
324 make_object_static( &dir_winstation->obj );
325 release_object( dir_windows );
326 release_object( dir_bno );
327 release_object( dir_id );
330 void init_directories(void)
332 /* Directories */
333 static const WCHAR dir_globalW[] = {'?','?'};
334 static const WCHAR dir_driverW[] = {'D','r','i','v','e','r'};
335 static const WCHAR dir_deviceW[] = {'D','e','v','i','c','e'};
336 static const WCHAR dir_objtypeW[] = {'O','b','j','e','c','t','T','y','p','e','s'};
337 static const WCHAR dir_kernelW[] = {'K','e','r','n','e','l','O','b','j','e','c','t','s'};
338 static const struct unicode_str dir_global_str = {dir_globalW, sizeof(dir_globalW)};
339 static const struct unicode_str dir_driver_str = {dir_driverW, sizeof(dir_driverW)};
340 static const struct unicode_str dir_device_str = {dir_deviceW, sizeof(dir_deviceW)};
341 static const struct unicode_str dir_objtype_str = {dir_objtypeW, sizeof(dir_objtypeW)};
342 static const struct unicode_str dir_kernel_str = {dir_kernelW, sizeof(dir_kernelW)};
344 /* symlinks */
345 static const WCHAR link_dosdevW[] = {'D','o','s','D','e','v','i','c','e','s'};
346 static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
347 static const WCHAR link_nulW[] = {'N','U','L'};
348 static const WCHAR link_pipeW[] = {'P','I','P','E'};
349 static const WCHAR link_mailslotW[] = {'M','A','I','L','S','L','O','T'};
350 static const struct unicode_str link_dosdev_str = {link_dosdevW, sizeof(link_dosdevW)};
351 static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
352 static const struct unicode_str link_nul_str = {link_nulW, sizeof(link_nulW)};
353 static const struct unicode_str link_pipe_str = {link_pipeW, sizeof(link_pipeW)};
354 static const struct unicode_str link_mailslot_str = {link_mailslotW, sizeof(link_mailslotW)};
356 /* devices */
357 static const WCHAR named_pipeW[] = {'N','a','m','e','d','P','i','p','e'};
358 static const WCHAR mailslotW[] = {'M','a','i','l','S','l','o','t'};
359 static const WCHAR nullW[] = {'N','u','l','l'};
360 static const struct unicode_str named_pipe_str = {named_pipeW, sizeof(named_pipeW)};
361 static const struct unicode_str mailslot_str = {mailslotW, sizeof(mailslotW)};
362 static const struct unicode_str null_str = {nullW, sizeof(nullW)};
364 /* events */
365 static const WCHAR event_low_memW[] = {'L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n'};
366 static const WCHAR event_low_pagedW[] = {'L','o','w','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
367 static const WCHAR event_low_nonpgW[] = {'L','o','w','N','o','n','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
368 static const WCHAR event_high_memW[] = {'H','i','g','h','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n'};
369 static const WCHAR event_high_pagedW[] = {'H','i','g','h','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
370 static const WCHAR event_high_nonpgW[] = {'H','i','g','h','N','o','n','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
371 static const WCHAR keyed_event_crit_sectW[] = {'C','r','i','t','S','e','c','O','u','t','O','f','M','e','m','o','r','y','E','v','e','n','t'};
372 static const struct unicode_str kernel_events[] =
374 { event_low_memW, sizeof(event_low_memW) },
375 { event_low_pagedW, sizeof(event_low_pagedW) },
376 { event_low_nonpgW, sizeof(event_low_nonpgW) },
377 { event_high_memW, sizeof(event_high_memW) },
378 { event_high_pagedW, sizeof(event_high_pagedW) },
379 { event_high_nonpgW, sizeof(event_high_nonpgW) }
381 static const struct unicode_str keyed_event_crit_sect_str = {keyed_event_crit_sectW, sizeof(keyed_event_crit_sectW)};
383 struct directory *dir_driver, *dir_device, *dir_global, *dir_kernel;
384 struct object *link_dosdev, *link_global, *link_nul, *link_pipe, *link_mailslot;
385 struct object *named_pipe_device, *mailslot_device, *null_device;
386 struct keyed_event *keyed_event;
387 unsigned int i;
389 root_directory = create_directory( NULL, NULL, 0, HASH_SIZE, NULL );
390 dir_driver = create_directory( &root_directory->obj, &dir_driver_str, 0, HASH_SIZE, NULL );
391 dir_device = create_directory( &root_directory->obj, &dir_device_str, 0, HASH_SIZE, NULL );
392 dir_objtype = create_directory( &root_directory->obj, &dir_objtype_str, 0, HASH_SIZE, NULL );
393 dir_kernel = create_directory( &root_directory->obj, &dir_kernel_str, 0, HASH_SIZE, NULL );
394 dir_global = create_directory( &root_directory->obj, &dir_global_str, 0, HASH_SIZE, NULL );
395 make_object_static( &root_directory->obj );
396 make_object_static( &dir_driver->obj );
397 make_object_static( &dir_objtype->obj );
399 /* devices */
400 named_pipe_device = create_named_pipe_device( &dir_device->obj, &named_pipe_str );
401 mailslot_device = create_mailslot_device( &dir_device->obj, &mailslot_str );
402 null_device = create_unix_device( &dir_device->obj, &null_str, "/dev/null" );
403 make_object_static( named_pipe_device );
404 make_object_static( mailslot_device );
405 make_object_static( null_device );
407 /* sessions */
408 create_session( 0 );
409 create_session( 1 );
411 /* symlinks */
412 link_dosdev = create_obj_symlink( &root_directory->obj, &link_dosdev_str, 0, &dir_global->obj, NULL );
413 link_global = create_obj_symlink( &dir_global->obj, &link_global_str, 0, &dir_global->obj, NULL );
414 link_nul = create_obj_symlink( &dir_global->obj, &link_nul_str, 0, null_device, NULL );
415 link_pipe = create_obj_symlink( &dir_global->obj, &link_pipe_str, 0, named_pipe_device, NULL );
416 link_mailslot = create_obj_symlink( &dir_global->obj, &link_mailslot_str, 0, mailslot_device, NULL );
417 make_object_static( link_dosdev );
418 make_object_static( link_global );
419 make_object_static( link_nul );
420 make_object_static( link_pipe );
421 make_object_static( link_mailslot );
423 /* events */
424 for (i = 0; i < sizeof(kernel_events)/sizeof(kernel_events[0]); i++)
426 struct event *event = create_event( &dir_kernel->obj, &kernel_events[i], 0, 1, 0, NULL );
427 make_object_static( (struct object *)event );
429 keyed_event = create_keyed_event( &dir_kernel->obj, &keyed_event_crit_sect_str, 0, NULL );
430 make_object_static( (struct object *)keyed_event );
432 /* the objects hold references so we can release these directories */
433 release_object( dir_global );
434 release_object( dir_device );
435 release_object( dir_kernel );
438 /* create a directory object */
439 DECL_HANDLER(create_directory)
441 struct unicode_str name;
442 struct object *root;
443 struct directory *dir;
444 const struct security_descriptor *sd;
445 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
447 if (!objattr) return;
449 if ((dir = create_directory( root, &name, objattr->attributes, HASH_SIZE, sd )))
451 reply->handle = alloc_handle( current->process, dir, req->access, objattr->attributes );
452 release_object( dir );
455 if (root) release_object( root );
458 /* open a directory object */
459 DECL_HANDLER(open_directory)
461 struct unicode_str name = get_req_unicode_str();
463 reply->handle = open_object( current->process, req->rootdir, req->access,
464 &directory_ops, &name, req->attributes );
467 /* get a directory entry by index */
468 DECL_HANDLER(get_directory_entry)
470 struct directory *dir = (struct directory *)get_handle_obj( current->process, req->handle,
471 DIRECTORY_QUERY, &directory_ops );
472 if (dir)
474 struct object *obj = find_object_index( dir->entries, req->index );
475 if (obj)
477 data_size_t name_len, type_len = 0;
478 const WCHAR *type_name = NULL;
479 const WCHAR *name = get_object_name( obj, &name_len );
480 struct object_type *type = obj->ops->get_type( obj );
482 if (type) type_name = get_object_name( &type->obj, &type_len );
484 if (name_len + type_len <= get_reply_max_size())
486 void *ptr = set_reply_data_size( name_len + type_len );
487 if (ptr)
489 reply->name_len = name_len;
490 memcpy( ptr, name, name_len );
491 memcpy( (char *)ptr + name_len, type_name, type_len );
494 else set_error( STATUS_BUFFER_OVERFLOW );
496 if (type) release_object( type );
497 release_object( obj );
499 release_object( dir );
503 /* unlink a named object */
504 DECL_HANDLER(unlink_object)
506 struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
508 if (obj)
510 unlink_named_object( obj );
511 release_object( obj );
515 /* query object type name information */
516 DECL_HANDLER(get_object_type)
518 struct object *obj;
519 struct object_type *type;
520 const WCHAR *name;
522 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
524 if ((type = obj->ops->get_type( obj )))
526 if ((name = get_object_name( &type->obj, &reply->total )))
527 set_reply_data( name, min( reply->total, get_reply_max_size() ) );
528 release_object( type );
530 release_object( obj );