server: Allow async i/o operations to send completion messages.
[wine] / server / directory.c
1 /*
2  * Server-side directory object management
3  *
4  * Copyright (C) 2005 Vitaliy Margolen
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "winternl.h"
34 #include "ddk/wdm.h"
35
36 #include "handle.h"
37 #include "request.h"
38 #include "process.h"
39 #include "file.h"
40 #include "unicode.h"
41
42 #define HASH_SIZE 7  /* default hash size */
43
44 struct directory
45 {
46     struct object     obj;        /* object header */
47     struct namespace *entries;    /* directory's name space */
48 };
49
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 );
54
55 static const struct object_ops directory_ops =
56 {
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     default_fd_map_access,        /* map_access */
66     default_get_sd,               /* get_sd */
67     default_set_sd,               /* set_sd */
68     directory_lookup_name,        /* lookup_name */
69     no_open_file,                 /* open_file */
70     no_close_handle,              /* close_handle */
71     directory_destroy             /* destroy */
72 };
73
74 static struct directory *root_directory;
75
76
77 static void directory_dump( struct object *obj, int verbose )
78 {
79     assert( obj->ops == &directory_ops );
80
81     fputs( "Directory ", stderr );
82     dump_object_name( obj );
83     fputc( '\n', stderr );
84 }
85
86 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
87                                              unsigned int attr )
88 {
89     struct directory *dir = (struct directory *)obj;
90     struct object *found;
91     struct unicode_str tmp;
92     const WCHAR *p;
93
94     assert( obj->ops == &directory_ops );
95
96     if (!(p = memchrW( name->str, '\\', name->len / sizeof(WCHAR) )))
97         /* Last element in the path name */
98         tmp.len = name->len;
99     else
100         tmp.len = (p - name->str) * sizeof(WCHAR);
101
102     tmp.str = name->str;
103     if ((found = find_object( dir->entries, &tmp, attr )))
104     {
105         /* Skip trailing \\ */
106         if (p)
107         {
108             p++;
109             tmp.len += sizeof(WCHAR);
110         }
111         /* Move to the next element*/
112         name->str = p;
113         name->len -= tmp.len;
114         return found;
115     }
116
117     if (name->str)
118     {
119         if (tmp.len == 0) /* Double backslash */
120             set_error( STATUS_OBJECT_NAME_INVALID );
121         else if (p)  /* Path still has backslashes */
122             set_error( STATUS_OBJECT_PATH_NOT_FOUND );
123     }
124     return NULL;
125 }
126
127 static void directory_destroy( struct object *obj )
128 {
129     struct directory *dir = (struct directory *)obj;
130     assert( obj->ops == &directory_ops );
131     free( dir->entries );
132 }
133
134 static struct directory *create_directory( struct directory *root, const struct unicode_str *name,
135                                            unsigned int attr, unsigned int hash_size )
136 {
137     struct directory *dir;
138
139     if ((dir = create_named_object_dir( root, name, attr, &directory_ops )) &&
140         get_error() != STATUS_OBJECT_NAME_EXISTS)
141     {
142         if (!(dir->entries = create_namespace( hash_size )))
143         {
144             release_object( dir );
145             dir = NULL;
146         }
147     }
148     return dir;
149 }
150
151 struct directory *get_directory_obj( struct process *process, obj_handle_t handle, unsigned int access )
152 {
153     return (struct directory *)get_handle_obj( process, handle, access, &directory_ops );
154 }
155
156 /******************************************************************************
157  * Find an object by its name in a given root object
158  *
159  * PARAMS
160  *  root      [I] directory to start search from or NULL to start from \\
161  *  name      [I] object name to search for
162  *  attr      [I] OBJECT_ATTRIBUTES.Attributes
163  *  name_left [O] [optional] leftover name if object is not found
164  *
165  * RETURNS
166  *  NULL:      If params are invalid
167  *  Found:     If object with exact name is found returns that object
168  *             (name_left->len == 0). Object's refcount is incremented
169  *  Not found: The last matched parent. (name_left->len > 0)
170  *             Parent's refcount is incremented.
171  */
172 struct object *find_object_dir( struct directory *root, const struct unicode_str *name,
173                                 unsigned int attr, struct unicode_str *name_left )
174 {
175     struct object *obj, *parent;
176     struct unicode_str name_tmp;
177
178     if (name) name_tmp = *name;
179     else name_tmp.len = 0;
180
181     /* Arguments check:
182      * - Either rootdir or name have to be specified
183      * - If root is specified path shouldn't start with backslash */
184     if (root)
185     {
186         if (name_tmp.len && name_tmp.str[0] == '\\')
187         {
188             set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
189             return NULL;
190         }
191         parent = grab_object( root );
192     }
193     else
194     {
195         if (!name_tmp.len || name_tmp.str[0] != '\\')
196         {
197             set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
198             return NULL;
199         }
200         parent = grab_object( &root_directory->obj );
201         /* skip leading backslash */
202         name_tmp.str++;
203         name_tmp.len -= sizeof(WCHAR);
204     }
205
206     /* Special case for opening RootDirectory */
207     if (!name_tmp.len) goto done;
208
209     while ((obj = parent->ops->lookup_name( parent, &name_tmp, attr )))
210     {
211         /* move to the next element */
212         release_object ( parent );
213         parent = obj;
214     }
215     if (get_error())
216     {
217         release_object( parent );
218         return NULL;
219     }
220
221     done:
222     if (name_left) *name_left = name_tmp;
223     return parent;
224 }
225
226 /* create a named (if name is present) or unnamed object. */
227 void *create_named_object_dir( struct directory *root, const struct unicode_str *name,
228                                unsigned int attributes, const struct object_ops *ops )
229 {
230     struct object *obj, *new_obj = NULL;
231     struct unicode_str new_name;
232
233     if (!name || !name->len) return alloc_object( ops );
234
235     if (!(obj = find_object_dir( root, name, attributes, &new_name ))) return NULL;
236     if (!new_name.len)
237     {
238         if (attributes & OBJ_OPENIF && obj->ops == ops)
239             set_error( STATUS_OBJECT_NAME_EXISTS );
240         else
241         {
242             release_object( obj );
243             obj = NULL;
244             if (attributes & OBJ_OPENIF)
245                 set_error( STATUS_OBJECT_TYPE_MISMATCH );
246             else
247                 set_error( STATUS_OBJECT_NAME_COLLISION );
248         }
249         return obj;
250     }
251
252     /* ATM we can't insert objects into anything else but directories */
253     if (obj->ops != &directory_ops)
254         set_error( STATUS_OBJECT_TYPE_MISMATCH );
255     else
256     {
257         struct directory *dir = (struct directory *)obj;
258         if ((new_obj = create_object( dir->entries, ops, &new_name, &dir->obj )))
259             clear_error();
260     }
261
262     release_object( obj );
263     return new_obj;
264 }
265
266 /* open a new handle to an existing object */
267 void *open_object_dir( struct directory *root, const struct unicode_str *name,
268                        unsigned int attr, const struct object_ops *ops )
269 {
270     struct unicode_str name_left;
271     struct object *obj;
272
273     if ((obj = find_object_dir( root, name, attr, &name_left )))
274     {
275         if (name_left.len) /* not fully parsed */
276             set_error( STATUS_OBJECT_NAME_NOT_FOUND );
277         else if (ops && obj->ops != ops)
278             set_error( STATUS_OBJECT_TYPE_MISMATCH );
279         else
280             return obj;
281
282         release_object( obj );
283     }
284     return NULL;
285 }
286
287
288 /* Global initialization */
289
290 void init_directories(void)
291 {
292     /* Directories */
293     static const WCHAR dir_globalW[] = {'\\','?','?'};
294     static const WCHAR dir_driverW[] = {'D','r','i','v','e','r'};
295     static const WCHAR dir_deviceW[] = {'D','e','v','i','c','e'};
296     static const WCHAR dir_basenamedW[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
297     static const WCHAR dir_named_pipeW[] = {'\\','D','e','v','i','c','e','\\','N','a','m','e','d','P','i','p','e'};
298     static const WCHAR dir_mailslotW[] = {'\\','D','e','v','i','c','e','\\','M','a','i','l','S','l','o','t'};
299     static const struct unicode_str dir_global_str = {dir_globalW, sizeof(dir_globalW)};
300     static const struct unicode_str dir_driver_str = {dir_driverW, sizeof(dir_driverW)};
301     static const struct unicode_str dir_device_str = {dir_deviceW, sizeof(dir_deviceW)};
302     static const struct unicode_str dir_basenamed_str = {dir_basenamedW, sizeof(dir_basenamedW)};
303     static const struct unicode_str dir_named_pipe_str = {dir_named_pipeW, sizeof(dir_named_pipeW)};
304     static const struct unicode_str dir_mailslot_str = {dir_mailslotW, sizeof(dir_mailslotW)};
305
306     /* symlinks */
307     static const WCHAR link_dosdevW[] = {'D','o','s','D','e','v','i','c','e','s'};
308     static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
309     static const WCHAR link_localW[]  = {'L','o','c','a','l'};
310     static const WCHAR link_pipeW[]   = {'P','I','P','E'};
311     static const WCHAR link_mailslotW[] = {'M','A','I','L','S','L','O','T'};
312     static const struct unicode_str link_dosdev_str = {link_dosdevW, sizeof(link_dosdevW)};
313     static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
314     static const struct unicode_str link_local_str  = {link_localW, sizeof(link_localW)};
315     static const struct unicode_str link_pipe_str   = {link_pipeW, sizeof(link_pipeW)};
316     static const struct unicode_str link_mailslot_str = {link_mailslotW, sizeof(link_mailslotW)};
317
318     /* devices */
319     static const WCHAR named_pipeW[] = {'N','a','m','e','d','P','i','p','e'};
320     static const WCHAR mailslotW[] = {'M','a','i','l','S','l','o','t'};
321     static const struct unicode_str named_pipe_str = {named_pipeW, sizeof(named_pipeW)};
322     static const struct unicode_str mailslot_str = {mailslotW, sizeof(mailslotW)};
323
324     struct directory *dir_driver, *dir_device, *dir_global, *dir_basenamed;
325     struct symlink *link_dosdev, *link_global1, *link_global2, *link_local, *link_pipe, *link_mailslot;
326
327     root_directory = create_directory( NULL, NULL, 0, HASH_SIZE );
328     dir_driver     = create_directory( root_directory, &dir_driver_str, 0, HASH_SIZE );
329     dir_device     = create_directory( root_directory, &dir_device_str, 0, HASH_SIZE );
330     make_object_static( &root_directory->obj );
331     make_object_static( &dir_driver->obj );
332
333     dir_global     = create_directory( NULL, &dir_global_str, 0, HASH_SIZE );
334     /* use a larger hash table for this one since it can contain a lot of objects */
335     dir_basenamed  = create_directory( NULL, &dir_basenamed_str, 0, 37 );
336
337     /* devices */
338     create_named_pipe_device( dir_device, &named_pipe_str );
339     create_mailslot_device( dir_device, &mailslot_str );
340
341     /* symlinks */
342     link_dosdev    = create_symlink( root_directory, &link_dosdev_str, 0, &dir_global_str );
343     link_global1   = create_symlink( dir_global, &link_global_str, 0, &dir_global_str );
344     link_global2   = create_symlink( dir_basenamed, &link_global_str, 0, &dir_basenamed_str );
345     link_local     = create_symlink( dir_basenamed, &link_local_str, 0, &dir_basenamed_str );
346     link_pipe      = create_symlink( dir_global, &link_pipe_str, 0, &dir_named_pipe_str );
347     link_mailslot  = create_symlink( dir_global, &link_mailslot_str, 0, &dir_mailslot_str );
348     make_object_static( (struct object *)link_dosdev );
349     make_object_static( (struct object *)link_global1 );
350     make_object_static( (struct object *)link_global2 );
351     make_object_static( (struct object *)link_local );
352     make_object_static( (struct object *)link_pipe );
353     make_object_static( (struct object *)link_mailslot );
354
355     /* the symlinks or devices hold references so we can release these */
356     release_object( dir_global );
357     release_object( dir_device );
358     release_object( dir_basenamed );
359 }
360
361 /* create a directory object */
362 DECL_HANDLER(create_directory)
363 {
364     struct unicode_str name;
365     struct directory *dir, *root = NULL;
366
367     reply->handle = 0;
368     get_req_unicode_str( &name );
369     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
370         return;
371
372     if ((dir = create_directory( root, &name, req->attributes, HASH_SIZE )))
373     {
374         reply->handle = alloc_handle( current->process, dir, req->access, req->attributes );
375         release_object( dir );
376     }
377
378     if (root) release_object( root );
379 }
380
381 /* open a directory object */
382 DECL_HANDLER(open_directory)
383 {
384     struct unicode_str name;
385     struct directory *dir, *root = NULL;
386
387     get_req_unicode_str( &name );
388     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
389         return;
390
391     if ((dir = open_object_dir( root, &name, req->attributes, &directory_ops )))
392     {
393         reply->handle = alloc_handle( current->process, &dir->obj, req->access, req->attributes );
394         release_object( dir );
395     }
396
397     if (root) release_object( root );
398 }