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 unsigned int directory_map_access( struct object
*obj
, unsigned int access
);
52 static struct object
*directory_lookup_name( struct object
*obj
, struct unicode_str
*name
,
54 static void directory_destroy( struct object
*obj
);
56 static const struct object_ops directory_ops
=
58 sizeof(struct directory
), /* size */
59 directory_dump
, /* dump */
60 no_add_queue
, /* add_queue */
61 NULL
, /* remove_queue */
64 no_signal
, /* signal */
65 no_get_fd
, /* get_fd */
66 directory_map_access
, /* map_access */
67 directory_lookup_name
, /* lookup_name */
68 no_open_file
, /* open_file */
69 no_close_handle
, /* close_handle */
70 directory_destroy
/* destroy */
73 static struct directory
*root_directory
;
76 static void directory_dump( struct object
*obj
, int verbose
)
78 assert( obj
->ops
== &directory_ops
);
80 fputs( "Directory ", stderr
);
81 dump_object_name( obj
);
82 fputc( '\n', stderr
);
85 static struct object
*directory_lookup_name( struct object
*obj
, struct unicode_str
*name
,
88 struct directory
*dir
= (struct directory
*)obj
;
90 struct unicode_str tmp
;
93 assert( obj
->ops
== &directory_ops
);
95 if (!(p
= memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )))
96 /* Last element in the path name */
99 tmp
.len
= (p
- name
->str
) * sizeof(WCHAR
);
102 if ((found
= find_object( dir
->entries
, &tmp
, attr
)))
104 /* Skip trailing \\ */
108 tmp
.len
+= sizeof(WCHAR
);
110 /* Move to the next element*/
112 name
->len
-= tmp
.len
;
118 if (tmp
.len
== 0) /* Double backslash */
119 set_error( STATUS_OBJECT_NAME_INVALID
);
120 else if (p
) /* Path still has backslashes */
121 set_error( STATUS_OBJECT_PATH_NOT_FOUND
);
126 static unsigned int directory_map_access( struct object
*obj
, unsigned int access
)
128 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
129 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
130 if (access
& GENERIC_EXECUTE
) access
|= FILE_GENERIC_EXECUTE
;
131 if (access
& GENERIC_ALL
) access
|= FILE_ALL_ACCESS
;
132 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
135 static void directory_destroy( struct object
*obj
)
137 struct directory
*dir
= (struct directory
*)obj
;
138 assert( obj
->ops
== &directory_ops
);
139 free( dir
->entries
);
142 static struct directory
*create_directory( struct directory
*root
, const struct unicode_str
*name
,
143 unsigned int attr
, unsigned int hash_size
)
145 struct directory
*dir
;
147 if ((dir
= create_named_object_dir( root
, name
, attr
, &directory_ops
)) &&
148 get_error() != STATUS_OBJECT_NAME_EXISTS
)
150 if (!(dir
->entries
= create_namespace( hash_size
)))
152 release_object( dir
);
159 struct directory
*get_directory_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
161 return (struct directory
*)get_handle_obj( process
, handle
, access
, &directory_ops
);
164 /******************************************************************************
165 * Find an object by its name in a given root object
168 * root [I] directory to start search from or NULL to start from \\
169 * name [I] object name to search for
170 * attr [I] OBJECT_ATTRIBUTES.Attributes
171 * name_left [O] [optional] leftover name if object is not found
174 * NULL: If params are invalid
175 * Found: If object with exact name is found returns that object
176 * (name_left->len == 0). Object's refcount is incremented
177 * Not found: The last matched parent. (name_left->len > 0)
178 * Parent's refcount is incremented.
180 struct object
*find_object_dir( struct directory
*root
, const struct unicode_str
*name
,
181 unsigned int attr
, struct unicode_str
*name_left
)
183 struct object
*obj
, *parent
;
184 struct unicode_str name_tmp
;
186 if (name
) name_tmp
= *name
;
187 else name_tmp
.len
= 0;
190 * - Either rootdir or name have to be specified
191 * - If root is specified path shouldn't start with backslash */
194 if (name_tmp
.len
&& name_tmp
.str
[0] == '\\')
196 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
199 parent
= grab_object( root
);
203 if (!name_tmp
.len
|| name_tmp
.str
[0] != '\\')
205 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
208 parent
= grab_object( &root_directory
->obj
);
209 /* skip leading backslash */
211 name_tmp
.len
-= sizeof(WCHAR
);
214 /* Special case for opening RootDirectory */
215 if (!name_tmp
.len
) goto done
;
217 while ((obj
= parent
->ops
->lookup_name( parent
, &name_tmp
, attr
)))
219 /* move to the next element */
220 release_object ( parent
);
225 release_object( parent
);
230 if (name_left
) *name_left
= name_tmp
;
234 /* create a named (if name is present) or unnamed object. */
235 void *create_named_object_dir( struct directory
*root
, const struct unicode_str
*name
,
236 unsigned int attributes
, const struct object_ops
*ops
)
238 struct object
*obj
, *new_obj
= NULL
;
239 struct unicode_str new_name
;
241 if (!name
|| !name
->len
) return alloc_object( ops
);
243 if (!(obj
= find_object_dir( root
, name
, attributes
, &new_name
))) return NULL
;
246 if (attributes
& OBJ_OPENIF
&& obj
->ops
== ops
)
247 set_error( STATUS_OBJECT_NAME_EXISTS
);
250 release_object( obj
);
252 if (attributes
& OBJ_OPENIF
)
253 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
255 set_error( STATUS_OBJECT_NAME_COLLISION
);
260 /* ATM we can't insert objects into anything else but directories */
261 if (obj
->ops
!= &directory_ops
)
262 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
265 struct directory
*dir
= (struct directory
*)obj
;
266 if ((new_obj
= create_object( dir
->entries
, ops
, &new_name
, &dir
->obj
)))
270 release_object( obj
);
274 /* open a new handle to an existing object */
275 void *open_object_dir( struct directory
*root
, const struct unicode_str
*name
,
276 unsigned int attr
, const struct object_ops
*ops
)
278 struct unicode_str name_left
;
281 if ((obj
= find_object_dir( root
, name
, attr
, &name_left
)))
283 if (name_left
.len
) /* not fully parsed */
284 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
285 else if (ops
&& obj
->ops
!= ops
)
286 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
290 release_object( obj
);
296 /* Global initialization */
298 void init_directories(void)
301 static const WCHAR dir_globalW
[] = {'\\','?','?'};
302 static const WCHAR dir_driverW
[] = {'D','r','i','v','e','r'};
303 static const WCHAR dir_deviceW
[] = {'D','e','v','i','c','e'};
304 static const WCHAR dir_basenamedW
[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
305 static const WCHAR dir_named_pipeW
[] = {'\\','D','e','v','i','c','e','\\','N','a','m','e','d','P','i','p','e'};
306 static const WCHAR dir_mailslotW
[] = {'\\','D','e','v','i','c','e','\\','M','a','i','l','S','l','o','t'};
307 static const struct unicode_str dir_global_str
= {dir_globalW
, sizeof(dir_globalW
)};
308 static const struct unicode_str dir_driver_str
= {dir_driverW
, sizeof(dir_driverW
)};
309 static const struct unicode_str dir_device_str
= {dir_deviceW
, sizeof(dir_deviceW
)};
310 static const struct unicode_str dir_basenamed_str
= {dir_basenamedW
, sizeof(dir_basenamedW
)};
311 static const struct unicode_str dir_named_pipe_str
= {dir_named_pipeW
, sizeof(dir_named_pipeW
)};
312 static const struct unicode_str dir_mailslot_str
= {dir_mailslotW
, sizeof(dir_mailslotW
)};
315 static const WCHAR link_dosdevW
[] = {'D','o','s','D','e','v','i','c','e','s'};
316 static const WCHAR link_globalW
[] = {'G','l','o','b','a','l'};
317 static const WCHAR link_localW
[] = {'L','o','c','a','l'};
318 static const WCHAR link_pipeW
[] = {'P','I','P','E'};
319 static const WCHAR link_mailslotW
[] = {'M','A','I','L','S','L','O','T'};
320 static const struct unicode_str link_dosdev_str
= {link_dosdevW
, sizeof(link_dosdevW
)};
321 static const struct unicode_str link_global_str
= {link_globalW
, sizeof(link_globalW
)};
322 static const struct unicode_str link_local_str
= {link_localW
, sizeof(link_localW
)};
323 static const struct unicode_str link_pipe_str
= {link_pipeW
, sizeof(link_pipeW
)};
324 static const struct unicode_str link_mailslot_str
= {link_mailslotW
, sizeof(link_mailslotW
)};
327 static const WCHAR named_pipeW
[] = {'N','a','m','e','d','P','i','p','e'};
328 static const WCHAR mailslotW
[] = {'M','a','i','l','S','l','o','t'};
329 static const struct unicode_str named_pipe_str
= {named_pipeW
, sizeof(named_pipeW
)};
330 static const struct unicode_str mailslot_str
= {mailslotW
, sizeof(mailslotW
)};
332 struct directory
*dir_driver
, *dir_device
, *dir_global
, *dir_basenamed
;
333 struct symlink
*link_dosdev
, *link_global1
, *link_global2
, *link_local
, *link_pipe
, *link_mailslot
;
335 root_directory
= create_directory( NULL
, NULL
, 0, HASH_SIZE
);
336 dir_driver
= create_directory( root_directory
, &dir_driver_str
, 0, HASH_SIZE
);
337 dir_device
= create_directory( root_directory
, &dir_device_str
, 0, HASH_SIZE
);
338 make_object_static( &root_directory
->obj
);
339 make_object_static( &dir_driver
->obj
);
341 dir_global
= create_directory( NULL
, &dir_global_str
, 0, HASH_SIZE
);
342 /* use a larger hash table for this one since it can contain a lot of objects */
343 dir_basenamed
= create_directory( NULL
, &dir_basenamed_str
, 0, 37 );
346 create_named_pipe_device( dir_device
, &named_pipe_str
);
347 create_mailslot_device( dir_device
, &mailslot_str
);
350 link_dosdev
= create_symlink( root_directory
, &link_dosdev_str
, 0, &dir_global_str
);
351 link_global1
= create_symlink( dir_global
, &link_global_str
, 0, &dir_global_str
);
352 link_global2
= create_symlink( dir_basenamed
, &link_global_str
, 0, &dir_basenamed_str
);
353 link_local
= create_symlink( dir_basenamed
, &link_local_str
, 0, &dir_basenamed_str
);
354 link_pipe
= create_symlink( dir_global
, &link_pipe_str
, 0, &dir_named_pipe_str
);
355 link_mailslot
= create_symlink( dir_global
, &link_mailslot_str
, 0, &dir_mailslot_str
);
356 make_object_static( (struct object
*)link_dosdev
);
357 make_object_static( (struct object
*)link_global1
);
358 make_object_static( (struct object
*)link_global2
);
359 make_object_static( (struct object
*)link_local
);
360 make_object_static( (struct object
*)link_pipe
);
361 make_object_static( (struct object
*)link_mailslot
);
363 /* the symlinks or devices hold references so we can release these */
364 release_object( dir_global
);
365 release_object( dir_device
);
366 release_object( dir_basenamed
);
369 /* create a directory object */
370 DECL_HANDLER(create_directory
)
372 struct unicode_str name
;
373 struct directory
*dir
, *root
= NULL
;
376 get_req_unicode_str( &name
);
377 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
380 if ((dir
= create_directory( root
, &name
, req
->attributes
, HASH_SIZE
)))
382 reply
->handle
= alloc_handle( current
->process
, dir
, req
->access
, req
->attributes
);
383 release_object( dir
);
386 if (root
) release_object( root
);
389 /* open a directory object */
390 DECL_HANDLER(open_directory
)
392 struct unicode_str name
;
393 struct directory
*dir
, *root
= NULL
;
395 get_req_unicode_str( &name
);
396 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
399 if ((dir
= open_object_dir( root
, &name
, req
->attributes
, &directory_ops
)))
401 reply
->handle
= alloc_handle( current
->process
, &dir
->obj
, req
->access
, req
->attributes
);
402 release_object( dir
);
405 if (root
) release_object( root
);