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
23 #include "wine/port.h"
29 #include <sys/types.h>
32 #define WIN32_NO_STATUS
42 #define HASH_SIZE 7 /* default hash size */
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
,
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 */
63 no_signal
, /* signal */
64 no_get_fd
, /* get_fd */
65 default_fd_map_access
, /* map_access */
66 directory_lookup_name
, /* lookup_name */
67 no_open_file
, /* open_file */
68 no_close_handle
, /* close_handle */
69 directory_destroy
/* destroy */
72 static struct directory
*root_directory
;
75 static void directory_dump( struct object
*obj
, int verbose
)
77 assert( obj
->ops
== &directory_ops
);
79 fputs( "Directory ", stderr
);
80 dump_object_name( obj
);
81 fputc( '\n', stderr
);
84 static struct object
*directory_lookup_name( struct object
*obj
, struct unicode_str
*name
,
87 struct directory
*dir
= (struct directory
*)obj
;
89 struct unicode_str tmp
;
92 assert( obj
->ops
== &directory_ops
);
94 if (!(p
= memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )))
95 /* Last element in the path name */
98 tmp
.len
= (p
- name
->str
) * sizeof(WCHAR
);
101 if ((found
= find_object( dir
->entries
, &tmp
, attr
)))
103 /* Skip trailing \\ */
107 tmp
.len
+= sizeof(WCHAR
);
109 /* Move to the next element*/
111 name
->len
-= tmp
.len
;
117 if (tmp
.len
== 0) /* Double backslash */
118 set_error( STATUS_OBJECT_NAME_INVALID
);
119 else if (p
) /* Path still has backslashes */
120 set_error( STATUS_OBJECT_PATH_NOT_FOUND
);
125 static void directory_destroy( struct object
*obj
)
127 struct directory
*dir
= (struct directory
*)obj
;
128 assert( obj
->ops
== &directory_ops
);
129 free( dir
->entries
);
132 static struct directory
*create_directory( struct directory
*root
, const struct unicode_str
*name
,
133 unsigned int attr
, unsigned int hash_size
)
135 struct directory
*dir
;
137 if ((dir
= create_named_object_dir( root
, name
, attr
, &directory_ops
)) &&
138 get_error() != STATUS_OBJECT_NAME_EXISTS
)
140 if (!(dir
->entries
= create_namespace( hash_size
)))
142 release_object( dir
);
149 struct directory
*get_directory_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
151 return (struct directory
*)get_handle_obj( process
, handle
, access
, &directory_ops
);
154 /******************************************************************************
155 * Find an object by its name in a given root object
158 * root [I] directory to start search from or NULL to start from \\
159 * name [I] object name to search for
160 * attr [I] OBJECT_ATTRIBUTES.Attributes
161 * name_left [O] [optional] leftover name if object is not found
164 * NULL: If params are invalid
165 * Found: If object with exact name is found returns that object
166 * (name_left->len == 0). Object's refcount is incremented
167 * Not found: The last matched parent. (name_left->len > 0)
168 * Parent's refcount is incremented.
170 struct object
*find_object_dir( struct directory
*root
, const struct unicode_str
*name
,
171 unsigned int attr
, struct unicode_str
*name_left
)
173 struct object
*obj
, *parent
;
174 struct unicode_str name_tmp
;
176 if (name
) name_tmp
= *name
;
177 else name_tmp
.len
= 0;
180 * - Either rootdir or name have to be specified
181 * - If root is specified path shouldn't start with backslash */
184 if (name_tmp
.len
&& name_tmp
.str
[0] == '\\')
186 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
189 parent
= grab_object( root
);
193 if (!name_tmp
.len
|| name_tmp
.str
[0] != '\\')
195 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
198 parent
= grab_object( &root_directory
->obj
);
199 /* skip leading backslash */
201 name_tmp
.len
-= sizeof(WCHAR
);
204 /* Special case for opening RootDirectory */
205 if (!name_tmp
.len
) goto done
;
207 while ((obj
= parent
->ops
->lookup_name( parent
, &name_tmp
, attr
)))
209 /* move to the next element */
210 release_object ( parent
);
215 release_object( parent
);
220 if (name_left
) *name_left
= name_tmp
;
224 /* create a named (if name is present) or unnamed object. */
225 void *create_named_object_dir( struct directory
*root
, const struct unicode_str
*name
,
226 unsigned int attributes
, const struct object_ops
*ops
)
228 struct object
*obj
, *new_obj
= NULL
;
229 struct unicode_str new_name
;
231 if (!name
|| !name
->len
) return alloc_object( ops
);
233 if (!(obj
= find_object_dir( root
, name
, attributes
, &new_name
))) return NULL
;
236 if (attributes
& OBJ_OPENIF
&& obj
->ops
== ops
)
237 set_error( STATUS_OBJECT_NAME_EXISTS
);
240 release_object( obj
);
242 if (attributes
& OBJ_OPENIF
)
243 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
245 set_error( STATUS_OBJECT_NAME_COLLISION
);
250 /* ATM we can't insert objects into anything else but directories */
251 if (obj
->ops
!= &directory_ops
)
252 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
255 struct directory
*dir
= (struct directory
*)obj
;
256 if ((new_obj
= create_object( dir
->entries
, ops
, &new_name
, &dir
->obj
)))
260 release_object( obj
);
264 /* open a new handle to an existing object */
265 void *open_object_dir( struct directory
*root
, const struct unicode_str
*name
,
266 unsigned int attr
, const struct object_ops
*ops
)
268 struct unicode_str name_left
;
271 if ((obj
= find_object_dir( root
, name
, attr
, &name_left
)))
273 if (name_left
.len
) /* not fully parsed */
274 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
275 else if (ops
&& obj
->ops
!= ops
)
276 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
280 release_object( obj
);
286 /* Global initialization */
288 void init_directories(void)
291 static const WCHAR dir_globalW
[] = {'\\','?','?'};
292 static const WCHAR dir_driverW
[] = {'D','r','i','v','e','r'};
293 static const WCHAR dir_deviceW
[] = {'D','e','v','i','c','e'};
294 static const WCHAR dir_basenamedW
[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
295 static const WCHAR dir_named_pipeW
[] = {'\\','D','e','v','i','c','e','\\','N','a','m','e','d','P','i','p','e'};
296 static const WCHAR dir_mailslotW
[] = {'\\','D','e','v','i','c','e','\\','M','a','i','l','S','l','o','t'};
297 static const struct unicode_str dir_global_str
= {dir_globalW
, sizeof(dir_globalW
)};
298 static const struct unicode_str dir_driver_str
= {dir_driverW
, sizeof(dir_driverW
)};
299 static const struct unicode_str dir_device_str
= {dir_deviceW
, sizeof(dir_deviceW
)};
300 static const struct unicode_str dir_basenamed_str
= {dir_basenamedW
, sizeof(dir_basenamedW
)};
301 static const struct unicode_str dir_named_pipe_str
= {dir_named_pipeW
, sizeof(dir_named_pipeW
)};
302 static const struct unicode_str dir_mailslot_str
= {dir_mailslotW
, sizeof(dir_mailslotW
)};
305 static const WCHAR link_dosdevW
[] = {'D','o','s','D','e','v','i','c','e','s'};
306 static const WCHAR link_globalW
[] = {'G','l','o','b','a','l'};
307 static const WCHAR link_localW
[] = {'L','o','c','a','l'};
308 static const WCHAR link_pipeW
[] = {'P','I','P','E'};
309 static const WCHAR link_mailslotW
[] = {'M','A','I','L','S','L','O','T'};
310 static const struct unicode_str link_dosdev_str
= {link_dosdevW
, sizeof(link_dosdevW
)};
311 static const struct unicode_str link_global_str
= {link_globalW
, sizeof(link_globalW
)};
312 static const struct unicode_str link_local_str
= {link_localW
, sizeof(link_localW
)};
313 static const struct unicode_str link_pipe_str
= {link_pipeW
, sizeof(link_pipeW
)};
314 static const struct unicode_str link_mailslot_str
= {link_mailslotW
, sizeof(link_mailslotW
)};
317 static const WCHAR named_pipeW
[] = {'N','a','m','e','d','P','i','p','e'};
318 static const WCHAR mailslotW
[] = {'M','a','i','l','S','l','o','t'};
319 static const struct unicode_str named_pipe_str
= {named_pipeW
, sizeof(named_pipeW
)};
320 static const struct unicode_str mailslot_str
= {mailslotW
, sizeof(mailslotW
)};
322 struct directory
*dir_driver
, *dir_device
, *dir_global
, *dir_basenamed
;
323 struct symlink
*link_dosdev
, *link_global1
, *link_global2
, *link_local
, *link_pipe
, *link_mailslot
;
325 root_directory
= create_directory( NULL
, NULL
, 0, HASH_SIZE
);
326 dir_driver
= create_directory( root_directory
, &dir_driver_str
, 0, HASH_SIZE
);
327 dir_device
= create_directory( root_directory
, &dir_device_str
, 0, HASH_SIZE
);
328 make_object_static( &root_directory
->obj
);
329 make_object_static( &dir_driver
->obj
);
331 dir_global
= create_directory( NULL
, &dir_global_str
, 0, HASH_SIZE
);
332 /* use a larger hash table for this one since it can contain a lot of objects */
333 dir_basenamed
= create_directory( NULL
, &dir_basenamed_str
, 0, 37 );
336 create_named_pipe_device( dir_device
, &named_pipe_str
);
337 create_mailslot_device( dir_device
, &mailslot_str
);
340 link_dosdev
= create_symlink( root_directory
, &link_dosdev_str
, 0, &dir_global_str
);
341 link_global1
= create_symlink( dir_global
, &link_global_str
, 0, &dir_global_str
);
342 link_global2
= create_symlink( dir_basenamed
, &link_global_str
, 0, &dir_basenamed_str
);
343 link_local
= create_symlink( dir_basenamed
, &link_local_str
, 0, &dir_basenamed_str
);
344 link_pipe
= create_symlink( dir_global
, &link_pipe_str
, 0, &dir_named_pipe_str
);
345 link_mailslot
= create_symlink( dir_global
, &link_mailslot_str
, 0, &dir_mailslot_str
);
346 make_object_static( (struct object
*)link_dosdev
);
347 make_object_static( (struct object
*)link_global1
);
348 make_object_static( (struct object
*)link_global2
);
349 make_object_static( (struct object
*)link_local
);
350 make_object_static( (struct object
*)link_pipe
);
351 make_object_static( (struct object
*)link_mailslot
);
353 /* the symlinks or devices hold references so we can release these */
354 release_object( dir_global
);
355 release_object( dir_device
);
356 release_object( dir_basenamed
);
359 /* create a directory object */
360 DECL_HANDLER(create_directory
)
362 struct unicode_str name
;
363 struct directory
*dir
, *root
= NULL
;
366 get_req_unicode_str( &name
);
367 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
370 if ((dir
= create_directory( root
, &name
, req
->attributes
, HASH_SIZE
)))
372 reply
->handle
= alloc_handle( current
->process
, dir
, req
->access
, req
->attributes
);
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 )))
389 if ((dir
= open_object_dir( root
, &name
, req
->attributes
, &directory_ops
)))
391 reply
->handle
= alloc_handle( current
->process
, &dir
->obj
, req
->access
, req
->attributes
);
392 release_object( dir
);
395 if (root
) release_object( root
);