Copy the first 128 colors from the default colormap to Wine's private
[wine.git] / server / registry.c
blob961f441606a5775b36441640d07a62e9ab890e3d
1 /*
2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
5 */
7 /* To do:
8 * - behavior with deleted keys
9 * - values larger than request buffer
10 * - symbolic links
13 #include <assert.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <limits.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <pwd.h>
24 #include "object.h"
25 #include "handle.h"
26 #include "request.h"
27 #include "unicode.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winnt.h" /* registry definitions */
34 /* a registry key */
35 struct key
37 struct object obj; /* object header */
38 WCHAR *name; /* key name */
39 WCHAR *class; /* key class */
40 struct key *parent; /* parent key */
41 int last_subkey; /* last in use subkey */
42 int nb_subkeys; /* count of allocated subkeys */
43 struct key **subkeys; /* subkeys array */
44 int last_value; /* last in use value */
45 int nb_values; /* count of allocated values in array */
46 struct key_value *values; /* values array */
47 short flags; /* flags */
48 short level; /* saving level */
49 time_t modif; /* last modification time */
52 /* key flags */
53 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
54 #define KEY_DELETED 0x0002 /* key has been deleted */
55 #define KEY_ROOT 0x0004 /* key is a root key */
57 /* a key value */
58 struct key_value
60 WCHAR *name; /* value name */
61 int type; /* value type */
62 size_t len; /* value data length in bytes */
63 void *data; /* pointer to value data */
66 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
67 #define MIN_VALUES 8 /* min. number of allocated values per key */
70 /* the special root keys */
71 #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
72 #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
73 #define NB_SPECIAL_ROOT_KEYS (HKEY_SPECIAL_ROOT_LAST - HKEY_SPECIAL_ROOT_FIRST + 1)
74 #define IS_SPECIAL_ROOT_HKEY(h) (((unsigned int)(h) >= HKEY_SPECIAL_ROOT_FIRST) && \
75 ((unsigned int)(h) <= HKEY_SPECIAL_ROOT_LAST))
77 static struct key *special_root_keys[NB_SPECIAL_ROOT_KEYS];
79 /* the real root key */
80 static struct key *root_key;
82 /* the special root key names */
83 static const char * const special_root_names[NB_SPECIAL_ROOT_KEYS] =
85 "Machine\\Software\\Classes", /* HKEY_CLASSES_ROOT */
86 "User\\", /* we append the user name dynamically */ /* HKEY_CURRENT_USER */
87 "Machine", /* HKEY_LOCAL_MACHINE */
88 "User", /* HKEY_USERS */
89 "PerfData", /* HKEY_PERFORMANCE_DATA */
90 "Machine\\System\\CurrentControlSet\\HardwareProfiles\\Current", /* HKEY_CURRENT_CONFIG */
91 "DynData" /* HKEY_DYN_DATA */
95 /* keys saving level */
96 /* current_level is the level that is put into all newly created or modified keys */
97 /* saving_level is the minimum level that a key needs in order to get saved */
98 static int current_level;
99 static int saving_level;
101 static struct timeval next_save_time; /* absolute time of next periodic save */
102 static int save_period; /* delay between periodic saves (ms) */
103 static struct timeout_user *save_timeout_user; /* saving timer */
105 /* information about where to save a registry branch */
106 struct save_branch_info
108 struct key *key;
109 char *path;
112 #define MAX_SAVE_BRANCH_INFO 8
113 static int save_branch_count;
114 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
117 /* information about a file being loaded */
118 struct file_load_info
120 FILE *file; /* input file */
121 char *buffer; /* line buffer */
122 int len; /* buffer length */
123 int line; /* current input line */
124 char *tmp; /* temp buffer to use while parsing input */
125 int tmplen; /* length of temp buffer */
129 static void key_dump( struct object *obj, int verbose );
130 static void key_destroy( struct object *obj );
132 static const struct object_ops key_ops =
134 sizeof(struct key), /* size */
135 key_dump, /* dump */
136 no_add_queue, /* add_queue */
137 NULL, /* remove_queue */
138 NULL, /* signaled */
139 NULL, /* satisfied */
140 NULL, /* get_poll_events */
141 NULL, /* poll_event */
142 no_get_fd, /* get_fd */
143 no_flush, /* flush */
144 no_get_file_info, /* get_file_info */
145 key_destroy /* destroy */
150 * The registry text file format v2 used by this code is similar to the one
151 * used by REGEDIT import/export functionality, with the following differences:
152 * - strings and key names can contain \x escapes for Unicode
153 * - key names use escapes too in order to support Unicode
154 * - the modification time optionally follows the key name
155 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
158 static inline char to_hex( char ch )
160 if (isdigit(ch)) return ch - '0';
161 return tolower(ch) - 'a' + 10;
164 /* dump the full path of a key */
165 static void dump_path( struct key *key, struct key *base, FILE *f )
167 if (key->parent && key->parent != base)
169 dump_path( key->parent, base, f );
170 fprintf( f, "\\\\" );
172 dump_strW( key->name, strlenW(key->name), f, "[]" );
175 /* dump a value to a text file */
176 static void dump_value( struct key_value *value, FILE *f )
178 int i, count;
180 if (value->name[0])
182 fputc( '\"', f );
183 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
184 count += fprintf( f, "\"=" );
186 else count = fprintf( f, "@=" );
188 switch(value->type)
190 case REG_SZ:
191 case REG_EXPAND_SZ:
192 case REG_MULTI_SZ:
193 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
194 fputc( '\"', f );
195 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
196 fputc( '\"', f );
197 break;
198 case REG_DWORD:
199 if (value->len == sizeof(DWORD))
201 DWORD dw;
202 memcpy( &dw, value->data, sizeof(DWORD) );
203 fprintf( f, "dword:%08lx", dw );
204 break;
206 /* else fall through */
207 default:
208 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
209 else count += fprintf( f, "hex(%x):", value->type );
210 for (i = 0; i < value->len; i++)
212 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
213 if (i < value->len-1)
215 fputc( ',', f );
216 if (++count > 76)
218 fprintf( f, "\\\n " );
219 count = 2;
223 break;
225 fputc( '\n', f );
228 /* save a registry and all its subkeys to a text file */
229 static void save_subkeys( struct key *key, struct key *base, FILE *f )
231 int i;
233 if (key->flags & KEY_VOLATILE) return;
234 /* save key if it has the proper level, and has either some values or no subkeys */
235 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
236 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
238 fprintf( f, "\n[" );
239 if (key != base) dump_path( key, base, f );
240 fprintf( f, "] %ld\n", key->modif );
241 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
243 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
246 static void dump_operation( struct key *key, struct key_value *value, const char *op )
248 fprintf( stderr, "%s key ", op );
249 if (key) dump_path( key, NULL, stderr );
250 else fprintf( stderr, "ERROR" );
251 if (value)
253 fprintf( stderr, " value ");
254 dump_value( value, stderr );
256 else fprintf( stderr, "\n" );
259 static void key_dump( struct object *obj, int verbose )
261 struct key *key = (struct key *)obj;
262 assert( obj->ops == &key_ops );
263 fprintf( stderr, "Key flags=%x ", key->flags );
264 dump_path( key, NULL, stderr );
265 fprintf( stderr, "\n" );
268 static void key_destroy( struct object *obj )
270 int i;
271 struct key *key = (struct key *)obj;
272 assert( obj->ops == &key_ops );
274 if (key->name) free( key->name );
275 if (key->class) free( key->class );
276 for (i = 0; i <= key->last_value; i++)
278 free( key->values[i].name );
279 if (key->values[i].data) free( key->values[i].data );
281 for (i = 0; i <= key->last_subkey; i++)
283 key->subkeys[i]->parent = NULL;
284 release_object( key->subkeys[i] );
288 /* duplicate a key path from the request buffer */
289 /* returns a pointer to a static buffer, so only useable once per request */
290 static WCHAR *copy_path( const WCHAR *path, size_t len, int skip_root )
292 static WCHAR buffer[MAX_PATH+1];
293 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\',0 };
295 if (len > sizeof(buffer)-sizeof(buffer[0]))
297 set_error( STATUS_BUFFER_OVERFLOW );
298 return NULL;
300 memcpy( buffer, path, len );
301 buffer[len / sizeof(WCHAR)] = 0;
302 if (skip_root && !strncmpiW( buffer, root_name, 10 )) return buffer + 10;
303 return buffer;
306 /* copy a path from the request buffer, in cases where the length is stored in front of the path */
307 static WCHAR *copy_req_path( void *req, size_t *len, int skip_root )
309 const WCHAR *name_ptr = get_req_data(req);
310 if ((*len = sizeof(WCHAR) + *name_ptr++) > get_req_data_size(req))
312 fatal_protocol_error( current, "copy_req_path: invalid length %d/%d\n",
313 *len, get_req_data_size(req) );
314 return NULL;
316 return copy_path( name_ptr, *len - sizeof(WCHAR), skip_root );
319 /* return the next token in a given path */
320 /* returns a pointer to a static buffer, so only useable once per request */
321 static WCHAR *get_path_token( WCHAR *initpath )
323 static WCHAR *path;
324 WCHAR *ret;
326 if (initpath)
328 /* path cannot start with a backslash */
329 if (*initpath == '\\')
331 set_error( STATUS_OBJECT_PATH_INVALID );
332 return NULL;
334 path = initpath;
336 else while (*path == '\\') path++;
338 ret = path;
339 while (*path && *path != '\\') path++;
340 if (*path) *path++ = 0;
341 return ret;
344 /* duplicate a Unicode string from the request buffer */
345 static WCHAR *req_strdupW( const void *req, const WCHAR *str, size_t len )
347 WCHAR *name;
348 if ((name = mem_alloc( len + sizeof(WCHAR) )) != NULL)
350 memcpy( name, str, len );
351 name[len / sizeof(WCHAR)] = 0;
353 return name;
356 /* allocate a key object */
357 static struct key *alloc_key( const WCHAR *name, time_t modif )
359 struct key *key;
360 if ((key = (struct key *)alloc_object( &key_ops, -1 )))
362 key->class = NULL;
363 key->flags = 0;
364 key->last_subkey = -1;
365 key->nb_subkeys = 0;
366 key->subkeys = NULL;
367 key->nb_values = 0;
368 key->last_value = -1;
369 key->values = NULL;
370 key->level = current_level;
371 key->modif = modif;
372 key->parent = NULL;
373 if (!(key->name = strdupW( name )))
375 release_object( key );
376 key = NULL;
379 return key;
382 /* update key modification time */
383 static void touch_key( struct key *key )
385 key->modif = time(NULL);
386 key->level = max( key->level, current_level );
389 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
390 static int grow_subkeys( struct key *key )
392 struct key **new_subkeys;
393 int nb_subkeys;
395 if (key->nb_subkeys)
397 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
398 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
400 set_error( STATUS_NO_MEMORY );
401 return 0;
404 else
406 nb_subkeys = MIN_VALUES;
407 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
409 key->subkeys = new_subkeys;
410 key->nb_subkeys = nb_subkeys;
411 return 1;
414 /* allocate a subkey for a given key, and return its index */
415 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
417 struct key *key;
418 int i;
420 if (parent->last_subkey + 1 == parent->nb_subkeys)
422 /* need to grow the array */
423 if (!grow_subkeys( parent )) return NULL;
425 if ((key = alloc_key( name, modif )) != NULL)
427 key->parent = parent;
428 for (i = ++parent->last_subkey; i > index; i--)
429 parent->subkeys[i] = parent->subkeys[i-1];
430 parent->subkeys[index] = key;
432 return key;
435 /* free a subkey of a given key */
436 static void free_subkey( struct key *parent, int index )
438 struct key *key;
439 int i, nb_subkeys;
441 assert( index >= 0 );
442 assert( index <= parent->last_subkey );
444 key = parent->subkeys[index];
445 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
446 parent->last_subkey--;
447 key->flags |= KEY_DELETED;
448 key->parent = NULL;
449 release_object( key );
451 /* try to shrink the array */
452 nb_subkeys = key->nb_subkeys;
453 if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2)
455 struct key **new_subkeys;
456 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
457 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
458 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
459 key->subkeys = new_subkeys;
460 key->nb_subkeys = nb_subkeys;
464 /* find the named child of a given key and return its index */
465 static struct key *find_subkey( struct key *key, const WCHAR *name, int *index )
467 int i, min, max, res;
469 min = 0;
470 max = key->last_subkey;
471 while (min <= max)
473 i = (min + max) / 2;
474 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
476 *index = i;
477 return key->subkeys[i];
479 if (res > 0) max = i - 1;
480 else min = i + 1;
482 *index = min; /* this is where we should insert it */
483 return NULL;
486 /* open a subkey */
487 /* warning: the key name must be writeable (use copy_path) */
488 static struct key *open_key( struct key *key, WCHAR *name )
490 int index;
491 WCHAR *path;
493 if (!(path = get_path_token( name ))) return NULL;
494 while (*path)
496 if (!(key = find_subkey( key, path, &index )))
498 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
499 break;
501 path = get_path_token( NULL );
504 if (debug_level > 1) dump_operation( key, NULL, "Open" );
505 if (key) grab_object( key );
506 return key;
509 /* create a subkey */
510 /* warning: the key name must be writeable (use copy_path) */
511 static struct key *create_key( struct key *key, WCHAR *name, WCHAR *class,
512 unsigned int options, time_t modif, int *created )
514 struct key *base;
515 int base_idx, index, flags = 0;
516 WCHAR *path;
518 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
520 set_error( STATUS_KEY_DELETED );
521 return NULL;
523 if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE;
524 else if (key->flags & KEY_VOLATILE)
526 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
527 return NULL;
529 if (!modif) modif = time(NULL);
531 if (!(path = get_path_token( name ))) return NULL;
532 *created = 0;
533 while (*path)
535 struct key *subkey;
536 if (!(subkey = find_subkey( key, path, &index ))) break;
537 key = subkey;
538 path = get_path_token( NULL );
541 /* create the remaining part */
543 if (!*path) goto done;
544 *created = 1;
545 base = key;
546 base_idx = index;
547 key = alloc_subkey( key, path, index, modif );
548 while (key)
550 key->flags |= flags;
551 path = get_path_token( NULL );
552 if (!*path) goto done;
553 /* we know the index is always 0 in a new key */
554 key = alloc_subkey( key, path, 0, modif );
556 if (base_idx != -1) free_subkey( base, base_idx );
557 return NULL;
559 done:
560 if (debug_level > 1) dump_operation( key, NULL, "Create" );
561 if (class) key->class = strdupW(class);
562 grab_object( key );
563 return key;
566 /* query information about a key or a subkey */
567 static size_t enum_key( struct key *key, int index, struct enum_key_request *req )
569 int i;
570 size_t len, namelen, classlen;
571 int max_subkey = 0, max_class = 0;
572 int max_value = 0, max_data = 0;
573 WCHAR *data = get_req_data(req);
575 if (index != -1) /* -1 means use the specified key directly */
577 if ((index < 0) || (index > key->last_subkey))
579 set_error( STATUS_NO_MORE_ENTRIES );
580 return 0;
582 key = key->subkeys[index];
585 if (req->full)
587 for (i = 0; i <= key->last_subkey; i++)
589 struct key *subkey = key->subkeys[i];
590 len = strlenW( subkey->name );
591 if (len > max_subkey) max_subkey = len;
592 if (!subkey->class) continue;
593 len = strlenW( subkey->class );
594 if (len > max_class) max_class = len;
596 for (i = 0; i <= key->last_value; i++)
598 len = strlenW( key->values[i].name );
599 if (len > max_value) max_value = len;
600 len = key->values[i].len;
601 if (len > max_data) max_data = len;
603 req->max_subkey = max_subkey;
604 req->max_class = max_class;
605 req->max_value = max_value;
606 req->max_data = max_data;
608 else
610 req->max_subkey = 0;
611 req->max_class = 0;
612 req->max_value = 0;
613 req->max_data = 0;
615 req->subkeys = key->last_subkey + 1;
616 req->values = key->last_value + 1;
617 req->modif = key->modif;
619 namelen = strlenW(key->name) * sizeof(WCHAR);
620 classlen = key->class ? strlenW(key->class) * sizeof(WCHAR) : 0;
622 len = namelen + classlen + sizeof(WCHAR);
623 if (len > get_req_data_size(req))
625 len = get_req_data_size(req);
626 if (len < sizeof(WCHAR)) return 0;
629 *data++ = namelen;
630 len -= sizeof(WCHAR);
631 if (len > namelen)
633 memcpy( data, key->name, namelen );
634 memcpy( (char *)data + namelen, key->class, min(classlen,len-namelen) );
636 else memcpy( data, key->name, len );
638 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
639 return len + sizeof(WCHAR);
642 /* delete a key and its values */
643 static void delete_key( struct key *key )
645 int index;
646 struct key *parent;
648 /* must find parent and index */
649 if (key->flags & KEY_ROOT)
651 set_error( STATUS_ACCESS_DENIED );
652 return;
654 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
656 set_error( STATUS_KEY_DELETED );
657 return;
659 for (index = 0; index <= parent->last_subkey; index++)
660 if (parent->subkeys[index] == key) break;
661 assert( index <= parent->last_subkey );
663 /* we can only delete a key that has no subkeys (FIXME) */
664 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
666 set_error( STATUS_ACCESS_DENIED );
667 return;
669 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
670 free_subkey( parent, index );
671 touch_key( parent );
674 /* try to grow the array of values; return 1 if OK, 0 on error */
675 static int grow_values( struct key *key )
677 struct key_value *new_val;
678 int nb_values;
680 if (key->nb_values)
682 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
683 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
685 set_error( STATUS_NO_MEMORY );
686 return 0;
689 else
691 nb_values = MIN_VALUES;
692 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
694 key->values = new_val;
695 key->nb_values = nb_values;
696 return 1;
699 /* find the named value of a given key and return its index in the array */
700 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
702 int i, min, max, res;
704 min = 0;
705 max = key->last_value;
706 while (min <= max)
708 i = (min + max) / 2;
709 if (!(res = strcmpiW( key->values[i].name, name )))
711 *index = i;
712 return &key->values[i];
714 if (res > 0) max = i - 1;
715 else min = i + 1;
717 *index = min; /* this is where we should insert it */
718 return NULL;
721 /* insert a new value or return a pointer to an existing one */
722 static struct key_value *insert_value( struct key *key, const WCHAR *name )
724 struct key_value *value;
725 WCHAR *new_name;
726 int i, index;
728 if (!(value = find_value( key, name, &index )))
730 /* not found, add it */
731 if (key->last_value + 1 == key->nb_values)
733 if (!grow_values( key )) return NULL;
735 if (!(new_name = strdupW(name))) return NULL;
736 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
737 value = &key->values[index];
738 value->name = new_name;
739 value->len = 0;
740 value->data = NULL;
742 return value;
745 /* set a key value */
746 static void set_value( struct key *key, WCHAR *name, int type, unsigned int total_len,
747 unsigned int offset, unsigned int data_len, const void *data )
749 struct key_value *value;
750 void *ptr = NULL;
752 if (data_len + offset > total_len)
754 set_error( STATUS_INVALID_PARAMETER );
755 return;
758 if (offset) /* adding data to an existing value */
760 int index;
761 if (!(value = find_value( key, name, &index )))
763 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
764 return;
766 if (value->len != total_len)
768 set_error( STATUS_INVALID_PARAMETER );
769 return;
771 memcpy( (char *)value->data + offset, data, data_len );
772 if (debug_level > 1) dump_operation( key, value, "Set" );
773 return;
776 /* first copy the data */
777 if (total_len)
779 if (!(ptr = mem_alloc( total_len ))) return;
780 memcpy( ptr, data, data_len );
781 if (data_len < total_len) memset( (char *)ptr + data_len, 0, total_len - data_len );
784 if (!(value = insert_value( key, name )))
786 if (ptr) free( ptr );
787 return;
789 if (value->data) free( value->data ); /* already existing, free previous data */
790 value->type = type;
791 value->len = total_len;
792 value->data = ptr;
793 touch_key( key );
794 if (debug_level > 1) dump_operation( key, value, "Set" );
797 /* get a key value */
798 static size_t get_value( struct key *key, const WCHAR *name, unsigned int offset,
799 unsigned int maxlen, int *type, int *len, void *data )
801 struct key_value *value;
802 int index;
803 size_t ret = 0;
805 if ((value = find_value( key, name, &index )))
807 *type = value->type;
808 *len = value->len;
809 if (value->data && offset < value->len)
811 if (maxlen > value->len - offset) maxlen = value->len - offset;
812 memcpy( data, (char *)value->data + offset, maxlen );
813 ret = maxlen;
815 if (debug_level > 1) dump_operation( key, value, "Get" );
817 else
819 *type = -1;
820 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
822 return ret;
825 /* enumerate a key value */
826 static size_t enum_value( struct key *key, int i, unsigned int offset,
827 unsigned int maxlen, int *type, int *len, void *data )
829 struct key_value *value;
830 size_t ret = 0;
832 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
833 else
835 WCHAR *name_ptr = data;
836 value = &key->values[i];
837 *type = value->type;
838 *len = value->len;
840 if (maxlen >= sizeof(WCHAR))
842 size_t name_len = 0;
844 /* copy the name only the first time (offset==0),
845 * otherwise store an empty name in the buffer
847 maxlen -= sizeof(WCHAR);
848 ret += sizeof(WCHAR);
849 if (!offset)
851 name_len = strlenW( value->name ) * sizeof(WCHAR);
852 if (name_len > maxlen) name_len = maxlen;
854 *name_ptr++ = name_len;
855 memcpy( name_ptr, value->name, name_len );
856 maxlen -= name_len;
857 ret += name_len;
858 data = (char *)name_ptr + name_len;
860 if (value->data && offset < value->len)
862 if (maxlen > value->len - offset) maxlen = value->len - offset;
863 memcpy( data, (char *)value->data + offset, maxlen );
864 ret += maxlen;
867 if (debug_level > 1) dump_operation( key, value, "Enum" );
869 return ret;
872 /* delete a value */
873 static void delete_value( struct key *key, const WCHAR *name )
875 struct key_value *value;
876 int i, index, nb_values;
878 if (!(value = find_value( key, name, &index )))
880 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
881 return;
883 if (debug_level > 1) dump_operation( key, value, "Delete" );
884 free( value->name );
885 if (value->data) free( value->data );
886 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
887 key->last_value--;
888 touch_key( key );
890 /* try to shrink the array */
891 nb_values = key->nb_values;
892 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
894 struct key_value *new_val;
895 nb_values -= nb_values / 3; /* shrink by 33% */
896 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
897 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
898 key->values = new_val;
899 key->nb_values = nb_values;
903 static struct key *create_root_key( handle_t hkey )
905 WCHAR keyname[80];
906 int i, dummy;
907 struct key *key;
908 const char *p;
910 p = special_root_names[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST];
911 i = 0;
912 while (*p) keyname[i++] = *p++;
914 if (hkey == (handle_t)HKEY_CURRENT_USER) /* this one is special */
916 /* get the current user name */
917 char buffer[10];
918 struct passwd *pwd = getpwuid( getuid() );
920 if (pwd) p = pwd->pw_name;
921 else
923 sprintf( buffer, "%ld", (long) getuid() );
924 p = buffer;
926 while (*p && i < sizeof(keyname)/sizeof(WCHAR)-1) keyname[i++] = *p++;
928 keyname[i++] = 0;
930 if ((key = create_key( root_key, keyname, NULL, 0, time(NULL), &dummy )))
932 special_root_keys[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST] = key;
933 key->flags |= KEY_ROOT;
935 return key;
938 /* get the registry key corresponding to an hkey handle */
939 static struct key *get_hkey_obj( handle_t hkey, unsigned int access )
941 struct key *key;
943 if (!hkey) return (struct key *)grab_object( root_key );
944 if (IS_SPECIAL_ROOT_HKEY(hkey))
946 if (!(key = special_root_keys[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST]))
947 key = create_root_key( hkey );
948 else
949 grab_object( key );
951 else
952 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
953 return key;
956 /* read a line from the input file */
957 static int read_next_line( struct file_load_info *info )
959 char *newbuf;
960 int newlen, pos = 0;
962 info->line++;
963 for (;;)
965 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
966 return (pos != 0); /* EOF */
967 pos = strlen(info->buffer);
968 if (info->buffer[pos-1] == '\n')
970 /* got a full line */
971 info->buffer[--pos] = 0;
972 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
973 return 1;
975 if (pos < info->len - 1) return 1; /* EOF but something was read */
977 /* need to enlarge the buffer */
978 newlen = info->len + info->len / 2;
979 if (!(newbuf = realloc( info->buffer, newlen )))
981 set_error( STATUS_NO_MEMORY );
982 return -1;
984 info->buffer = newbuf;
985 info->len = newlen;
989 /* make sure the temp buffer holds enough space */
990 static int get_file_tmp_space( struct file_load_info *info, int size )
992 char *tmp;
993 if (info->tmplen >= size) return 1;
994 if (!(tmp = realloc( info->tmp, size )))
996 set_error( STATUS_NO_MEMORY );
997 return 0;
999 info->tmp = tmp;
1000 info->tmplen = size;
1001 return 1;
1004 /* report an error while loading an input file */
1005 static void file_read_error( const char *err, struct file_load_info *info )
1007 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
1010 /* parse an escaped string back into Unicode */
1011 /* return the number of chars read from the input, or -1 on output overflow */
1012 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
1014 int count = sizeof(WCHAR); /* for terminating null */
1015 const char *p = src;
1016 while (*p && *p != endchar)
1018 if (*p != '\\') *dest = (WCHAR)*p++;
1019 else
1021 p++;
1022 switch(*p)
1024 case 'a': *dest = '\a'; p++; break;
1025 case 'b': *dest = '\b'; p++; break;
1026 case 'e': *dest = '\e'; p++; break;
1027 case 'f': *dest = '\f'; p++; break;
1028 case 'n': *dest = '\n'; p++; break;
1029 case 'r': *dest = '\r'; p++; break;
1030 case 't': *dest = '\t'; p++; break;
1031 case 'v': *dest = '\v'; p++; break;
1032 case 'x': /* hex escape */
1033 p++;
1034 if (!isxdigit(*p)) *dest = 'x';
1035 else
1037 *dest = to_hex(*p++);
1038 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1039 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1040 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1042 break;
1043 case '0':
1044 case '1':
1045 case '2':
1046 case '3':
1047 case '4':
1048 case '5':
1049 case '6':
1050 case '7': /* octal escape */
1051 *dest = *p++ - '0';
1052 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1053 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1054 break;
1055 default:
1056 *dest = (WCHAR)*p++;
1057 break;
1060 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1061 dest++;
1063 *dest = 0;
1064 if (!*p) return -1; /* delimiter not found */
1065 *len = count;
1066 return p + 1 - src;
1069 /* convert a data type tag to a value type */
1070 static int get_data_type( const char *buffer, int *type, int *parse_type )
1072 struct data_type { const char *tag; int len; int type; int parse_type; };
1074 static const struct data_type data_types[] =
1075 { /* actual type */ /* type to assume for parsing */
1076 { "\"", 1, REG_SZ, REG_SZ },
1077 { "str:\"", 5, REG_SZ, REG_SZ },
1078 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1079 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1080 { "hex:", 4, REG_BINARY, REG_BINARY },
1081 { "dword:", 6, REG_DWORD, REG_DWORD },
1082 { "hex(", 4, -1, REG_BINARY },
1083 { NULL, 0, 0, 0 }
1086 const struct data_type *ptr;
1087 char *end;
1089 for (ptr = data_types; ptr->tag; ptr++)
1091 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1092 *parse_type = ptr->parse_type;
1093 if ((*type = ptr->type) != -1) return ptr->len;
1094 /* "hex(xx):" is special */
1095 *type = (int)strtoul( buffer + 4, &end, 16 );
1096 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1097 return end + 2 - buffer;
1099 return 0;
1102 /* load and create a key from the input file */
1103 static struct key *load_key( struct key *base, const char *buffer, unsigned int options,
1104 int prefix_len, struct file_load_info *info )
1106 WCHAR *p, *name;
1107 int res, len, modif;
1109 len = strlen(buffer) * sizeof(WCHAR);
1110 if (!get_file_tmp_space( info, len )) return NULL;
1112 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1114 file_read_error( "Malformed key", info );
1115 return NULL;
1117 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = time(NULL);
1119 p = (WCHAR *)info->tmp;
1120 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1122 if (!*p)
1124 if (prefix_len > 1)
1126 file_read_error( "Malformed key", info );
1127 return NULL;
1129 /* empty key name, return base key */
1130 return (struct key *)grab_object( base );
1132 if (!(name = copy_path( p, len - ((char *)p - info->tmp), 0 )))
1134 file_read_error( "Key is too long", info );
1135 return NULL;
1137 return create_key( base, name, NULL, options, modif, &res );
1140 /* parse a comma-separated list of hex digits */
1141 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1143 const char *p = buffer;
1144 int count = 0;
1145 while (isxdigit(*p))
1147 int val;
1148 char buf[3];
1149 memcpy( buf, p, 2 );
1150 buf[2] = 0;
1151 sscanf( buf, "%x", &val );
1152 if (count++ >= *len) return -1; /* dest buffer overflow */
1153 *dest++ = (unsigned char )val;
1154 p += 2;
1155 if (*p == ',') p++;
1157 *len = count;
1158 return p - buffer;
1161 /* parse a value name and create the corresponding value */
1162 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1163 struct file_load_info *info )
1165 int maxlen = strlen(buffer) * sizeof(WCHAR);
1166 if (!get_file_tmp_space( info, maxlen )) return NULL;
1167 if (buffer[0] == '@')
1169 info->tmp[0] = info->tmp[1] = 0;
1170 *len = 1;
1172 else
1174 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1175 (*len)++; /* for initial quote */
1177 while (isspace(buffer[*len])) (*len)++;
1178 if (buffer[*len] != '=') goto error;
1179 (*len)++;
1180 while (isspace(buffer[*len])) (*len)++;
1181 return insert_value( key, (WCHAR *)info->tmp );
1183 error:
1184 file_read_error( "Malformed value name", info );
1185 return NULL;
1188 /* load a value from the input file */
1189 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1191 DWORD dw;
1192 void *ptr, *newptr;
1193 int maxlen, len, res;
1194 int type, parse_type;
1195 struct key_value *value;
1197 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1198 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1199 buffer += len + res;
1201 switch(parse_type)
1203 case REG_SZ:
1204 len = strlen(buffer) * sizeof(WCHAR);
1205 if (!get_file_tmp_space( info, len )) return 0;
1206 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1207 ptr = info->tmp;
1208 break;
1209 case REG_DWORD:
1210 dw = strtoul( buffer, NULL, 16 );
1211 ptr = &dw;
1212 len = sizeof(dw);
1213 break;
1214 case REG_BINARY: /* hex digits */
1215 len = 0;
1216 for (;;)
1218 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1219 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1220 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1221 len += maxlen;
1222 buffer += res;
1223 while (isspace(*buffer)) buffer++;
1224 if (!*buffer) break;
1225 if (*buffer != '\\') goto error;
1226 if (read_next_line( info) != 1) goto error;
1227 buffer = info->buffer;
1228 while (isspace(*buffer)) buffer++;
1230 ptr = info->tmp;
1231 break;
1232 default:
1233 assert(0);
1234 ptr = NULL; /* keep compiler quiet */
1235 break;
1238 if (!len) newptr = NULL;
1239 else if (!(newptr = memdup( ptr, len ))) return 0;
1241 if (value->data) free( value->data );
1242 value->data = newptr;
1243 value->len = len;
1244 value->type = type;
1245 /* update the key level but not the modification time */
1246 key->level = max( key->level, current_level );
1247 return 1;
1249 error:
1250 file_read_error( "Malformed value", info );
1251 return 0;
1254 /* return the length (in path elements) of name that is part of the key name */
1255 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1256 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1258 WCHAR *p;
1259 int res;
1260 int len = strlen(name) * sizeof(WCHAR);
1261 if (!get_file_tmp_space( info, len )) return 0;
1263 if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
1265 file_read_error( "Malformed key", info );
1266 return 0;
1268 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
1269 *p = 0;
1270 for (res = 1; key != root_key; res++)
1272 if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
1273 key = key->parent;
1275 if (key == root_key) res = 0; /* no matching name */
1276 return res;
1279 /* load all the keys from the input file */
1280 static void load_keys( struct key *key, FILE *f )
1282 struct key *subkey = NULL;
1283 struct file_load_info info;
1284 char *p;
1285 unsigned int options = 0;
1286 int prefix_len = -1; /* number of key name prefixes to skip */
1288 if (key->flags & KEY_VOLATILE) options |= REG_OPTION_VOLATILE;
1290 info.file = f;
1291 info.len = 4;
1292 info.tmplen = 4;
1293 info.line = 0;
1294 if (!(info.buffer = mem_alloc( info.len ))) return;
1295 if (!(info.tmp = mem_alloc( info.tmplen )))
1297 free( info.buffer );
1298 return;
1301 if ((read_next_line( &info ) != 1) ||
1302 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1304 set_error( STATUS_NOT_REGISTRY_FILE );
1305 goto done;
1308 while (read_next_line( &info ) == 1)
1310 p = info.buffer;
1311 while (*p && isspace(*p)) p++;
1312 switch(*p)
1314 case '[': /* new key */
1315 if (subkey) release_object( subkey );
1316 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1317 if (!(subkey = load_key( key, p + 1, options, prefix_len, &info )))
1318 file_read_error( "Error creating key", &info );
1319 break;
1320 case '@': /* default value */
1321 case '\"': /* value */
1322 if (subkey) load_value( subkey, p, &info );
1323 else file_read_error( "Value without key", &info );
1324 break;
1325 case '#': /* comment */
1326 case ';': /* comment */
1327 case 0: /* empty line */
1328 break;
1329 default:
1330 file_read_error( "Unrecognized input", &info );
1331 break;
1335 done:
1336 if (subkey) release_object( subkey );
1337 free( info.buffer );
1338 free( info.tmp );
1341 /* load a part of the registry from a file */
1342 static void load_registry( struct key *key, handle_t handle )
1344 struct object *obj;
1345 int fd;
1347 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return;
1348 fd = dup(obj->ops->get_fd( obj ));
1349 release_object( obj );
1350 if (fd != -1)
1352 FILE *f = fdopen( fd, "r" );
1353 if (f)
1355 load_keys( key, f );
1356 fclose( f );
1358 else file_set_error();
1362 /* registry initialisation */
1363 void init_registry(void)
1365 static const WCHAR root_name[] = { 0 };
1366 static const WCHAR config_name[] =
1367 { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
1368 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 };
1370 char *filename;
1371 const char *config;
1372 FILE *f;
1374 /* create the root key */
1375 root_key = alloc_key( root_name, time(NULL) );
1376 assert( root_key );
1377 root_key->flags |= KEY_ROOT;
1379 /* load the config file */
1380 config = get_config_dir();
1381 if (!(filename = malloc( strlen(config) + 8 ))) fatal_error( "out of memory\n" );
1382 strcpy( filename, config );
1383 strcat( filename, "/config" );
1384 if ((f = fopen( filename, "r" )))
1386 struct key *key;
1387 int dummy;
1389 /* create the config key */
1390 if (!(key = create_key( root_key, copy_path( config_name, sizeof(config_name), 0 ),
1391 NULL, 0, time(NULL), &dummy )))
1392 fatal_error( "could not create config key\n" );
1393 key->flags |= KEY_VOLATILE;
1395 load_keys( key, f );
1396 fclose( f );
1397 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1398 fatal_error( "%s is not a valid registry file\n", filename );
1399 if (get_error())
1400 fatal_error( "loading %s failed with error %x\n", filename, get_error() );
1402 release_object( key );
1404 free( filename );
1407 /* update the level of the parents of a key (only needed for the old format) */
1408 static int update_level( struct key *key )
1410 int i;
1411 int max = key->level;
1412 for (i = 0; i <= key->last_subkey; i++)
1414 int sub = update_level( key->subkeys[i] );
1415 if (sub > max) max = sub;
1417 key->level = max;
1418 return max;
1421 /* save a registry branch to a file */
1422 static void save_all_subkeys( struct key *key, FILE *f )
1424 fprintf( f, "WINE REGISTRY Version 2\n" );
1425 fprintf( f, ";; All keys relative to " );
1426 dump_path( key, NULL, f );
1427 fprintf( f, "\n" );
1428 save_subkeys( key, key, f );
1431 /* save a registry branch to a file handle */
1432 static void save_registry( struct key *key, handle_t handle )
1434 struct object *obj;
1435 int fd;
1437 if (key->flags & KEY_DELETED)
1439 set_error( STATUS_KEY_DELETED );
1440 return;
1442 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return;
1443 fd = dup(obj->ops->get_fd( obj ));
1444 release_object( obj );
1445 if (fd != -1)
1447 FILE *f = fdopen( fd, "w" );
1448 if (f)
1450 save_all_subkeys( key, f );
1451 if (fclose( f )) file_set_error();
1453 else
1455 file_set_error();
1456 close( fd );
1461 /* register a key branch for being saved on exit */
1462 static void register_branch_for_saving( struct key *key, const char *path, size_t len )
1464 if (save_branch_count >= MAX_SAVE_BRANCH_INFO)
1466 set_error( STATUS_NO_MORE_ENTRIES );
1467 return;
1469 if (!len || !(save_branch_info[save_branch_count].path = memdup( path, len ))) return;
1470 save_branch_info[save_branch_count].path[len - 1] = 0;
1471 save_branch_info[save_branch_count].key = (struct key *)grab_object( key );
1472 save_branch_count++;
1475 /* save a registry branch to a file */
1476 static int save_branch( struct key *key, const char *path )
1478 char *p, *real, *tmp = NULL;
1479 int fd, count = 0, ret = 0;
1480 FILE *f;
1482 /* get the real path */
1484 if (!(real = malloc( PATH_MAX ))) return 0;
1485 if (!realpath( path, real ))
1487 free( real );
1488 real = NULL;
1490 else path = real;
1492 /* test the file type */
1494 if ((fd = open( path, O_WRONLY )) != -1)
1496 struct stat st;
1497 /* if file is not a regular file or has multiple links,
1498 write directly into it; otherwise use a temp file */
1499 if (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1501 ftruncate( fd, 0 );
1502 goto save;
1504 close( fd );
1507 /* create a temp file in the same directory */
1509 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1510 strcpy( tmp, path );
1511 if ((p = strrchr( tmp, '/' ))) p++;
1512 else p = tmp;
1513 for (;;)
1515 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1516 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1517 if (errno != EEXIST) goto done;
1518 close( fd );
1521 /* now save to it */
1523 save:
1524 if (!(f = fdopen( fd, "w" )))
1526 if (tmp) unlink( tmp );
1527 close( fd );
1528 goto done;
1531 if (debug_level > 1)
1533 fprintf( stderr, "%s: ", path );
1534 dump_operation( key, NULL, "saving" );
1537 save_all_subkeys( key, f );
1538 ret = !fclose(f);
1540 if (tmp)
1542 /* if successfully written, rename to final name */
1543 if (ret) ret = !rename( tmp, path );
1544 if (!ret) unlink( tmp );
1545 free( tmp );
1548 done:
1549 if (real) free( real );
1550 return ret;
1553 /* periodic saving of the registry */
1554 static void periodic_save( void *arg )
1556 int i;
1557 for (i = 0; i < save_branch_count; i++)
1558 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1559 add_timeout( &next_save_time, save_period );
1560 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1563 /* save the registry and close the top-level keys; used on server exit */
1564 void close_registry(void)
1566 int i;
1568 for (i = 0; i < save_branch_count; i++)
1570 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1572 fprintf( stderr, "wineserver: could not save registry branch to %s",
1573 save_branch_info[i].path );
1574 perror( " " );
1576 release_object( save_branch_info[i].key );
1578 release_object( root_key );
1582 /* create a registry key */
1583 DECL_HANDLER(create_key)
1585 struct key *key = NULL, *parent;
1586 unsigned int access = req->access;
1587 WCHAR *name, *class;
1588 size_t len;
1590 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1591 req->hkey = 0;
1592 if (!(name = copy_req_path( req, &len, !req->parent ))) return;
1593 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1595 if (len == get_req_data_size(req)) /* no class specified */
1597 key = create_key( parent, name, NULL, req->options, req->modif, &req->created );
1599 else
1601 const WCHAR *class_ptr = (WCHAR *)((char *)get_req_data(req) + len);
1603 if ((class = req_strdupW( req, class_ptr, get_req_data_size(req) - len )))
1605 key = create_key( parent, name, class, req->options,
1606 req->modif, &req->created );
1607 free( class );
1610 if (key)
1612 req->hkey = alloc_handle( current->process, key, access, 0 );
1613 release_object( key );
1615 release_object( parent );
1619 /* open a registry key */
1620 DECL_HANDLER(open_key)
1622 struct key *key, *parent;
1623 unsigned int access = req->access;
1625 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1626 req->hkey = 0;
1627 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1629 WCHAR *name = copy_path( get_req_data(req), get_req_data_size(req), !req->parent );
1630 if (name && (key = open_key( parent, name )))
1632 req->hkey = alloc_handle( current->process, key, access, 0 );
1633 release_object( key );
1635 release_object( parent );
1639 /* delete a registry key */
1640 DECL_HANDLER(delete_key)
1642 struct key *key;
1644 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1646 delete_key( key );
1647 release_object( key );
1651 /* enumerate registry subkeys */
1652 DECL_HANDLER(enum_key)
1654 struct key *key;
1655 size_t len = 0;
1657 if ((key = get_hkey_obj( req->hkey,
1658 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1660 len = enum_key( key, req->index, req );
1661 release_object( key );
1663 set_req_data_size( req, len );
1666 /* set a value of a registry key */
1667 DECL_HANDLER(set_key_value)
1669 struct key *key;
1670 WCHAR *name;
1671 size_t len;
1673 if (!(name = copy_req_path( req, &len, 0 ))) return;
1674 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1676 size_t datalen = get_req_data_size(req) - len;
1677 const char *data = (char *)get_req_data(req) + len;
1679 set_value( key, name, req->type, req->total, req->offset, datalen, data );
1680 release_object( key );
1684 /* retrieve the value of a registry key */
1685 DECL_HANDLER(get_key_value)
1687 struct key *key;
1688 WCHAR *name;
1689 size_t len = 0, tmp;
1691 req->len = 0;
1692 if (!(name = copy_req_path( req, &tmp, 0 ))) return;
1693 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1695 len = get_value( key, name, req->offset, get_req_data_size(req),
1696 &req->type, &req->len, get_req_data(req) );
1697 release_object( key );
1699 set_req_data_size( req, len );
1702 /* enumerate the value of a registry key */
1703 DECL_HANDLER(enum_key_value)
1705 struct key *key;
1706 size_t len = 0;
1708 req->len = 0;
1709 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1711 len = enum_value( key, req->index, req->offset, get_req_data_size(req),
1712 &req->type, &req->len, get_req_data(req) );
1713 release_object( key );
1715 set_req_data_size( req, len );
1718 /* delete a value of a registry key */
1719 DECL_HANDLER(delete_key_value)
1721 WCHAR *name;
1722 struct key *key;
1724 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1726 if ((name = req_strdupW( req, get_req_data(req), get_req_data_size(req) )))
1728 delete_value( key, name );
1729 free( name );
1731 release_object( key );
1735 /* load a registry branch from a file */
1736 DECL_HANDLER(load_registry)
1738 struct key *key;
1740 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1742 /* FIXME: use subkey name */
1743 load_registry( key, req->file );
1744 release_object( key );
1748 /* save a registry branch to a file */
1749 DECL_HANDLER(save_registry)
1751 struct key *key;
1753 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1755 save_registry( key, req->file );
1756 release_object( key );
1760 /* set the current and saving level for the registry */
1761 DECL_HANDLER(set_registry_levels)
1763 current_level = req->current;
1764 saving_level = req->saving;
1766 /* set periodic save timer */
1768 if (save_timeout_user)
1770 remove_timeout_user( save_timeout_user );
1771 save_timeout_user = NULL;
1773 if ((save_period = req->period))
1775 if (save_period < 10000) save_period = 10000; /* limit rate */
1776 gettimeofday( &next_save_time, 0 );
1777 add_timeout( &next_save_time, save_period );
1778 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1782 /* save a registry branch at server exit */
1783 DECL_HANDLER(save_registry_atexit)
1785 struct key *key;
1787 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1789 register_branch_for_saving( key, get_req_data(req), get_req_data_size(req) );
1790 release_object( key );