Fixed C++ operators return value.
[wine/multimedia.git] / server / registry.c
blobbd125320f370cb36c684d225c06a55796f37e8c8
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 root keys */
71 #define HKEY_ROOT_FIRST HKEY_CLASSES_ROOT
72 #define HKEY_ROOT_LAST HKEY_DYN_DATA
73 #define NB_ROOT_KEYS (HKEY_ROOT_LAST - HKEY_ROOT_FIRST + 1)
74 #define IS_ROOT_HKEY(h) (((h) >= HKEY_ROOT_FIRST) && ((h) <= HKEY_ROOT_LAST))
75 static struct key *root_keys[NB_ROOT_KEYS];
78 /* keys saving level */
79 /* current_level is the level that is put into all newly created or modified keys */
80 /* saving_level is the minimum level that a key needs in order to get saved */
81 static int current_level;
82 static int saving_level;
84 static struct timeval next_save_time; /* absolute time of next periodic save */
85 static int save_period; /* delay between periodic saves (ms) */
86 static struct timeout_user *save_timeout_user; /* saving timer */
88 /* information about where to save a registry branch */
89 struct save_branch_info
91 struct key *key;
92 char *path;
95 #define MAX_SAVE_BRANCH_INFO 8
96 static int save_branch_count;
97 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
100 /* information about a file being loaded */
101 struct file_load_info
103 FILE *file; /* input file */
104 char *buffer; /* line buffer */
105 int len; /* buffer length */
106 int line; /* current input line */
107 char *tmp; /* temp buffer to use while parsing input */
108 int tmplen; /* length of temp buffer */
112 static void key_dump( struct object *obj, int verbose );
113 static void key_destroy( struct object *obj );
115 static const struct object_ops key_ops =
117 sizeof(struct key), /* size */
118 key_dump, /* dump */
119 no_add_queue, /* add_queue */
120 NULL, /* remove_queue */
121 NULL, /* signaled */
122 NULL, /* satisfied */
123 NULL, /* get_poll_events */
124 NULL, /* poll_event */
125 no_read_fd, /* get_read_fd */
126 no_write_fd, /* get_write_fd */
127 no_flush, /* flush */
128 no_get_file_info, /* get_file_info */
129 key_destroy /* destroy */
134 * The registry text file format v2 used by this code is similar to the one
135 * used by REGEDIT import/export functionality, with the following differences:
136 * - strings and key names can contain \x escapes for Unicode
137 * - key names use escapes too in order to support Unicode
138 * - the modification time optionally follows the key name
139 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
142 static inline char to_hex( char ch )
144 if (isdigit(ch)) return ch - '0';
145 return tolower(ch) - 'a' + 10;
148 /* dump the full path of a key */
149 static void dump_path( struct key *key, struct key *base, FILE *f )
151 if (key->parent && key != base)
153 dump_path( key->parent, base, f );
154 fprintf( f, "\\\\" );
156 dump_strW( key->name, strlenW(key->name), f, "[]" );
159 /* dump a value to a text file */
160 static void dump_value( struct key_value *value, FILE *f )
162 int i, count;
164 if (value->name[0])
166 fputc( '\"', f );
167 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
168 count += fprintf( f, "\"=" );
170 else count = fprintf( f, "@=" );
172 switch(value->type)
174 case REG_SZ:
175 case REG_EXPAND_SZ:
176 case REG_MULTI_SZ:
177 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
178 fputc( '\"', f );
179 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
180 fputc( '\"', f );
181 break;
182 case REG_DWORD:
183 if (value->len == sizeof(DWORD))
185 DWORD dw;
186 memcpy( &dw, value->data, sizeof(DWORD) );
187 fprintf( f, "dword:%08lx", dw );
188 break;
190 /* else fall through */
191 default:
192 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
193 else count += fprintf( f, "hex(%x):", value->type );
194 for (i = 0; i < value->len; i++)
196 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
197 if (i < value->len-1)
199 fputc( ',', f );
200 if (++count > 76)
202 fprintf( f, "\\\n " );
203 count = 2;
207 break;
209 fputc( '\n', f );
212 /* save a registry and all its subkeys to a text file */
213 static void save_subkeys( struct key *key, struct key *base, FILE *f )
215 int i;
217 if (key->flags & KEY_VOLATILE) return;
218 /* save key if it has the proper level, and has either some values or no subkeys */
219 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
220 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
222 fprintf( f, "\n[" );
223 dump_path( key, base, f );
224 fprintf( f, "] %ld\n", key->modif );
225 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
227 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
230 static void dump_operation( struct key *key, struct key_value *value, const char *op )
232 fprintf( stderr, "%s key ", op );
233 if (key) dump_path( key, NULL, stderr );
234 else fprintf( stderr, "ERROR" );
235 if (value)
237 fprintf( stderr, " value ");
238 dump_value( value, stderr );
240 else fprintf( stderr, "\n" );
243 static void key_dump( struct object *obj, int verbose )
245 struct key *key = (struct key *)obj;
246 assert( obj->ops == &key_ops );
247 fprintf( stderr, "Key flags=%x ", key->flags );
248 dump_path( key, NULL, stderr );
249 fprintf( stderr, "\n" );
252 static void key_destroy( struct object *obj )
254 int i;
255 struct key *key = (struct key *)obj;
256 assert( obj->ops == &key_ops );
258 if (key->name) free( key->name );
259 if (key->class) free( key->class );
260 for (i = 0; i <= key->last_value; i++)
262 free( key->values[i].name );
263 if (key->values[i].data) free( key->values[i].data );
265 for (i = 0; i <= key->last_subkey; i++)
267 key->subkeys[i]->parent = NULL;
268 release_object( key->subkeys[i] );
272 /* duplicate a key path from the request buffer */
273 /* returns a pointer to a static buffer, so only useable once per request */
274 static WCHAR *copy_path( const path_t path )
276 static WCHAR buffer[MAX_PATH+1];
277 WCHAR *p = buffer;
279 while (p < buffer + sizeof(buffer) - 1) if (!(*p++ = *path++)) break;
280 *p = 0;
281 return buffer;
284 /* return the next token in a given path */
285 /* returns a pointer to a static buffer, so only useable once per request */
286 static WCHAR *get_path_token( const WCHAR *initpath, size_t maxlen )
288 static const WCHAR *path;
289 static const WCHAR *end;
290 static WCHAR buffer[MAX_PATH+1];
291 WCHAR *p = buffer;
293 if (initpath)
295 path = initpath;
296 end = path + maxlen / sizeof(WCHAR);
298 while ((path < end) && (*path == '\\')) path++;
299 while ((path < end) && (p < buffer + sizeof(buffer) - 1))
301 WCHAR ch = *path;
302 if (!ch || (ch == '\\')) break;
303 *p++ = ch;
304 path++;
306 *p = 0;
307 return buffer;
310 /* duplicate a Unicode string from the request buffer */
311 static WCHAR *req_strdupW( const void *req, const WCHAR *str )
313 WCHAR *name;
314 size_t len = get_req_strlenW( req, str );
315 if ((name = mem_alloc( (len + 1) * sizeof(WCHAR) )) != NULL)
317 memcpy( name, str, len * sizeof(WCHAR) );
318 name[len] = 0;
320 return name;
323 /* allocate a key object */
324 static struct key *alloc_key( const WCHAR *name, time_t modif )
326 struct key *key;
327 if ((key = (struct key *)alloc_object( &key_ops, -1 )))
329 key->class = NULL;
330 key->flags = 0;
331 key->last_subkey = -1;
332 key->nb_subkeys = 0;
333 key->subkeys = NULL;
334 key->nb_values = 0;
335 key->last_value = -1;
336 key->values = NULL;
337 key->level = current_level;
338 key->modif = modif;
339 key->parent = NULL;
340 if (!(key->name = strdupW( name )))
342 release_object( key );
343 key = NULL;
346 return key;
349 /* update key modification time */
350 static void touch_key( struct key *key )
352 key->modif = time(NULL);
353 key->level = max( key->level, current_level );
356 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
357 static int grow_subkeys( struct key *key )
359 struct key **new_subkeys;
360 int nb_subkeys;
362 if (key->nb_subkeys)
364 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
365 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
367 set_error( STATUS_NO_MEMORY );
368 return 0;
371 else
373 nb_subkeys = MIN_VALUES;
374 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
376 key->subkeys = new_subkeys;
377 key->nb_subkeys = nb_subkeys;
378 return 1;
381 /* allocate a subkey for a given key, and return its index */
382 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
384 struct key *key;
385 int i;
387 if (parent->last_subkey + 1 == parent->nb_subkeys)
389 /* need to grow the array */
390 if (!grow_subkeys( parent )) return NULL;
392 if ((key = alloc_key( name, modif )) != NULL)
394 key->parent = parent;
395 for (i = ++parent->last_subkey; i > index; i--)
396 parent->subkeys[i] = parent->subkeys[i-1];
397 parent->subkeys[index] = key;
399 return key;
402 /* free a subkey of a given key */
403 static void free_subkey( struct key *parent, int index )
405 struct key *key;
406 int i, nb_subkeys;
408 assert( index >= 0 );
409 assert( index <= parent->last_subkey );
411 key = parent->subkeys[index];
412 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
413 parent->last_subkey--;
414 key->flags |= KEY_DELETED;
415 key->parent = NULL;
416 release_object( key );
418 /* try to shrink the array */
419 nb_subkeys = key->nb_subkeys;
420 if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2)
422 struct key **new_subkeys;
423 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
424 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
425 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
426 key->subkeys = new_subkeys;
427 key->nb_subkeys = nb_subkeys;
431 /* find the named child of a given key and return its index */
432 static struct key *find_subkey( struct key *key, const WCHAR *name, int *index )
434 int i, min, max, res;
436 min = 0;
437 max = key->last_subkey;
438 while (min <= max)
440 i = (min + max) / 2;
441 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
443 *index = i;
444 return key->subkeys[i];
446 if (res > 0) max = i - 1;
447 else min = i + 1;
449 *index = min; /* this is where we should insert it */
450 return NULL;
453 /* open a subkey */
454 static struct key *open_key( struct key *key, const WCHAR *name, size_t maxlen )
456 int index;
457 WCHAR *path;
459 path = get_path_token( name, maxlen );
460 while (*path)
462 if (!(key = find_subkey( key, path, &index )))
464 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
465 break;
467 path = get_path_token( NULL, 0 );
470 if (debug_level > 1) dump_operation( key, NULL, "Open" );
471 if (key) grab_object( key );
472 return key;
475 /* create a subkey */
476 static struct key *create_key( struct key *key, const WCHAR *name, size_t maxlen, WCHAR *class,
477 unsigned int options, time_t modif, int *created )
479 struct key *base;
480 int base_idx, index, flags = 0;
481 WCHAR *path;
483 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
485 set_error( STATUS_KEY_DELETED );
486 return NULL;
488 if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE;
489 else if (key->flags & KEY_VOLATILE)
491 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
492 return NULL;
495 path = get_path_token( name, maxlen );
496 *created = 0;
497 while (*path)
499 struct key *subkey;
500 if (!(subkey = find_subkey( key, path, &index ))) break;
501 key = subkey;
502 path = get_path_token( NULL, 0 );
505 /* create the remaining part */
507 if (!*path) goto done;
508 *created = 1;
509 base = key;
510 base_idx = index;
511 key = alloc_subkey( key, path, index, modif );
512 while (key)
514 key->flags |= flags;
515 path = get_path_token( NULL, 0 );
516 if (!*path) goto done;
517 /* we know the index is always 0 in a new key */
518 key = alloc_subkey( key, path, 0, modif );
520 if (base_idx != -1) free_subkey( base, base_idx );
521 return NULL;
523 done:
524 if (debug_level > 1) dump_operation( key, NULL, "Create" );
525 if (class) key->class = strdupW(class);
526 grab_object( key );
527 return key;
530 /* find a subkey of a given key by its index */
531 static void enum_key( struct key *parent, int index, WCHAR *name, WCHAR *class, time_t *modif )
533 struct key *key;
535 if ((index < 0) || (index > parent->last_subkey)) set_error( STATUS_NO_MORE_ENTRIES );
536 else
538 key = parent->subkeys[index];
539 *modif = key->modif;
540 strcpyW( name, key->name );
541 if (key->class) strcpyW( class, key->class ); /* FIXME: length */
542 else *class = 0;
543 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
547 /* query information about a key */
548 static void query_key( struct key *key, struct query_key_info_request *req )
550 int i, len;
551 int max_subkey = 0, max_class = 0;
552 int max_value = 0, max_data = 0;
554 for (i = 0; i <= key->last_subkey; i++)
556 struct key *subkey = key->subkeys[i];
557 len = strlenW( subkey->name );
558 if (len > max_subkey) max_subkey = len;
559 if (!subkey->class) continue;
560 len = strlenW( subkey->class );
561 if (len > max_class) max_class = len;
563 for (i = 0; i <= key->last_value; i++)
565 len = strlenW( key->values[i].name );
566 if (len > max_value) max_value = len;
567 len = key->values[i].len;
568 if (len > max_data) max_data = len;
570 req->subkeys = key->last_subkey + 1;
571 req->max_subkey = max_subkey;
572 req->max_class = max_class;
573 req->values = key->last_value + 1;
574 req->max_value = max_value;
575 req->max_data = max_data;
576 req->modif = key->modif;
577 strcpyW( req->name, key->name);
578 if (key->class) strcpyW( req->class, key->class ); /* FIXME: length */
579 else req->class[0] = 0;
580 if (debug_level > 1) dump_operation( key, NULL, "Query" );
583 /* delete a key and its values */
584 static void delete_key( struct key *key, const WCHAR *name, size_t maxlen )
586 int index;
587 struct key *parent;
588 WCHAR *path;
590 path = get_path_token( name, maxlen );
591 if (!*path)
593 /* deleting this key, must find parent and index */
594 if (key->flags & KEY_ROOT)
596 set_error( STATUS_ACCESS_DENIED );
597 return;
599 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
601 set_error( STATUS_KEY_DELETED );
602 return;
604 for (index = 0; index <= parent->last_subkey; index++)
605 if (parent->subkeys[index] == key) break;
606 assert( index <= parent->last_subkey );
608 else while (*path)
610 parent = key;
611 if (!(key = find_subkey( parent, path, &index )))
613 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
614 return;
616 path = get_path_token( NULL, 0 );
619 /* we can only delete a key that has no subkeys (FIXME) */
620 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
622 set_error( STATUS_ACCESS_DENIED );
623 return;
625 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
626 free_subkey( parent, index );
627 touch_key( parent );
630 /* try to grow the array of values; return 1 if OK, 0 on error */
631 static int grow_values( struct key *key )
633 struct key_value *new_val;
634 int nb_values;
636 if (key->nb_values)
638 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
639 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
641 set_error( STATUS_NO_MEMORY );
642 return 0;
645 else
647 nb_values = MIN_VALUES;
648 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
650 key->values = new_val;
651 key->nb_values = nb_values;
652 return 1;
655 /* find the named value of a given key and return its index in the array */
656 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
658 int i, min, max, res;
660 min = 0;
661 max = key->last_value;
662 while (min <= max)
664 i = (min + max) / 2;
665 if (!(res = strcmpiW( key->values[i].name, name )))
667 *index = i;
668 return &key->values[i];
670 if (res > 0) max = i - 1;
671 else min = i + 1;
673 *index = min; /* this is where we should insert it */
674 return NULL;
677 /* insert a new value or return a pointer to an existing one */
678 static struct key_value *insert_value( struct key *key, const WCHAR *name )
680 struct key_value *value;
681 WCHAR *new_name;
682 int i, index;
684 if (!(value = find_value( key, name, &index )))
686 /* not found, add it */
687 if (key->last_value + 1 == key->nb_values)
689 if (!grow_values( key )) return NULL;
691 if (!(new_name = strdupW(name))) return NULL;
692 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
693 value = &key->values[index];
694 value->name = new_name;
695 value->len = 0;
696 value->data = NULL;
698 return value;
701 /* set a key value */
702 static void set_value( struct key *key, WCHAR *name, int type, unsigned int total_len,
703 unsigned int offset, unsigned int data_len, void *data )
705 struct key_value *value;
706 void *ptr = NULL;
708 if (data_len + offset > total_len)
710 set_error( STATUS_INVALID_PARAMETER );
711 return;
714 if (offset) /* adding data to an existing value */
716 int index;
717 if (!(value = find_value( key, name, &index )))
719 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
720 return;
722 if (value->len != total_len)
724 set_error( STATUS_INVALID_PARAMETER );
725 return;
727 memcpy( (char *)value->data + offset, data, data_len );
728 if (debug_level > 1) dump_operation( key, value, "Set" );
729 return;
732 /* first copy the data */
733 if (total_len)
735 if (!(ptr = mem_alloc( total_len ))) return;
736 memcpy( ptr, data, data_len );
737 if (data_len < total_len) memset( (char *)ptr + data_len, 0, total_len - data_len );
740 if (!(value = insert_value( key, name )))
742 if (ptr) free( ptr );
743 return;
745 if (value->data) free( value->data ); /* already existing, free previous data */
746 value->type = type;
747 value->len = total_len;
748 value->data = ptr;
749 touch_key( key );
750 if (debug_level > 1) dump_operation( key, value, "Set" );
753 /* get a key value */
754 static void get_value( struct key *key, WCHAR *name, unsigned int offset,
755 unsigned int maxlen, int *type, int *len, void *data )
757 struct key_value *value;
758 int index;
760 if ((value = find_value( key, name, &index )))
762 *type = value->type;
763 *len = value->len;
764 if (value->data && offset < value->len)
766 if (maxlen > value->len - offset) maxlen = value->len - offset;
767 memcpy( data, (char *)value->data + offset, maxlen );
769 if (debug_level > 1) dump_operation( key, value, "Get" );
771 else
773 *type = -1;
774 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
778 /* enumerate a key value */
779 static void enum_value( struct key *key, int i, WCHAR *name, unsigned int offset,
780 unsigned int maxlen, int *type, int *len, void *data )
782 struct key_value *value;
784 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
785 else
787 value = &key->values[i];
788 strcpyW( name, value->name );
789 *type = value->type;
790 *len = value->len;
791 if (value->data && offset < value->len)
793 if (maxlen > value->len - offset) maxlen = value->len - offset;
794 memcpy( data, (char *)value->data + offset, maxlen );
796 if (debug_level > 1) dump_operation( key, value, "Enum" );
800 /* delete a value */
801 static void delete_value( struct key *key, const WCHAR *name )
803 struct key_value *value;
804 int i, index, nb_values;
806 if (!(value = find_value( key, name, &index )))
808 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
809 return;
811 if (debug_level > 1) dump_operation( key, value, "Delete" );
812 free( value->name );
813 if (value->data) free( value->data );
814 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
815 key->last_value--;
816 touch_key( key );
818 /* try to shrink the array */
819 nb_values = key->nb_values;
820 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
822 struct key_value *new_val;
823 nb_values -= nb_values / 3; /* shrink by 33% */
824 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
825 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
826 key->values = new_val;
827 key->nb_values = nb_values;
831 static struct key *get_hkey_obj( int hkey, unsigned int access );
833 static struct key *create_root_key( int hkey )
835 int dummy;
836 struct key *key;
838 switch(hkey)
840 /* the two real root-keys */
841 case HKEY_LOCAL_MACHINE:
843 static const WCHAR name[] = { 'M','A','C','H','I','N','E',0 };
844 key = alloc_key( name, time(NULL) );
846 break;
847 case HKEY_USERS:
849 static const WCHAR name[] = { 'U','S','E','R',0 };
850 key = alloc_key( name, time(NULL) );
852 break;
853 /* special subkeys */
854 case HKEY_CLASSES_ROOT:
856 static const WCHAR name[] =
857 { 'S','O','F','T','W','A','R','E','\\','C','l','a','s','s','e','s',0 };
859 struct key *root = get_hkey_obj( HKEY_LOCAL_MACHINE, 0 );
860 assert( root );
861 key = create_key( root, name, sizeof(name), NULL, 0, time(NULL), &dummy );
862 release_object( root );
864 break;
865 case HKEY_CURRENT_CONFIG:
867 static const WCHAR name[] = {
868 'S','Y','S','T','E','M','\\',
869 'C','U','R','R','E','N','T','C','O','N','T','R','O','L','S','E','T','\\',
870 'H','A','R','D','W','A','R','E','P','R','O','F','I','L','E','S','\\',
871 'C','U','R','R','E','N','T',0};
872 struct key *root = get_hkey_obj( HKEY_LOCAL_MACHINE, 0 );
873 assert( root );
874 key = create_key( root, name, sizeof(name), NULL, 0, time(NULL), &dummy );
875 release_object( root );
877 break;
878 case HKEY_CURRENT_USER:
880 /* get the current user name */
881 int i, len;
882 WCHAR *name;
883 char buffer[10];
884 const char *p;
885 struct passwd *pwd = getpwuid( getuid() );
887 if (pwd) p = pwd->pw_name;
888 else
890 sprintf( buffer, "%ld", (long) getuid() );
891 p = buffer;
893 len = strlen(p);
894 if ((name = mem_alloc( (len+1) * sizeof(WCHAR) )))
896 struct key *root = get_hkey_obj( HKEY_USERS, 0 );
897 assert( root );
898 for (i = 0; i <= len; i++) name[i] = p[i];
899 key = create_key( root, name, (len+1) * sizeof(WCHAR),
900 NULL, 0, time(NULL), &dummy );
901 release_object( root );
902 free( name );
904 else key = NULL;
906 break;
907 /* dynamically generated keys */
908 case HKEY_PERFORMANCE_DATA:
910 static const WCHAR name[] = { 'P','E','R','F','D','A','T','A',0 }; /* FIXME */
911 key = alloc_key( name, time(NULL) );
913 break;
914 case HKEY_DYN_DATA:
916 static const WCHAR name[] = { 'D','Y','N','D','A','T','A',0 }; /* FIXME */
917 key = alloc_key( name, time(NULL) );
919 break;
920 default:
921 key = NULL;
922 assert(0);
924 if (key)
926 root_keys[hkey - HKEY_ROOT_FIRST] = key;
927 key->flags |= KEY_ROOT;
929 return key;
932 /* get the registry key corresponding to an hkey handle */
933 static struct key *get_hkey_obj( int hkey, unsigned int access )
935 struct key *key;
937 if (IS_ROOT_HKEY(hkey))
939 if (!(key = root_keys[hkey - HKEY_ROOT_FIRST])) key = create_root_key( hkey );
940 grab_object( key );
942 else
943 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
944 return key;
947 /* read a line from the input file */
948 static int read_next_line( struct file_load_info *info )
950 char *newbuf;
951 int newlen, pos = 0;
953 info->line++;
954 for (;;)
956 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
957 return (pos != 0); /* EOF */
958 pos = strlen(info->buffer);
959 if (info->buffer[pos-1] == '\n')
961 /* got a full line */
962 info->buffer[--pos] = 0;
963 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
964 return 1;
966 if (pos < info->len - 1) return 1; /* EOF but something was read */
968 /* need to enlarge the buffer */
969 newlen = info->len + info->len / 2;
970 if (!(newbuf = realloc( info->buffer, newlen )))
972 set_error( STATUS_NO_MEMORY );
973 return -1;
975 info->buffer = newbuf;
976 info->len = newlen;
980 /* make sure the temp buffer holds enough space */
981 static int get_file_tmp_space( struct file_load_info *info, int size )
983 char *tmp;
984 if (info->tmplen >= size) return 1;
985 if (!(tmp = realloc( info->tmp, size )))
987 set_error( STATUS_NO_MEMORY );
988 return 0;
990 info->tmp = tmp;
991 info->tmplen = size;
992 return 1;
995 /* report an error while loading an input file */
996 static void file_read_error( const char *err, struct file_load_info *info )
998 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
1001 /* parse an escaped string back into Unicode */
1002 /* return the number of chars read from the input, or -1 on output overflow */
1003 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
1005 int count = sizeof(WCHAR); /* for terminating null */
1006 const char *p = src;
1007 while (*p && *p != endchar)
1009 if (*p != '\\') *dest = (WCHAR)*p++;
1010 else
1012 p++;
1013 switch(*p)
1015 case 'a': *dest = '\a'; p++; break;
1016 case 'b': *dest = '\b'; p++; break;
1017 case 'e': *dest = '\e'; p++; break;
1018 case 'f': *dest = '\f'; p++; break;
1019 case 'n': *dest = '\n'; p++; break;
1020 case 'r': *dest = '\r'; p++; break;
1021 case 't': *dest = '\t'; p++; break;
1022 case 'v': *dest = '\v'; p++; break;
1023 case 'x': /* hex escape */
1024 p++;
1025 if (!isxdigit(*p)) *dest = 'x';
1026 else
1028 *dest = to_hex(*p++);
1029 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1030 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1031 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1033 break;
1034 case '0':
1035 case '1':
1036 case '2':
1037 case '3':
1038 case '4':
1039 case '5':
1040 case '6':
1041 case '7': /* octal escape */
1042 *dest = *p++ - '0';
1043 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1044 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1045 break;
1046 default:
1047 *dest = (WCHAR)*p++;
1048 break;
1051 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1052 dest++;
1054 *dest = 0;
1055 if (!*p) return -1; /* delimiter not found */
1056 *len = count;
1057 return p + 1 - src;
1060 /* convert a data type tag to a value type */
1061 static int get_data_type( const char *buffer, int *type, int *parse_type )
1063 struct data_type { const char *tag; int len; int type; int parse_type; };
1065 static const struct data_type data_types[] =
1066 { /* actual type */ /* type to assume for parsing */
1067 { "\"", 1, REG_SZ, REG_SZ },
1068 { "str:\"", 5, REG_SZ, REG_SZ },
1069 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1070 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1071 { "hex:", 4, REG_BINARY, REG_BINARY },
1072 { "dword:", 6, REG_DWORD, REG_DWORD },
1073 { "hex(", 4, -1, REG_BINARY },
1074 { NULL, }
1077 const struct data_type *ptr;
1078 char *end;
1080 for (ptr = data_types; ptr->tag; ptr++)
1082 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1083 *parse_type = ptr->parse_type;
1084 if ((*type = ptr->type) != -1) return ptr->len;
1085 /* "hex(xx):" is special */
1086 *type = (int)strtoul( buffer + 4, &end, 16 );
1087 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1088 return end + 2 - buffer;
1090 return 0;
1093 /* load and create a key from the input file */
1094 static struct key *load_key( struct key *base, const char *buffer, unsigned int options,
1095 int prefix_len, struct file_load_info *info )
1097 WCHAR *p;
1098 int res, len, modif;
1100 len = strlen(buffer) * sizeof(WCHAR);
1101 if (!get_file_tmp_space( info, len )) return NULL;
1103 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1105 file_read_error( "Malformed key", info );
1106 return NULL;
1108 if (!sscanf( buffer + res, " %d", &modif )) modif = time(NULL);
1110 p = (WCHAR *)info->tmp;
1111 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1113 if (!*p)
1115 if (prefix_len > 1)
1117 file_read_error( "Malformed key", info );
1118 return NULL;
1120 /* empty key name, return base key */
1121 return (struct key *)grab_object( base );
1123 return create_key( base, p, len - ((char *)p - info->tmp), NULL, options, modif, &res );
1126 /* parse a comma-separated list of hex digits */
1127 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1129 const char *p = buffer;
1130 int count = 0;
1131 while (isxdigit(*p))
1133 int val;
1134 char buf[3];
1135 memcpy( buf, p, 2 );
1136 buf[2] = 0;
1137 sscanf( buf, "%x", &val );
1138 if (count++ >= *len) return -1; /* dest buffer overflow */
1139 *dest++ = (unsigned char )val;
1140 p += 2;
1141 if (*p == ',') p++;
1143 *len = count;
1144 return p - buffer;
1147 /* parse a value name and create the corresponding value */
1148 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1149 struct file_load_info *info )
1151 int maxlen = strlen(buffer) * sizeof(WCHAR);
1152 if (!get_file_tmp_space( info, maxlen )) return NULL;
1153 if (buffer[0] == '@')
1155 info->tmp[0] = info->tmp[1] = 0;
1156 *len = 1;
1158 else
1160 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1161 (*len)++; /* for initial quote */
1163 if (buffer[*len] != '=') goto error;
1164 (*len)++;
1165 return insert_value( key, (WCHAR *)info->tmp );
1167 error:
1168 file_read_error( "Malformed value name", info );
1169 return NULL;
1172 /* load a value from the input file */
1173 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1175 DWORD dw;
1176 void *ptr, *newptr;
1177 int maxlen, len, res;
1178 int type, parse_type;
1179 struct key_value *value;
1181 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1182 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1183 buffer += len + res;
1185 switch(parse_type)
1187 case REG_SZ:
1188 len = strlen(buffer) * sizeof(WCHAR);
1189 if (!get_file_tmp_space( info, len )) return 0;
1190 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1191 ptr = info->tmp;
1192 break;
1193 case REG_DWORD:
1194 dw = strtoul( buffer, NULL, 16 );
1195 ptr = &dw;
1196 len = sizeof(dw);
1197 break;
1198 case REG_BINARY: /* hex digits */
1199 len = 0;
1200 for (;;)
1202 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1203 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1204 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1205 len += maxlen;
1206 buffer += res;
1207 while (isspace(*buffer)) buffer++;
1208 if (!*buffer) break;
1209 if (*buffer != '\\') goto error;
1210 if (read_next_line( info) != 1) goto error;
1211 buffer = info->buffer;
1212 while (isspace(*buffer)) buffer++;
1214 ptr = info->tmp;
1215 break;
1216 default:
1217 assert(0);
1218 ptr = NULL; /* keep compiler quiet */
1219 break;
1222 if (!len) newptr = NULL;
1223 else if (!(newptr = memdup( ptr, len ))) return 0;
1225 if (value->data) free( value->data );
1226 value->data = newptr;
1227 value->len = len;
1228 value->type = type;
1229 /* update the key level but not the modification time */
1230 key->level = max( key->level, current_level );
1231 return 1;
1233 error:
1234 file_read_error( "Malformed value", info );
1235 return 0;
1238 /* return the length (in path elements) of name that is part of the key name */
1239 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1240 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1242 WCHAR *p;
1243 int res;
1244 int len = strlen(name) * sizeof(WCHAR);
1245 if (!get_file_tmp_space( info, len )) return 0;
1247 if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
1249 file_read_error( "Malformed key", info );
1250 return 0;
1252 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
1253 *p = 0;
1254 for (res = 1; key; res++)
1256 if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
1257 key = key->parent;
1259 if (!key) res = 0; /* no matching name */
1260 return res;
1263 /* load all the keys from the input file */
1264 static void load_keys( struct key *key, FILE *f )
1266 struct key *subkey = NULL;
1267 struct file_load_info info;
1268 char *p;
1269 unsigned int options = 0;
1270 int prefix_len = -1; /* number of key name prefixes to skip */
1272 if (key->flags & KEY_VOLATILE) options |= REG_OPTION_VOLATILE;
1274 info.file = f;
1275 info.len = 4;
1276 info.tmplen = 4;
1277 info.line = 0;
1278 if (!(info.buffer = mem_alloc( info.len ))) return;
1279 if (!(info.tmp = mem_alloc( info.tmplen )))
1281 free( info.buffer );
1282 return;
1285 if ((read_next_line( &info ) != 1) ||
1286 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1288 set_error( STATUS_NOT_REGISTRY_FILE );
1289 goto done;
1292 while (read_next_line( &info ) == 1)
1294 for (p = info.buffer; *p && isspace(*p); p++);
1295 switch(*p)
1297 case '[': /* new key */
1298 if (subkey) release_object( subkey );
1299 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1300 if (!(subkey = load_key( key, p + 1, options, prefix_len, &info )))
1301 file_read_error( "Error creating key", &info );
1302 break;
1303 case '@': /* default value */
1304 case '\"': /* value */
1305 if (subkey) load_value( subkey, p, &info );
1306 else file_read_error( "Value without key", &info );
1307 break;
1308 case '#': /* comment */
1309 case ';': /* comment */
1310 case 0: /* empty line */
1311 break;
1312 default:
1313 file_read_error( "Unrecognized input", &info );
1314 break;
1318 done:
1319 if (subkey) release_object( subkey );
1320 free( info.buffer );
1321 free( info.tmp );
1324 /* load a part of the registry from a file */
1325 static void load_registry( struct key *key, int handle )
1327 struct object *obj;
1328 int fd;
1330 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return;
1331 fd = obj->ops->get_read_fd( obj );
1332 release_object( obj );
1333 if (fd != -1)
1335 FILE *f = fdopen( fd, "r" );
1336 if (f)
1338 load_keys( key, f );
1339 fclose( f );
1341 else file_set_error();
1345 /* update the level of the parents of a key (only needed for the old format) */
1346 static int update_level( struct key *key )
1348 int i;
1349 int max = key->level;
1350 for (i = 0; i <= key->last_subkey; i++)
1352 int sub = update_level( key->subkeys[i] );
1353 if (sub > max) max = sub;
1355 key->level = max;
1356 return max;
1359 /* save a registry branch to a file */
1360 static void save_all_subkeys( struct key *key, FILE *f )
1362 fprintf( f, "WINE REGISTRY Version 2\n" );
1363 fprintf( f, ";; All keys relative to " );
1364 dump_path( key, NULL, f );
1365 fprintf( f, "\n" );
1366 save_subkeys( key, key, f );
1369 /* save a registry branch to a file handle */
1370 static void save_registry( struct key *key, int handle )
1372 struct object *obj;
1373 int fd;
1375 if (key->flags & KEY_DELETED)
1377 set_error( STATUS_KEY_DELETED );
1378 return;
1380 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return;
1381 fd = obj->ops->get_write_fd( obj );
1382 release_object( obj );
1383 if (fd != -1)
1385 FILE *f = fdopen( fd, "w" );
1386 if (f)
1388 save_all_subkeys( key, f );
1389 if (fclose( f )) file_set_error();
1391 else
1393 file_set_error();
1394 close( fd );
1399 /* register a key branch for being saved on exit */
1400 static void register_branch_for_saving( struct key *key, const char *path, size_t len )
1402 if (save_branch_count >= MAX_SAVE_BRANCH_INFO)
1404 set_error( STATUS_NO_MORE_ENTRIES );
1405 return;
1407 if (!(save_branch_info[save_branch_count].path = memdup( path, len+1 ))) return;
1408 save_branch_info[save_branch_count].path[len] = 0;
1409 save_branch_info[save_branch_count].key = (struct key *)grab_object( key );
1410 save_branch_count++;
1413 /* save a registry branch to a file */
1414 static int save_branch( struct key *key, const char *path )
1416 char *p, *real, *tmp = NULL;
1417 int fd, count = 0, ret = 0;
1418 FILE *f;
1420 /* get the real path */
1422 if (!(real = malloc( PATH_MAX ))) return 0;
1423 if (!realpath( path, real ))
1425 free( real );
1426 real = NULL;
1428 else path = real;
1430 /* test the file type */
1432 if ((fd = open( path, O_WRONLY )) != -1)
1434 struct stat st;
1435 /* if file is not a regular file or has multiple links,
1436 write directly into it; otherwise use a temp file */
1437 if (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1439 ftruncate( fd, 0 );
1440 goto save;
1442 close( fd );
1445 /* create a temp file in the same directory */
1447 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1448 strcpy( tmp, path );
1449 if ((p = strrchr( tmp, '/' ))) p++;
1450 else p = tmp;
1451 for (;;)
1453 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1454 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1455 if (errno != EEXIST) goto done;
1456 close( fd );
1459 /* now save to it */
1461 save:
1462 if (!(f = fdopen( fd, "w" )))
1464 if (tmp) unlink( tmp );
1465 close( fd );
1466 goto done;
1469 if (debug_level > 1)
1471 fprintf( stderr, "%s: ", path );
1472 dump_operation( key, NULL, "saving" );
1475 save_all_subkeys( key, f );
1476 ret = !fclose(f);
1478 if (tmp)
1480 /* if successfully written, rename to final name */
1481 if (ret) ret = !rename( tmp, path );
1482 if (!ret) unlink( tmp );
1483 free( tmp );
1486 done:
1487 if (real) free( real );
1488 return ret;
1491 /* periodic saving of the registry */
1492 static void periodic_save( void *arg )
1494 int i;
1495 for (i = 0; i < save_branch_count; i++)
1496 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1497 add_timeout( &next_save_time, save_period );
1498 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1501 /* save the registry and close the top-level keys; used on server exit */
1502 void close_registry(void)
1504 int i;
1506 for (i = 0; i < save_branch_count; i++)
1508 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1510 fprintf( stderr, "wineserver: could not save registry branch to %s",
1511 save_branch_info[i].path );
1512 perror( " " );
1514 release_object( save_branch_info[i].key );
1516 for (i = 0; i < NB_ROOT_KEYS; i++)
1518 if (root_keys[i]) release_object( root_keys[i] );
1523 /* create a registry key */
1524 DECL_HANDLER(create_key)
1526 struct key *key, *parent;
1527 WCHAR *class;
1528 unsigned int access = req->access;
1530 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1531 req->hkey = -1;
1532 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1534 if ((class = req_strdupW( req, req->class )))
1536 if ((key = create_key( parent, req->name, sizeof(req->name), class, req->options,
1537 req->modif, &req->created )))
1539 req->hkey = alloc_handle( current->process, key, access, 0 );
1540 release_object( key );
1542 free( class );
1544 release_object( parent );
1548 /* open a registry key */
1549 DECL_HANDLER(open_key)
1551 struct key *key, *parent;
1552 unsigned int access = req->access;
1554 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1555 req->hkey = -1;
1556 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1558 if ((key = open_key( parent, req->name, sizeof(req->name) )))
1560 req->hkey = alloc_handle( current->process, key, access, 0 );
1561 release_object( key );
1563 release_object( parent );
1567 /* delete a registry key */
1568 DECL_HANDLER(delete_key)
1570 struct key *key;
1572 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1574 delete_key( key, req->name, sizeof(req->name) );
1575 release_object( key );
1579 /* close a registry key */
1580 DECL_HANDLER(close_key)
1582 int hkey = req->hkey;
1583 /* ignore attempts to close a root key */
1584 if (!IS_ROOT_HKEY(hkey)) close_handle( current->process, hkey );
1587 /* enumerate registry subkeys */
1588 DECL_HANDLER(enum_key)
1590 struct key *key;
1592 req->name[0] = req->class[0] = 0;
1593 if ((key = get_hkey_obj( req->hkey, KEY_ENUMERATE_SUB_KEYS )))
1595 enum_key( key, req->index, req->name, req->class, &req->modif );
1596 release_object( key );
1600 /* query information about a registry key */
1601 DECL_HANDLER(query_key_info)
1603 struct key *key;
1605 req->name[0] = req->class[0] = 0;
1606 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1608 query_key( key, req );
1609 release_object( key );
1613 /* set a value of a registry key */
1614 DECL_HANDLER(set_key_value)
1616 struct key *key;
1617 unsigned int max = get_req_size( req, req->data, sizeof(req->data[0]) );
1618 unsigned int datalen = req->len;
1620 if (datalen > max) datalen = max;
1621 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1623 set_value( key, copy_path( req->name ), req->type, req->total,
1624 req->offset, datalen, req->data );
1625 release_object( key );
1629 /* retrieve the value of a registry key */
1630 DECL_HANDLER(get_key_value)
1632 struct key *key;
1633 unsigned int max = get_req_size( req, req->data, sizeof(req->data[0]) );
1635 req->len = 0;
1636 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1638 get_value( key, copy_path( req->name ), req->offset, max,
1639 &req->type, &req->len, req->data );
1640 release_object( key );
1644 /* enumerate the value of a registry key */
1645 DECL_HANDLER(enum_key_value)
1647 struct key *key;
1648 unsigned int max = get_req_size( req, req->data, sizeof(req->data[0]) );
1650 req->len = 0;
1651 req->name[0] = 0;
1652 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1654 enum_value( key, req->index, req->name, req->offset, max,
1655 &req->type, &req->len, req->data );
1656 release_object( key );
1660 /* delete a value of a registry key */
1661 DECL_HANDLER(delete_key_value)
1663 WCHAR *name;
1664 struct key *key;
1666 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1668 if ((name = req_strdupW( req, req->name )))
1670 delete_value( key, name );
1671 free( name );
1673 release_object( key );
1677 /* load a registry branch from a file */
1678 DECL_HANDLER(load_registry)
1680 struct key *key;
1682 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1684 /* FIXME: use subkey name */
1685 load_registry( key, req->file );
1686 release_object( key );
1690 /* save a registry branch to a file */
1691 DECL_HANDLER(save_registry)
1693 struct key *key;
1695 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1697 save_registry( key, req->file );
1698 release_object( key );
1702 /* set the current and saving level for the registry */
1703 DECL_HANDLER(set_registry_levels)
1705 current_level = req->current;
1706 saving_level = req->saving;
1708 /* set periodic save timer */
1710 if (save_timeout_user)
1712 remove_timeout_user( save_timeout_user );
1713 save_timeout_user = NULL;
1715 if ((save_period = req->period))
1717 if (save_period < 10000) save_period = 10000; /* limit rate */
1718 gettimeofday( &next_save_time, 0 );
1719 add_timeout( &next_save_time, save_period );
1720 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1724 /* save a registry branch at server exit */
1725 DECL_HANDLER(save_registry_atexit)
1727 struct key *key;
1729 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1731 register_branch_for_saving( key, req->file, get_req_strlen( req, req->file ) );
1732 release_object( key );