- Wine was passing the wrong hwnd as the owner when processing owner
[wine/multimedia.git] / server / registry.c
blob6d6b034943d9ae7d9f18b74ae527d741e1f916f6
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 <errno.h>
15 #include <fcntl.h>
16 #include <limits.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <pwd.h>
23 #include "object.h"
24 #include "handle.h"
25 #include "request.h"
26 #include "unicode.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winnt.h" /* registry definitions */
33 /* a registry key */
34 struct key
36 struct object obj; /* object header */
37 WCHAR *name; /* key name */
38 WCHAR *class; /* key class */
39 struct key *parent; /* parent key */
40 int last_subkey; /* last in use subkey */
41 int nb_subkeys; /* count of allocated subkeys */
42 struct key **subkeys; /* subkeys array */
43 int last_value; /* last in use value */
44 int nb_values; /* count of allocated values in array */
45 struct key_value *values; /* values array */
46 short flags; /* flags */
47 short level; /* saving level */
48 time_t modif; /* last modification time */
51 /* key flags */
52 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
53 #define KEY_DELETED 0x0002 /* key has been deleted */
54 #define KEY_ROOT 0x0004 /* key is a root key */
56 /* a key value */
57 struct key_value
59 WCHAR *name; /* value name */
60 int type; /* value type */
61 size_t len; /* value data length in bytes */
62 void *data; /* pointer to value data */
65 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
66 #define MIN_VALUES 8 /* min. number of allocated values per key */
69 /* the root keys */
70 #define HKEY_ROOT_FIRST HKEY_CLASSES_ROOT
71 #define HKEY_ROOT_LAST HKEY_DYN_DATA
72 #define NB_ROOT_KEYS (HKEY_ROOT_LAST - HKEY_ROOT_FIRST + 1)
73 #define IS_ROOT_HKEY(h) (((h) >= HKEY_ROOT_FIRST) && ((h) <= HKEY_ROOT_LAST))
74 static struct key *root_keys[NB_ROOT_KEYS];
76 static const char * const root_key_names[NB_ROOT_KEYS] =
78 "HKEY_CLASSES_ROOT",
79 "HKEY_CURRENT_USER",
80 "HKEY_LOCAL_MACHINE",
81 "HKEY_USERS",
82 "HKEY_PERFORMANCE_DATA",
83 "HKEY_CURRENT_CONFIG",
84 "HKEY_DYN_DATA"
88 /* keys saving level */
89 /* current_level is the level that is put into all newly created or modified keys */
90 /* saving_level is the minimum level that a key needs in order to get saved */
91 static int current_level;
92 static int saving_level;
94 static int saving_version = 1; /* file format version */
96 static struct timeval next_save_time; /* absolute time of next periodic save */
97 static int save_period; /* delay between periodic saves (ms) */
98 static struct timeout_user *save_timeout_user; /* saving timer */
100 /* information about where to save a registry branch */
101 struct save_branch_info
103 struct key *key;
104 char *path;
107 #define MAX_SAVE_BRANCH_INFO 8
108 static int save_branch_count;
109 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
112 /* information about a file being loaded */
113 struct file_load_info
115 FILE *file; /* input file */
116 char *buffer; /* line buffer */
117 int len; /* buffer length */
118 int line; /* current input line */
119 char *tmp; /* temp buffer to use while parsing input */
120 int tmplen; /* length of temp buffer */
124 static void key_dump( struct object *obj, int verbose );
125 static void key_destroy( struct object *obj );
127 static const struct object_ops key_ops =
129 sizeof(struct key), /* size */
130 key_dump, /* dump */
131 no_add_queue, /* add_queue */
132 NULL, /* remove_queue */
133 NULL, /* signaled */
134 NULL, /* satisfied */
135 NULL, /* get_poll_events */
136 NULL, /* poll_event */
137 no_read_fd, /* get_read_fd */
138 no_write_fd, /* get_write_fd */
139 no_flush, /* flush */
140 no_get_file_info, /* get_file_info */
141 key_destroy /* destroy */
146 * The registry text file format v2 used by this code is similar to the one
147 * used by REGEDIT import/export functionality, with the following differences:
148 * - strings and key names can contain \x escapes for Unicode
149 * - key names use escapes too in order to support Unicode
150 * - the modification time optionally follows the key name
151 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
154 static inline char to_hex( char ch )
156 if (isdigit(ch)) return ch - '0';
157 return tolower(ch) - 'a' + 10;
160 /* dump the full path of a key */
161 static void dump_path( struct key *key, struct key *base, FILE *f )
163 if (key->parent && key != base)
165 dump_path( key->parent, base, f );
166 fprintf( f, "\\\\" );
169 if (key->name) dump_strW( key->name, strlenW(key->name), f, "[]" );
170 else /* root key */
172 int i;
173 for (i = 0; i < NB_ROOT_KEYS; i++)
174 if (root_keys[i] == key) fprintf( f, "%s", root_key_names[i] );
178 /* dump a value to a text file */
179 static void dump_value( struct key_value *value, FILE *f )
181 int i, count;
183 if (value->name[0])
185 fputc( '\"', f );
186 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
187 count += fprintf( f, "\"=" );
189 else count = fprintf( f, "@=" );
191 switch(value->type)
193 case REG_SZ:
194 case REG_EXPAND_SZ:
195 case REG_MULTI_SZ:
196 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
197 fputc( '\"', f );
198 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
199 fputc( '\"', f );
200 break;
201 case REG_DWORD:
202 if (value->len == sizeof(DWORD))
204 DWORD dw;
205 memcpy( &dw, value->data, sizeof(DWORD) );
206 fprintf( f, "dword:%08lx", dw );
207 break;
209 /* else fall through */
210 default:
211 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
212 else count += fprintf( f, "hex(%x):", value->type );
213 for (i = 0; i < value->len; i++)
215 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
216 if (i < value->len-1)
218 fputc( ',', f );
219 if (++count > 76)
221 fprintf( f, "\\\n " );
222 count = 2;
226 break;
228 fputc( '\n', f );
231 /* save a registry and all its subkeys to a text file */
232 static void save_subkeys( struct key *key, struct key *base, FILE *f )
234 int i;
236 if (key->flags & KEY_VOLATILE) return;
237 /* save key if it has the proper level, and has either some values or no subkeys */
238 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
239 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
241 fprintf( f, "\n[" );
242 dump_path( key, base, f );
243 fprintf( f, "] %ld\n", key->modif );
244 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
246 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
249 static void dump_operation( struct key *key, struct key_value *value, const char *op )
251 fprintf( stderr, "%s key ", op );
252 if (key) dump_path( key, NULL, stderr );
253 else fprintf( stderr, "ERROR" );
254 if (value)
256 fprintf( stderr, " value ");
257 dump_value( value, stderr );
259 else fprintf( stderr, "\n" );
262 static void key_dump( struct object *obj, int verbose )
264 struct key *key = (struct key *)obj;
265 assert( obj->ops == &key_ops );
266 fprintf( stderr, "Key flags=%x ", key->flags );
267 dump_path( key, NULL, stderr );
268 fprintf( stderr, "\n" );
271 static void key_destroy( struct object *obj )
273 int i;
274 struct key *key = (struct key *)obj;
275 assert( obj->ops == &key_ops );
277 free( key->name );
278 if (key->class) free( key->class );
279 for (i = 0; i <= key->last_value; i++)
281 free( key->values[i].name );
282 if (key->values[i].data) free( key->values[i].data );
284 for (i = 0; i <= key->last_subkey; i++)
286 key->subkeys[i]->parent = NULL;
287 release_object( key->subkeys[i] );
291 /* duplicate a key path from the request buffer */
292 /* returns a pointer to a static buffer, so only useable once per request */
293 static WCHAR *copy_path( const path_t path )
295 static WCHAR buffer[MAX_PATH+1];
296 WCHAR *p = buffer;
298 while (p < buffer + sizeof(buffer) - 1) if (!(*p++ = *path++)) break;
299 *p = 0;
300 return buffer;
303 /* return the next token in a given path */
304 /* returns a pointer to a static buffer, so only useable once per request */
305 static WCHAR *get_path_token( const WCHAR *initpath, size_t maxlen )
307 static const WCHAR *path;
308 static const WCHAR *end;
309 static WCHAR buffer[MAX_PATH+1];
310 WCHAR *p = buffer;
312 if (initpath)
314 path = initpath;
315 end = path + maxlen / sizeof(WCHAR);
317 while ((path < end) && (*path == '\\')) path++;
318 while ((path < end) && (p < buffer + sizeof(buffer) - 1))
320 WCHAR ch = *path;
321 if (!ch || (ch == '\\')) break;
322 *p++ = ch;
323 path++;
325 *p = 0;
326 return buffer;
329 /* duplicate a Unicode string from the request buffer */
330 static WCHAR *req_strdupW( const void *req, const WCHAR *str )
332 WCHAR *name;
333 size_t len = get_req_strlenW( req, str );
334 if ((name = mem_alloc( (len + 1) * sizeof(WCHAR) )) != NULL)
336 memcpy( name, str, len * sizeof(WCHAR) );
337 name[len] = 0;
339 return name;
342 /* allocate a key object */
343 static struct key *alloc_key( const WCHAR *name, time_t modif )
345 struct key *key;
346 if ((key = (struct key *)alloc_object( &key_ops, -1 )))
348 key->name = NULL;
349 key->class = NULL;
350 key->flags = 0;
351 key->last_subkey = -1;
352 key->nb_subkeys = 0;
353 key->subkeys = NULL;
354 key->nb_values = 0;
355 key->last_value = -1;
356 key->values = NULL;
357 key->level = current_level;
358 key->modif = modif;
359 key->parent = NULL;
360 if (name && !(key->name = strdupW( name )))
362 release_object( key );
363 key = NULL;
366 return key;
369 /* update key modification time */
370 static void touch_key( struct key *key )
372 key->modif = time(NULL);
373 key->level = max( key->level, current_level );
376 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
377 static int grow_subkeys( struct key *key )
379 struct key **new_subkeys;
380 int nb_subkeys;
382 if (key->nb_subkeys)
384 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
385 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
387 set_error( STATUS_NO_MEMORY );
388 return 0;
391 else
393 nb_subkeys = MIN_VALUES;
394 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
396 key->subkeys = new_subkeys;
397 key->nb_subkeys = nb_subkeys;
398 return 1;
401 /* allocate a subkey for a given key, and return its index */
402 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
404 struct key *key;
405 int i;
407 if (parent->last_subkey + 1 == parent->nb_subkeys)
409 /* need to grow the array */
410 if (!grow_subkeys( parent )) return NULL;
412 if ((key = alloc_key( name, modif )) != NULL)
414 key->parent = parent;
415 for (i = ++parent->last_subkey; i > index; i--)
416 parent->subkeys[i] = parent->subkeys[i-1];
417 parent->subkeys[index] = key;
419 return key;
422 /* free a subkey of a given key */
423 static void free_subkey( struct key *parent, int index )
425 struct key *key;
426 int i, nb_subkeys;
428 assert( index >= 0 );
429 assert( index <= parent->last_subkey );
431 key = parent->subkeys[index];
432 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
433 parent->last_subkey--;
434 key->flags |= KEY_DELETED;
435 key->parent = NULL;
436 release_object( key );
438 /* try to shrink the array */
439 nb_subkeys = key->nb_subkeys;
440 if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2)
442 struct key **new_subkeys;
443 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
444 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
445 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
446 key->subkeys = new_subkeys;
447 key->nb_subkeys = nb_subkeys;
451 /* find the named child of a given key and return its index */
452 static struct key *find_subkey( struct key *key, const WCHAR *name, int *index )
454 int i, min, max, res;
456 min = 0;
457 max = key->last_subkey;
458 while (min <= max)
460 i = (min + max) / 2;
461 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
463 *index = i;
464 return key->subkeys[i];
466 if (res > 0) max = i - 1;
467 else min = i + 1;
469 *index = min; /* this is where we should insert it */
470 return NULL;
473 /* open a subkey */
474 static struct key *open_key( struct key *key, const WCHAR *name, size_t maxlen )
476 int index;
477 WCHAR *path;
479 path = get_path_token( name, maxlen );
480 while (*path)
482 if (!(key = find_subkey( key, path, &index )))
484 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
485 break;
487 path = get_path_token( NULL, 0 );
490 if (debug_level > 1) dump_operation( key, NULL, "Open" );
491 if (key) grab_object( key );
492 return key;
495 /* create a subkey */
496 static struct key *create_key( struct key *key, const WCHAR *name, size_t maxlen, WCHAR *class,
497 unsigned int options, time_t modif, int *created )
499 struct key *base;
500 int base_idx, index, flags = 0;
501 WCHAR *path;
503 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
505 set_error( STATUS_KEY_DELETED );
506 return NULL;
508 if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE;
509 else if (key->flags & KEY_VOLATILE)
511 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
512 return NULL;
515 path = get_path_token( name, maxlen );
516 *created = 0;
517 while (*path)
519 struct key *subkey;
520 if (!(subkey = find_subkey( key, path, &index ))) break;
521 key = subkey;
522 path = get_path_token( NULL, 0 );
525 /* create the remaining part */
527 if (!*path) goto done;
528 *created = 1;
529 base = key;
530 base_idx = index;
531 key = alloc_subkey( key, path, index, modif );
532 while (key)
534 key->flags |= flags;
535 path = get_path_token( NULL, 0 );
536 if (!*path) goto done;
537 /* we know the index is always 0 in a new key */
538 key = alloc_subkey( key, path, 0, modif );
540 if (base_idx != -1) free_subkey( base, base_idx );
541 return NULL;
543 done:
544 if (debug_level > 1) dump_operation( key, NULL, "Create" );
545 if (class) key->class = strdupW(class);
546 grab_object( key );
547 return key;
550 /* find a subkey of a given key by its index */
551 static void enum_key( struct key *parent, int index, WCHAR *name, WCHAR *class, time_t *modif )
553 struct key *key;
555 if ((index < 0) || (index > parent->last_subkey)) set_error( STATUS_NO_MORE_ENTRIES );
556 else
558 key = parent->subkeys[index];
559 *modif = key->modif;
560 strcpyW( name, key->name );
561 if (key->class) strcpyW( class, key->class ); /* FIXME: length */
562 else *class = 0;
563 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
567 /* query information about a key */
568 static void query_key( struct key *key, struct query_key_info_request *req )
570 int i, len;
571 int max_subkey = 0, max_class = 0;
572 int max_value = 0, max_data = 0;
574 for (i = 0; i <= key->last_subkey; i++)
576 struct key *subkey = key->subkeys[i];
577 len = strlenW( subkey->name );
578 if (len > max_subkey) max_subkey = len;
579 if (!subkey->class) continue;
580 len = strlenW( subkey->class );
581 if (len > max_class) max_class = len;
583 for (i = 0; i <= key->last_value; i++)
585 len = strlenW( key->values[i].name );
586 if (len > max_value) max_value = len;
587 len = key->values[i].len;
588 if (len > max_data) max_data = len;
590 req->subkeys = key->last_subkey + 1;
591 req->max_subkey = max_subkey;
592 req->max_class = max_class;
593 req->values = key->last_value + 1;
594 req->max_value = max_value;
595 req->max_data = max_data;
596 req->modif = key->modif;
597 strcpyW( req->name, key->name);
598 if (key->class) strcpyW( req->class, key->class ); /* FIXME: length */
599 else req->class[0] = 0;
600 if (debug_level > 1) dump_operation( key, NULL, "Query" );
603 /* delete a key and its values */
604 static void delete_key( struct key *key, const WCHAR *name, size_t maxlen )
606 int index;
607 struct key *parent;
608 WCHAR *path;
610 path = get_path_token( name, maxlen );
611 if (!*path)
613 /* deleting this key, must find parent and index */
614 if (key->flags & KEY_ROOT)
616 set_error( STATUS_ACCESS_DENIED );
617 return;
619 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
621 set_error( STATUS_KEY_DELETED );
622 return;
624 for (index = 0; index <= parent->last_subkey; index++)
625 if (parent->subkeys[index] == key) break;
626 assert( index <= parent->last_subkey );
628 else while (*path)
630 parent = key;
631 if (!(key = find_subkey( parent, path, &index )))
633 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
634 return;
636 path = get_path_token( NULL, 0 );
639 /* we can only delete a key that has no subkeys (FIXME) */
640 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
642 set_error( STATUS_ACCESS_DENIED );
643 return;
645 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
646 free_subkey( parent, index );
647 touch_key( parent );
650 /* try to grow the array of values; return 1 if OK, 0 on error */
651 static int grow_values( struct key *key )
653 struct key_value *new_val;
654 int nb_values;
656 if (key->nb_values)
658 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
659 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
661 set_error( STATUS_NO_MEMORY );
662 return 0;
665 else
667 nb_values = MIN_VALUES;
668 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
670 key->values = new_val;
671 key->nb_values = nb_values;
672 return 1;
675 /* find the named value of a given key and return its index in the array */
676 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
678 int i, min, max, res;
680 min = 0;
681 max = key->last_value;
682 while (min <= max)
684 i = (min + max) / 2;
685 if (!(res = strcmpiW( key->values[i].name, name )))
687 *index = i;
688 return &key->values[i];
690 if (res > 0) max = i - 1;
691 else min = i + 1;
693 *index = min; /* this is where we should insert it */
694 return NULL;
697 /* insert a new value or return a pointer to an existing one */
698 static struct key_value *insert_value( struct key *key, const WCHAR *name )
700 struct key_value *value;
701 WCHAR *new_name;
702 int i, index;
704 if (!(value = find_value( key, name, &index )))
706 /* not found, add it */
707 if (key->last_value + 1 == key->nb_values)
709 if (!grow_values( key )) return NULL;
711 if (!(new_name = strdupW(name))) return NULL;
712 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
713 value = &key->values[index];
714 value->name = new_name;
715 value->len = 0;
716 value->data = NULL;
718 return value;
721 /* set a key value */
722 static void set_value( struct key *key, WCHAR *name, int type, int datalen, void *data )
724 struct key_value *value;
725 void *ptr = NULL;
727 /* first copy the data */
728 if (datalen)
730 if (!(ptr = mem_alloc( datalen ))) return;
731 memcpy( ptr, data, datalen );
734 if (!(value = insert_value( key, name )))
736 if (ptr) free( ptr );
737 return;
739 if (value->data) free( value->data ); /* already existing, free previous data */
740 value->type = type;
741 value->len = datalen;
742 value->data = ptr;
743 touch_key( key );
744 if (debug_level > 1) dump_operation( key, value, "Set" );
747 /* get a key value */
748 static void get_value( struct key *key, WCHAR *name, int *type, int *len, void *data )
750 struct key_value *value;
751 int index;
753 if ((value = find_value( key, name, &index )))
755 *type = value->type;
756 *len = value->len;
757 if (value->data) memcpy( data, value->data, value->len );
758 if (debug_level > 1) dump_operation( key, value, "Get" );
760 else
762 *type = -1;
763 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
767 /* enumerate a key value */
768 static void enum_value( struct key *key, int i, WCHAR *name, int *type, int *len, void *data )
770 struct key_value *value;
772 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
773 else
775 value = &key->values[i];
776 strcpyW( name, value->name );
777 *type = value->type;
778 *len = value->len;
779 if (value->data) memcpy( data, value->data, value->len );
780 if (debug_level > 1) dump_operation( key, value, "Enum" );
784 /* delete a value */
785 static void delete_value( struct key *key, const WCHAR *name )
787 struct key_value *value;
788 int i, index, nb_values;
790 if (!(value = find_value( key, name, &index )))
792 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
793 return;
795 if (debug_level > 1) dump_operation( key, value, "Delete" );
796 free( value->name );
797 if (value->data) free( value->data );
798 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
799 key->last_value--;
800 touch_key( key );
802 /* try to shrink the array */
803 nb_values = key->nb_values;
804 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
806 struct key_value *new_val;
807 nb_values -= nb_values / 3; /* shrink by 33% */
808 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
809 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
810 key->values = new_val;
811 key->nb_values = nb_values;
815 static struct key *get_hkey_obj( int hkey, unsigned int access );
817 static struct key *create_root_key( int hkey )
819 int dummy;
820 struct key *key;
822 switch(hkey)
824 /* the two real root-keys */
825 case HKEY_LOCAL_MACHINE:
827 static const WCHAR name[] = { 'M','A','C','H','I','N','E',0 };
828 key = alloc_key( name, time(NULL) );
830 break;
831 case HKEY_USERS:
833 static const WCHAR name[] = { 'U','S','E','R',0 };
834 key = alloc_key( name, time(NULL) );
836 break;
837 /* special subkeys */
838 case HKEY_CLASSES_ROOT:
840 static const WCHAR name[] =
841 { 'S','O','F','T','W','A','R','E','\\','C','l','a','s','s','e','s',0 };
843 struct key *root = get_hkey_obj( HKEY_LOCAL_MACHINE, 0 );
844 assert( root );
845 key = create_key( root, name, sizeof(name), NULL, 0, time(NULL), &dummy );
846 release_object( root );
848 break;
849 case HKEY_CURRENT_CONFIG:
851 static const WCHAR name[] = {
852 'S','Y','S','T','E','M','\\',
853 'C','U','R','R','E','N','T','C','O','N','T','R','O','L','S','E','T','\\',
854 'H','A','R','D','W','A','R','E','P','R','O','F','I','L','E','S','\\',
855 'C','U','R','R','E','N','T',0};
856 struct key *root = get_hkey_obj( HKEY_LOCAL_MACHINE, 0 );
857 assert( root );
858 key = create_key( root, name, sizeof(name), NULL, 0, time(NULL), &dummy );
859 release_object( root );
861 break;
862 case HKEY_CURRENT_USER:
864 /* get the current user name */
865 int i, len;
866 WCHAR *name;
867 char buffer[10];
868 const char *p;
869 struct passwd *pwd = getpwuid( getuid() );
871 if (pwd) p = pwd->pw_name;
872 else
874 sprintf( buffer, "%ld", (long) getuid() );
875 p = buffer;
877 len = strlen(p);
878 if ((name = mem_alloc( (len+1) * sizeof(WCHAR) )))
880 struct key *root = get_hkey_obj( HKEY_USERS, 0 );
881 assert( root );
882 for (i = 0; i <= len; i++) name[i] = p[i];
883 key = create_key( root, name, (len+1) * sizeof(WCHAR),
884 NULL, 0, time(NULL), &dummy );
885 release_object( root );
886 free( name );
888 else key = NULL;
890 break;
891 /* dynamically generated keys */
892 case HKEY_PERFORMANCE_DATA:
893 case HKEY_DYN_DATA:
894 key = alloc_key( NULL, time(NULL) );
895 break;
896 default:
897 key = NULL;
898 assert(0);
900 if (key)
902 root_keys[hkey - HKEY_ROOT_FIRST] = key;
903 key->flags |= KEY_ROOT;
905 return key;
908 /* get the registry key corresponding to an hkey handle */
909 static struct key *get_hkey_obj( int hkey, unsigned int access )
911 struct key *key;
913 if (IS_ROOT_HKEY(hkey))
915 if (!(key = root_keys[hkey - HKEY_ROOT_FIRST])) key = create_root_key( hkey );
916 grab_object( key );
918 else
919 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
920 return key;
923 /* read a line from the input file */
924 static int read_next_line( struct file_load_info *info )
926 char *newbuf;
927 int newlen, pos = 0;
929 info->line++;
930 for (;;)
932 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
933 return (pos != 0); /* EOF */
934 pos = strlen(info->buffer);
935 if (info->buffer[pos-1] == '\n')
937 /* got a full line */
938 info->buffer[--pos] = 0;
939 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
940 return 1;
942 if (pos < info->len - 1) return 1; /* EOF but something was read */
944 /* need to enlarge the buffer */
945 newlen = info->len + info->len / 2;
946 if (!(newbuf = realloc( info->buffer, newlen )))
948 set_error( STATUS_NO_MEMORY );
949 return -1;
951 info->buffer = newbuf;
952 info->len = newlen;
956 /* make sure the temp buffer holds enough space */
957 static int get_file_tmp_space( struct file_load_info *info, int size )
959 char *tmp;
960 if (info->tmplen >= size) return 1;
961 if (!(tmp = realloc( info->tmp, size )))
963 set_error( STATUS_NO_MEMORY );
964 return 0;
966 info->tmp = tmp;
967 info->tmplen = size;
968 return 1;
971 /* report an error while loading an input file */
972 static void file_read_error( const char *err, struct file_load_info *info )
974 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
977 /* parse an escaped string back into Unicode */
978 /* return the number of chars read from the input, or -1 on output overflow */
979 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
981 int count = sizeof(WCHAR); /* for terminating null */
982 const char *p = src;
983 while (*p && *p != endchar)
985 if (*p != '\\') *dest = (WCHAR)*p++;
986 else
988 p++;
989 switch(*p)
991 case 'a': *dest = '\a'; p++; break;
992 case 'b': *dest = '\b'; p++; break;
993 case 'e': *dest = '\e'; p++; break;
994 case 'f': *dest = '\f'; p++; break;
995 case 'n': *dest = '\n'; p++; break;
996 case 'r': *dest = '\r'; p++; break;
997 case 't': *dest = '\t'; p++; break;
998 case 'v': *dest = '\v'; p++; break;
999 case 'x': /* hex escape */
1000 p++;
1001 if (!isxdigit(*p)) *dest = 'x';
1002 else
1004 *dest = to_hex(*p++);
1005 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1006 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1007 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1009 break;
1010 case '0':
1011 case '1':
1012 case '2':
1013 case '3':
1014 case '4':
1015 case '5':
1016 case '6':
1017 case '7': /* octal escape */
1018 *dest = *p++ - '0';
1019 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1020 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1021 break;
1022 default:
1023 *dest = (WCHAR)*p++;
1024 break;
1027 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1028 dest++;
1030 *dest = 0;
1031 if (!*p) return -1; /* delimiter not found */
1032 *len = count;
1033 return p + 1 - src;
1036 /* convert a data type tag to a value type */
1037 static int get_data_type( const char *buffer, int *type, int *parse_type )
1039 struct data_type { const char *tag; int len; int type; int parse_type; };
1041 static const struct data_type data_types[] =
1042 { /* actual type */ /* type to assume for parsing */
1043 { "\"", 1, REG_SZ, REG_SZ },
1044 { "str:\"", 5, REG_SZ, REG_SZ },
1045 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1046 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1047 { "hex:", 4, REG_BINARY, REG_BINARY },
1048 { "dword:", 6, REG_DWORD, REG_DWORD },
1049 { "hex(", 4, -1, REG_BINARY },
1050 { NULL, }
1053 const struct data_type *ptr;
1054 char *end;
1056 for (ptr = data_types; ptr->tag; ptr++)
1058 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1059 *parse_type = ptr->parse_type;
1060 if ((*type = ptr->type) != -1) return ptr->len;
1061 /* "hex(xx):" is special */
1062 *type = (int)strtoul( buffer + 4, &end, 16 );
1063 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1064 return end + 2 - buffer;
1066 return 0;
1069 /* load and create a key from the input file */
1070 static struct key *load_key( struct key *base, const char *buffer, struct file_load_info *info )
1072 WCHAR *p;
1073 int res, len, modif;
1075 len = strlen(buffer) * sizeof(WCHAR);
1076 if (!get_file_tmp_space( info, len )) return NULL;
1078 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1080 file_read_error( "Malformed key", info );
1081 return NULL;
1083 if (!sscanf( buffer + res, " %d", &modif )) modif = time(NULL);
1085 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') { p++; break; }
1086 return create_key( base, p, len - ((char *)p - info->tmp), NULL, 0, modif, &res );
1089 /* parse a comma-separated list of hex digits */
1090 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1092 const char *p = buffer;
1093 int count = 0;
1094 while (isxdigit(*p))
1096 int val;
1097 char buf[3];
1098 memcpy( buf, p, 2 );
1099 buf[2] = 0;
1100 sscanf( buf, "%x", &val );
1101 if (count++ >= *len) return -1; /* dest buffer overflow */
1102 *dest++ = (unsigned char )val;
1103 p += 2;
1104 if (*p == ',') p++;
1106 *len = count;
1107 return p - buffer;
1110 /* parse a value name and create the corresponding value */
1111 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1112 struct file_load_info *info )
1114 int maxlen = strlen(buffer) * sizeof(WCHAR);
1115 if (!get_file_tmp_space( info, maxlen )) return NULL;
1116 if (buffer[0] == '@')
1118 info->tmp[0] = info->tmp[1] = 0;
1119 *len = 1;
1121 else
1123 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1124 (*len)++; /* for initial quote */
1126 if (buffer[*len] != '=') goto error;
1127 (*len)++;
1128 return insert_value( key, (WCHAR *)info->tmp );
1130 error:
1131 file_read_error( "Malformed value name", info );
1132 return NULL;
1135 /* load a value from the input file */
1136 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1138 DWORD dw;
1139 void *ptr, *newptr;
1140 int maxlen, len, res;
1141 int type, parse_type;
1142 struct key_value *value;
1144 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1145 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1146 buffer += len + res;
1148 switch(parse_type)
1150 case REG_SZ:
1151 len = strlen(buffer) * sizeof(WCHAR);
1152 if (!get_file_tmp_space( info, len )) return 0;
1153 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1154 ptr = info->tmp;
1155 break;
1156 case REG_DWORD:
1157 dw = strtoul( buffer, NULL, 16 );
1158 ptr = &dw;
1159 len = sizeof(dw);
1160 break;
1161 case REG_BINARY: /* hex digits */
1162 len = 0;
1163 for (;;)
1165 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1166 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1167 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1168 len += maxlen;
1169 buffer += res;
1170 while (isspace(*buffer)) buffer++;
1171 if (!*buffer) break;
1172 if (*buffer != '\\') goto error;
1173 if (read_next_line( info) != 1) goto error;
1174 buffer = info->buffer;
1175 while (isspace(*buffer)) buffer++;
1177 ptr = info->tmp;
1178 break;
1179 default:
1180 assert(0);
1181 ptr = NULL; /* keep compiler quiet */
1182 break;
1185 if (!len) newptr = NULL;
1186 else if (!(newptr = memdup( ptr, len ))) return 0;
1188 if (value->data) free( value->data );
1189 value->data = newptr;
1190 value->len = len;
1191 value->type = type;
1192 /* update the key level but not the modification time */
1193 key->level = max( key->level, current_level );
1194 return 1;
1196 error:
1197 file_read_error( "Malformed value", info );
1198 return 0;
1201 /* load all the keys from the input file */
1202 static void load_keys( struct key *key, FILE *f )
1204 struct key *subkey = NULL;
1205 struct file_load_info info;
1206 char *p;
1208 info.file = f;
1209 info.len = 4;
1210 info.tmplen = 4;
1211 info.line = 0;
1212 if (!(info.buffer = mem_alloc( info.len ))) return;
1213 if (!(info.tmp = mem_alloc( info.tmplen )))
1215 free( info.buffer );
1216 return;
1219 if ((read_next_line( &info ) != 1) ||
1220 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1222 set_error( STATUS_NOT_REGISTRY_FILE );
1223 goto done;
1226 while (read_next_line( &info ) == 1)
1228 for (p = info.buffer; *p && isspace(*p); p++);
1229 switch(*p)
1231 case '[': /* new key */
1232 if (subkey) release_object( subkey );
1233 subkey = load_key( key, p + 1, &info );
1234 break;
1235 case '@': /* default value */
1236 case '\"': /* value */
1237 if (subkey) load_value( subkey, p, &info );
1238 else file_read_error( "Value without key", &info );
1239 break;
1240 case '#': /* comment */
1241 case ';': /* comment */
1242 case 0: /* empty line */
1243 break;
1244 default:
1245 file_read_error( "Unrecognized input", &info );
1246 break;
1250 done:
1251 if (subkey) release_object( subkey );
1252 free( info.buffer );
1253 free( info.tmp );
1256 /* load a part of the registry from a file */
1257 static void load_registry( struct key *key, int handle )
1259 struct object *obj;
1260 int fd;
1262 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return;
1263 fd = obj->ops->get_read_fd( obj );
1264 release_object( obj );
1265 if (fd != -1)
1267 FILE *f = fdopen( fd, "r" );
1268 if (f)
1270 load_keys( key, f );
1271 fclose( f );
1273 else file_set_error();
1277 /* update the level of the parents of a key (only needed for the old format) */
1278 static int update_level( struct key *key )
1280 int i;
1281 int max = key->level;
1282 for (i = 0; i <= key->last_subkey; i++)
1284 int sub = update_level( key->subkeys[i] );
1285 if (sub > max) max = sub;
1287 key->level = max;
1288 return max;
1291 /* dump a string to a registry save file in the old v1 format */
1292 static void save_string_v1( LPCWSTR str, FILE *f, int len )
1294 if (!str) return;
1295 while ((len == -1) ? *str : (*str && len--))
1297 if ((*str > 0x7f) || (*str == '\n') || (*str == '='))
1298 fprintf( f, "\\u%04x", *str );
1299 else
1301 if (*str == '\\') fputc( '\\', f );
1302 fputc( (char)*str, f );
1304 str++;
1308 /* save a registry and all its subkeys to a text file in the old v1 format */
1309 static void save_subkeys_v1( struct key *key, int nesting, FILE *f )
1311 int i, j;
1313 if (key->flags & KEY_VOLATILE) return;
1314 if (key->level < saving_level) return;
1315 for (i = 0; i <= key->last_value; i++)
1317 struct key_value *value = &key->values[i];
1318 for (j = nesting; j > 0; j --) fputc( '\t', f );
1319 save_string_v1( value->name, f, -1 );
1320 fprintf( f, "=%d,%d,", value->type, 0 );
1321 if (value->type == REG_SZ || value->type == REG_EXPAND_SZ)
1322 save_string_v1( (LPWSTR)value->data, f, value->len / 2 );
1323 else
1324 for (j = 0; j < value->len; j++)
1325 fprintf( f, "%02x", *((unsigned char *)value->data + j) );
1326 fputc( '\n', f );
1328 for (i = 0; i <= key->last_subkey; i++)
1330 for (j = nesting; j > 0; j --) fputc( '\t', f );
1331 save_string_v1( key->subkeys[i]->name, f, -1 );
1332 fputc( '\n', f );
1333 save_subkeys_v1( key->subkeys[i], nesting + 1, f );
1337 /* save a registry branch to a file handle */
1338 static void save_registry( struct key *key, int handle )
1340 struct object *obj;
1341 int fd;
1343 if (key->flags & KEY_DELETED)
1345 set_error( STATUS_KEY_DELETED );
1346 return;
1348 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return;
1349 fd = obj->ops->get_write_fd( obj );
1350 release_object( obj );
1351 if (fd != -1)
1353 FILE *f = fdopen( fd, "w" );
1354 if (f)
1356 fprintf( f, "WINE REGISTRY Version %d\n", saving_version );
1357 if (saving_version == 2) save_subkeys( key, key, f );
1358 else
1360 update_level( key );
1361 save_subkeys_v1( key, 0, f );
1363 if (fclose( f )) file_set_error();
1365 else
1367 file_set_error();
1368 close( fd );
1373 /* register a key branch for being saved on exit */
1374 static void register_branch_for_saving( struct key *key, const char *path, size_t len )
1376 if (save_branch_count >= MAX_SAVE_BRANCH_INFO)
1378 set_error( STATUS_NO_MORE_ENTRIES );
1379 return;
1381 if (!(save_branch_info[save_branch_count].path = memdup( path, len+1 ))) return;
1382 save_branch_info[save_branch_count].path[len] = 0;
1383 save_branch_info[save_branch_count].key = (struct key *)grab_object( key );
1384 save_branch_count++;
1387 /* save a registry branch to a file */
1388 static int save_branch( struct key *key, const char *path )
1390 char *p, *real, *tmp = NULL;
1391 int fd, count = 0, ret = 0;
1392 FILE *f;
1394 /* get the real path */
1396 if (!(real = malloc( PATH_MAX ))) return 0;
1397 if (!realpath( path, real ))
1399 free( real );
1400 real = NULL;
1402 else path = real;
1404 /* test the file type */
1406 if ((fd = open( path, O_WRONLY )) != -1)
1408 struct stat st;
1409 /* if file is not a regular file or has multiple links,
1410 write directly into it; otherwise use a temp file */
1411 if (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1413 ftruncate( fd, 0 );
1414 goto save;
1416 close( fd );
1419 /* create a temp file in the same directory */
1421 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1422 strcpy( tmp, path );
1423 if ((p = strrchr( tmp, '/' ))) p++;
1424 else p = tmp;
1425 for (;;)
1427 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1428 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1429 if (errno != EEXIST) goto done;
1430 close( fd );
1433 /* now save to it */
1435 save:
1436 if (!(f = fdopen( fd, "w" )))
1438 if (tmp) unlink( tmp );
1439 close( fd );
1440 goto done;
1443 if (debug_level > 1)
1445 fprintf( stderr, "%s: ", path );
1446 dump_operation( key, NULL, "saving" );
1449 fprintf( f, "WINE REGISTRY Version %d\n", saving_version );
1450 if (saving_version == 2) save_subkeys( key, key, f );
1451 else
1453 update_level( key );
1454 save_subkeys_v1( key, 0, f );
1456 ret = !fclose(f);
1458 if (tmp)
1460 /* if successfully written, rename to final name */
1461 if (ret) ret = !rename( tmp, path );
1462 if (!ret) unlink( tmp );
1463 free( tmp );
1466 done:
1467 if (real) free( real );
1468 return ret;
1471 /* periodic saving of the registry */
1472 static void periodic_save( void *arg )
1474 int i;
1475 for (i = 0; i < save_branch_count; i++)
1476 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1477 add_timeout( &next_save_time, save_period );
1478 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1481 /* save the registry and close the top-level keys; used on server exit */
1482 void close_registry(void)
1484 int i;
1486 for (i = 0; i < save_branch_count; i++)
1488 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1490 fprintf( stderr, "wineserver: could not save registry branch to %s",
1491 save_branch_info[i].path );
1492 perror( " " );
1494 release_object( save_branch_info[i].key );
1496 for (i = 0; i < NB_ROOT_KEYS; i++)
1498 if (root_keys[i]) release_object( root_keys[i] );
1503 /* create a registry key */
1504 DECL_HANDLER(create_key)
1506 struct key *key, *parent;
1507 WCHAR *class;
1508 unsigned int access = req->access;
1510 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1511 req->hkey = -1;
1512 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1514 if ((class = req_strdupW( req, req->class )))
1516 if ((key = create_key( parent, req->name, sizeof(req->name), class, req->options,
1517 req->modif, &req->created )))
1519 req->hkey = alloc_handle( current->process, key, access, 0 );
1520 release_object( key );
1522 free( class );
1524 release_object( parent );
1528 /* open a registry key */
1529 DECL_HANDLER(open_key)
1531 struct key *key, *parent;
1532 unsigned int access = req->access;
1534 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1535 req->hkey = -1;
1536 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1538 if ((key = open_key( parent, req->name, sizeof(req->name) )))
1540 req->hkey = alloc_handle( current->process, key, access, 0 );
1541 release_object( key );
1543 release_object( parent );
1547 /* delete a registry key */
1548 DECL_HANDLER(delete_key)
1550 struct key *key;
1552 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1554 delete_key( key, req->name, sizeof(req->name) );
1555 release_object( key );
1559 /* close a registry key */
1560 DECL_HANDLER(close_key)
1562 int hkey = req->hkey;
1563 /* ignore attempts to close a root key */
1564 if (!IS_ROOT_HKEY(hkey)) close_handle( current->process, hkey );
1567 /* enumerate registry subkeys */
1568 DECL_HANDLER(enum_key)
1570 struct key *key;
1572 req->name[0] = req->class[0] = 0;
1573 if ((key = get_hkey_obj( req->hkey, KEY_ENUMERATE_SUB_KEYS )))
1575 enum_key( key, req->index, req->name, req->class, &req->modif );
1576 release_object( key );
1580 /* query information about a registry key */
1581 DECL_HANDLER(query_key_info)
1583 struct key *key;
1585 req->name[0] = req->class[0] = 0;
1586 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1588 query_key( key, req );
1589 release_object( key );
1593 /* set a value of a registry key */
1594 DECL_HANDLER(set_key_value)
1596 struct key *key;
1597 int max = get_req_size( req, req->data, sizeof(req->data[0]) );
1598 int datalen = req->len;
1599 if (datalen > max)
1601 set_error( STATUS_NO_MEMORY ); /* FIXME */
1602 return;
1604 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1606 set_value( key, copy_path( req->name ), req->type, datalen, req->data );
1607 release_object( key );
1611 /* retrieve the value of a registry key */
1612 DECL_HANDLER(get_key_value)
1614 struct key *key;
1616 req->len = 0;
1617 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1619 get_value( key, copy_path( req->name ), &req->type, &req->len, req->data );
1620 release_object( key );
1624 /* enumerate the value of a registry key */
1625 DECL_HANDLER(enum_key_value)
1627 struct key *key;
1629 req->len = 0;
1630 req->name[0] = 0;
1631 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1633 enum_value( key, req->index, req->name, &req->type, &req->len, req->data );
1634 release_object( key );
1638 /* delete a value of a registry key */
1639 DECL_HANDLER(delete_key_value)
1641 WCHAR *name;
1642 struct key *key;
1644 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1646 if ((name = req_strdupW( req, req->name )))
1648 delete_value( key, name );
1649 free( name );
1651 release_object( key );
1655 /* load a registry branch from a file */
1656 DECL_HANDLER(load_registry)
1658 struct key *key;
1660 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1662 /* FIXME: use subkey name */
1663 load_registry( key, req->file );
1664 release_object( key );
1668 /* save a registry branch to a file */
1669 DECL_HANDLER(save_registry)
1671 struct key *key;
1673 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1675 save_registry( key, req->file );
1676 release_object( key );
1680 /* set the current and saving level for the registry */
1681 DECL_HANDLER(set_registry_levels)
1683 current_level = req->current;
1684 saving_level = req->saving;
1685 saving_version = req->version;
1687 /* set periodic save timer */
1689 if (save_timeout_user)
1691 remove_timeout_user( save_timeout_user );
1692 save_timeout_user = NULL;
1694 if ((save_period = req->period))
1696 if (save_period < 10000) save_period = 10000; /* limit rate */
1697 gettimeofday( &next_save_time, 0 );
1698 add_timeout( &next_save_time, save_period );
1699 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1703 /* save a registry branch at server exit */
1704 DECL_HANDLER(save_registry_atexit)
1706 struct key *key;
1708 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1710 register_branch_for_saving( key, req->file, get_req_strlen( req, req->file ) );
1711 release_object( key );