Warning fix.
[wine/multimedia.git] / server / registry.c
blob5905aba998910702c546bf35d2b1066665d9ddd2
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 )
292 static WCHAR buffer[MAX_PATH+1];
294 if (len > sizeof(buffer)-sizeof(buffer[0]))
296 set_error( STATUS_BUFFER_OVERFLOW );
297 return NULL;
299 memcpy( buffer, path, len );
300 buffer[len / sizeof(WCHAR)] = 0;
301 return buffer;
304 /* copy a path from the request buffer, in cases where the length is stored in front of the path */
305 static WCHAR *copy_req_path( void *req, size_t *len )
307 const WCHAR *name_ptr = get_req_data(req);
308 if ((*len = sizeof(WCHAR) + *name_ptr++) > get_req_data_size(req))
310 fatal_protocol_error( current, "copy_req_path: invalid length %d/%d\n",
311 *len, get_req_data_size(req) );
312 return NULL;
314 return copy_path( name_ptr, *len - sizeof(WCHAR) );
317 /* return the next token in a given path */
318 /* returns a pointer to a static buffer, so only useable once per request */
319 static WCHAR *get_path_token( WCHAR *initpath )
321 static WCHAR *path;
322 WCHAR *ret;
324 if (initpath)
326 /* path cannot start with a backslash */
327 if (*initpath == '\\')
329 set_error( STATUS_OBJECT_PATH_INVALID );
330 return NULL;
332 path = initpath;
334 else while (*path == '\\') path++;
336 ret = path;
337 while (*path && *path != '\\') path++;
338 if (*path) *path++ = 0;
339 return ret;
342 /* duplicate a Unicode string from the request buffer */
343 static WCHAR *req_strdupW( const void *req, const WCHAR *str, size_t len )
345 WCHAR *name;
346 if ((name = mem_alloc( len + sizeof(WCHAR) )) != NULL)
348 memcpy( name, str, len );
349 name[len / sizeof(WCHAR)] = 0;
351 return name;
354 /* allocate a key object */
355 static struct key *alloc_key( const WCHAR *name, time_t modif )
357 struct key *key;
358 if ((key = (struct key *)alloc_object( &key_ops, -1 )))
360 key->class = NULL;
361 key->flags = 0;
362 key->last_subkey = -1;
363 key->nb_subkeys = 0;
364 key->subkeys = NULL;
365 key->nb_values = 0;
366 key->last_value = -1;
367 key->values = NULL;
368 key->level = current_level;
369 key->modif = modif;
370 key->parent = NULL;
371 if (!(key->name = strdupW( name )))
373 release_object( key );
374 key = NULL;
377 return key;
380 /* update key modification time */
381 static void touch_key( struct key *key )
383 key->modif = time(NULL);
384 key->level = max( key->level, current_level );
387 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
388 static int grow_subkeys( struct key *key )
390 struct key **new_subkeys;
391 int nb_subkeys;
393 if (key->nb_subkeys)
395 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
396 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
398 set_error( STATUS_NO_MEMORY );
399 return 0;
402 else
404 nb_subkeys = MIN_VALUES;
405 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
407 key->subkeys = new_subkeys;
408 key->nb_subkeys = nb_subkeys;
409 return 1;
412 /* allocate a subkey for a given key, and return its index */
413 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
415 struct key *key;
416 int i;
418 if (parent->last_subkey + 1 == parent->nb_subkeys)
420 /* need to grow the array */
421 if (!grow_subkeys( parent )) return NULL;
423 if ((key = alloc_key( name, modif )) != NULL)
425 key->parent = parent;
426 for (i = ++parent->last_subkey; i > index; i--)
427 parent->subkeys[i] = parent->subkeys[i-1];
428 parent->subkeys[index] = key;
430 return key;
433 /* free a subkey of a given key */
434 static void free_subkey( struct key *parent, int index )
436 struct key *key;
437 int i, nb_subkeys;
439 assert( index >= 0 );
440 assert( index <= parent->last_subkey );
442 key = parent->subkeys[index];
443 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
444 parent->last_subkey--;
445 key->flags |= KEY_DELETED;
446 key->parent = NULL;
447 release_object( key );
449 /* try to shrink the array */
450 nb_subkeys = key->nb_subkeys;
451 if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2)
453 struct key **new_subkeys;
454 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
455 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
456 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
457 key->subkeys = new_subkeys;
458 key->nb_subkeys = nb_subkeys;
462 /* find the named child of a given key and return its index */
463 static struct key *find_subkey( struct key *key, const WCHAR *name, int *index )
465 int i, min, max, res;
467 min = 0;
468 max = key->last_subkey;
469 while (min <= max)
471 i = (min + max) / 2;
472 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
474 *index = i;
475 return key->subkeys[i];
477 if (res > 0) max = i - 1;
478 else min = i + 1;
480 *index = min; /* this is where we should insert it */
481 return NULL;
484 /* open a subkey */
485 /* warning: the key name must be writeable (use copy_path) */
486 static struct key *open_key( struct key *key, WCHAR *name )
488 int index;
489 WCHAR *path;
491 if (!(path = get_path_token( name ))) return NULL;
492 while (*path)
494 if (!(key = find_subkey( key, path, &index )))
496 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
497 break;
499 path = get_path_token( NULL );
502 if (debug_level > 1) dump_operation( key, NULL, "Open" );
503 if (key) grab_object( key );
504 return key;
507 /* create a subkey */
508 /* warning: the key name must be writeable (use copy_path) */
509 static struct key *create_key( struct key *key, WCHAR *name, WCHAR *class,
510 unsigned int options, time_t modif, int *created )
512 struct key *base;
513 int base_idx, index, flags = 0;
514 WCHAR *path;
516 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
518 set_error( STATUS_KEY_DELETED );
519 return NULL;
521 if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE;
522 else if (key->flags & KEY_VOLATILE)
524 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
525 return NULL;
527 if (!modif) modif = time(NULL);
529 if (!(path = get_path_token( name ))) return NULL;
530 *created = 0;
531 while (*path)
533 struct key *subkey;
534 if (!(subkey = find_subkey( key, path, &index ))) break;
535 key = subkey;
536 path = get_path_token( NULL );
539 /* create the remaining part */
541 if (!*path) goto done;
542 *created = 1;
543 base = key;
544 base_idx = index;
545 key = alloc_subkey( key, path, index, modif );
546 while (key)
548 key->flags |= flags;
549 path = get_path_token( NULL );
550 if (!*path) goto done;
551 /* we know the index is always 0 in a new key */
552 key = alloc_subkey( key, path, 0, modif );
554 if (base_idx != -1) free_subkey( base, base_idx );
555 return NULL;
557 done:
558 if (debug_level > 1) dump_operation( key, NULL, "Create" );
559 if (class) key->class = strdupW(class);
560 grab_object( key );
561 return key;
564 /* query information about a key or a subkey */
565 static size_t enum_key( struct key *key, int index, struct enum_key_request *req )
567 int i;
568 size_t len, namelen, classlen;
569 int max_subkey = 0, max_class = 0;
570 int max_value = 0, max_data = 0;
571 WCHAR *data = get_req_data(req);
573 if (index != -1) /* -1 means use the specified key directly */
575 if ((index < 0) || (index > key->last_subkey))
577 set_error( STATUS_NO_MORE_ENTRIES );
578 return 0;
580 key = key->subkeys[index];
583 if (req->full)
585 for (i = 0; i <= key->last_subkey; i++)
587 struct key *subkey = key->subkeys[i];
588 len = strlenW( subkey->name );
589 if (len > max_subkey) max_subkey = len;
590 if (!subkey->class) continue;
591 len = strlenW( subkey->class );
592 if (len > max_class) max_class = len;
594 for (i = 0; i <= key->last_value; i++)
596 len = strlenW( key->values[i].name );
597 if (len > max_value) max_value = len;
598 len = key->values[i].len;
599 if (len > max_data) max_data = len;
601 req->max_subkey = max_subkey;
602 req->max_class = max_class;
603 req->max_value = max_value;
604 req->max_data = max_data;
606 else
608 req->max_subkey = 0;
609 req->max_class = 0;
610 req->max_value = 0;
611 req->max_data = 0;
613 req->subkeys = key->last_subkey + 1;
614 req->values = key->last_value + 1;
615 req->modif = key->modif;
617 namelen = strlenW(key->name) * sizeof(WCHAR);
618 classlen = key->class ? strlenW(key->class) * sizeof(WCHAR) : 0;
620 len = namelen + classlen + sizeof(WCHAR);
621 if (len > get_req_data_size(req))
623 len = get_req_data_size(req);
624 if (len < sizeof(WCHAR)) return 0;
627 *data++ = namelen;
628 len -= sizeof(WCHAR);
629 if (len > namelen)
631 memcpy( data, key->name, namelen );
632 memcpy( (char *)data + namelen, key->class, min(classlen,len-namelen) );
634 else memcpy( data, key->name, len );
636 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
637 return len + sizeof(WCHAR);
640 /* delete a key and its values */
641 static void delete_key( struct key *key )
643 int index;
644 struct key *parent;
646 /* must find parent and index */
647 if (key->flags & KEY_ROOT)
649 set_error( STATUS_ACCESS_DENIED );
650 return;
652 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
654 set_error( STATUS_KEY_DELETED );
655 return;
657 for (index = 0; index <= parent->last_subkey; index++)
658 if (parent->subkeys[index] == key) break;
659 assert( index <= parent->last_subkey );
661 /* we can only delete a key that has no subkeys (FIXME) */
662 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
664 set_error( STATUS_ACCESS_DENIED );
665 return;
667 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
668 free_subkey( parent, index );
669 touch_key( parent );
672 /* try to grow the array of values; return 1 if OK, 0 on error */
673 static int grow_values( struct key *key )
675 struct key_value *new_val;
676 int nb_values;
678 if (key->nb_values)
680 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
681 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
683 set_error( STATUS_NO_MEMORY );
684 return 0;
687 else
689 nb_values = MIN_VALUES;
690 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
692 key->values = new_val;
693 key->nb_values = nb_values;
694 return 1;
697 /* find the named value of a given key and return its index in the array */
698 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
700 int i, min, max, res;
702 min = 0;
703 max = key->last_value;
704 while (min <= max)
706 i = (min + max) / 2;
707 if (!(res = strcmpiW( key->values[i].name, name )))
709 *index = i;
710 return &key->values[i];
712 if (res > 0) max = i - 1;
713 else min = i + 1;
715 *index = min; /* this is where we should insert it */
716 return NULL;
719 /* insert a new value or return a pointer to an existing one */
720 static struct key_value *insert_value( struct key *key, const WCHAR *name )
722 struct key_value *value;
723 WCHAR *new_name;
724 int i, index;
726 if (!(value = find_value( key, name, &index )))
728 /* not found, add it */
729 if (key->last_value + 1 == key->nb_values)
731 if (!grow_values( key )) return NULL;
733 if (!(new_name = strdupW(name))) return NULL;
734 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
735 value = &key->values[index];
736 value->name = new_name;
737 value->len = 0;
738 value->data = NULL;
740 return value;
743 /* set a key value */
744 static void set_value( struct key *key, WCHAR *name, int type, unsigned int total_len,
745 unsigned int offset, unsigned int data_len, const void *data )
747 struct key_value *value;
748 void *ptr = NULL;
750 if (data_len + offset > total_len)
752 set_error( STATUS_INVALID_PARAMETER );
753 return;
756 if (offset) /* adding data to an existing value */
758 int index;
759 if (!(value = find_value( key, name, &index )))
761 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
762 return;
764 if (value->len != total_len)
766 set_error( STATUS_INVALID_PARAMETER );
767 return;
769 memcpy( (char *)value->data + offset, data, data_len );
770 if (debug_level > 1) dump_operation( key, value, "Set" );
771 return;
774 /* first copy the data */
775 if (total_len)
777 if (!(ptr = mem_alloc( total_len ))) return;
778 memcpy( ptr, data, data_len );
779 if (data_len < total_len) memset( (char *)ptr + data_len, 0, total_len - data_len );
782 if (!(value = insert_value( key, name )))
784 if (ptr) free( ptr );
785 return;
787 if (value->data) free( value->data ); /* already existing, free previous data */
788 value->type = type;
789 value->len = total_len;
790 value->data = ptr;
791 touch_key( key );
792 if (debug_level > 1) dump_operation( key, value, "Set" );
795 /* get a key value */
796 static size_t get_value( struct key *key, const WCHAR *name, unsigned int offset,
797 unsigned int maxlen, int *type, int *len, void *data )
799 struct key_value *value;
800 int index;
801 size_t ret = 0;
803 if ((value = find_value( key, name, &index )))
805 *type = value->type;
806 *len = value->len;
807 if (value->data && offset < value->len)
809 if (maxlen > value->len - offset) maxlen = value->len - offset;
810 memcpy( data, (char *)value->data + offset, maxlen );
811 ret = maxlen;
813 if (debug_level > 1) dump_operation( key, value, "Get" );
815 else
817 *type = -1;
818 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
820 return ret;
823 /* enumerate a key value */
824 static size_t enum_value( struct key *key, int i, unsigned int offset,
825 unsigned int maxlen, int *type, int *len, void *data )
827 struct key_value *value;
828 size_t ret = 0;
830 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
831 else
833 WCHAR *name_ptr = data;
834 value = &key->values[i];
835 *type = value->type;
836 *len = value->len;
838 if (maxlen >= sizeof(WCHAR))
840 size_t name_len = 0;
842 /* copy the name only the first time (offset==0),
843 * otherwise store an empty name in the buffer
845 maxlen -= sizeof(WCHAR);
846 ret += sizeof(WCHAR);
847 if (!offset)
849 name_len = strlenW( value->name ) * sizeof(WCHAR);
850 if (name_len > maxlen) name_len = maxlen;
852 *name_ptr++ = name_len;
853 memcpy( name_ptr, value->name, name_len );
854 maxlen -= name_len;
855 ret += name_len;
856 data = (char *)name_ptr + name_len;
858 if (value->data && offset < value->len)
860 if (maxlen > value->len - offset) maxlen = value->len - offset;
861 memcpy( data, (char *)value->data + offset, maxlen );
862 ret += maxlen;
865 if (debug_level > 1) dump_operation( key, value, "Enum" );
867 return ret;
870 /* delete a value */
871 static void delete_value( struct key *key, const WCHAR *name )
873 struct key_value *value;
874 int i, index, nb_values;
876 if (!(value = find_value( key, name, &index )))
878 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
879 return;
881 if (debug_level > 1) dump_operation( key, value, "Delete" );
882 free( value->name );
883 if (value->data) free( value->data );
884 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
885 key->last_value--;
886 touch_key( key );
888 /* try to shrink the array */
889 nb_values = key->nb_values;
890 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
892 struct key_value *new_val;
893 nb_values -= nb_values / 3; /* shrink by 33% */
894 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
895 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
896 key->values = new_val;
897 key->nb_values = nb_values;
901 static struct key *create_root_key( handle_t hkey )
903 WCHAR keyname[80];
904 int i, dummy;
905 struct key *key;
906 const char *p;
908 p = special_root_names[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST];
909 i = 0;
910 while (*p) keyname[i++] = *p++;
912 if (hkey == (handle_t)HKEY_CURRENT_USER) /* this one is special */
914 /* get the current user name */
915 char buffer[10];
916 struct passwd *pwd = getpwuid( getuid() );
918 if (pwd) p = pwd->pw_name;
919 else
921 sprintf( buffer, "%ld", (long) getuid() );
922 p = buffer;
924 while (*p && i < sizeof(keyname)/sizeof(WCHAR)-1) keyname[i++] = *p++;
926 keyname[i++] = 0;
928 if ((key = create_key( root_key, keyname, NULL, 0, time(NULL), &dummy )))
930 special_root_keys[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST] = key;
931 key->flags |= KEY_ROOT;
933 return key;
936 /* get the registry key corresponding to an hkey handle */
937 static struct key *get_hkey_obj( handle_t hkey, unsigned int access )
939 struct key *key;
941 if (!hkey) return (struct key *)grab_object( root_key );
942 if (IS_SPECIAL_ROOT_HKEY(hkey))
944 if (!(key = special_root_keys[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST]))
945 key = create_root_key( hkey );
946 else
947 grab_object( key );
949 else
950 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
951 return key;
954 /* read a line from the input file */
955 static int read_next_line( struct file_load_info *info )
957 char *newbuf;
958 int newlen, pos = 0;
960 info->line++;
961 for (;;)
963 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
964 return (pos != 0); /* EOF */
965 pos = strlen(info->buffer);
966 if (info->buffer[pos-1] == '\n')
968 /* got a full line */
969 info->buffer[--pos] = 0;
970 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
971 return 1;
973 if (pos < info->len - 1) return 1; /* EOF but something was read */
975 /* need to enlarge the buffer */
976 newlen = info->len + info->len / 2;
977 if (!(newbuf = realloc( info->buffer, newlen )))
979 set_error( STATUS_NO_MEMORY );
980 return -1;
982 info->buffer = newbuf;
983 info->len = newlen;
987 /* make sure the temp buffer holds enough space */
988 static int get_file_tmp_space( struct file_load_info *info, int size )
990 char *tmp;
991 if (info->tmplen >= size) return 1;
992 if (!(tmp = realloc( info->tmp, size )))
994 set_error( STATUS_NO_MEMORY );
995 return 0;
997 info->tmp = tmp;
998 info->tmplen = size;
999 return 1;
1002 /* report an error while loading an input file */
1003 static void file_read_error( const char *err, struct file_load_info *info )
1005 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
1008 /* parse an escaped string back into Unicode */
1009 /* return the number of chars read from the input, or -1 on output overflow */
1010 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
1012 int count = sizeof(WCHAR); /* for terminating null */
1013 const char *p = src;
1014 while (*p && *p != endchar)
1016 if (*p != '\\') *dest = (WCHAR)*p++;
1017 else
1019 p++;
1020 switch(*p)
1022 case 'a': *dest = '\a'; p++; break;
1023 case 'b': *dest = '\b'; p++; break;
1024 case 'e': *dest = '\e'; p++; break;
1025 case 'f': *dest = '\f'; p++; break;
1026 case 'n': *dest = '\n'; p++; break;
1027 case 'r': *dest = '\r'; p++; break;
1028 case 't': *dest = '\t'; p++; break;
1029 case 'v': *dest = '\v'; p++; break;
1030 case 'x': /* hex escape */
1031 p++;
1032 if (!isxdigit(*p)) *dest = 'x';
1033 else
1035 *dest = to_hex(*p++);
1036 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1037 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1038 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1040 break;
1041 case '0':
1042 case '1':
1043 case '2':
1044 case '3':
1045 case '4':
1046 case '5':
1047 case '6':
1048 case '7': /* octal escape */
1049 *dest = *p++ - '0';
1050 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1051 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1052 break;
1053 default:
1054 *dest = (WCHAR)*p++;
1055 break;
1058 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1059 dest++;
1061 *dest = 0;
1062 if (!*p) return -1; /* delimiter not found */
1063 *len = count;
1064 return p + 1 - src;
1067 /* convert a data type tag to a value type */
1068 static int get_data_type( const char *buffer, int *type, int *parse_type )
1070 struct data_type { const char *tag; int len; int type; int parse_type; };
1072 static const struct data_type data_types[] =
1073 { /* actual type */ /* type to assume for parsing */
1074 { "\"", 1, REG_SZ, REG_SZ },
1075 { "str:\"", 5, REG_SZ, REG_SZ },
1076 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1077 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1078 { "hex:", 4, REG_BINARY, REG_BINARY },
1079 { "dword:", 6, REG_DWORD, REG_DWORD },
1080 { "hex(", 4, -1, REG_BINARY },
1081 { NULL, 0, 0, 0 }
1084 const struct data_type *ptr;
1085 char *end;
1087 for (ptr = data_types; ptr->tag; ptr++)
1089 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1090 *parse_type = ptr->parse_type;
1091 if ((*type = ptr->type) != -1) return ptr->len;
1092 /* "hex(xx):" is special */
1093 *type = (int)strtoul( buffer + 4, &end, 16 );
1094 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1095 return end + 2 - buffer;
1097 return 0;
1100 /* load and create a key from the input file */
1101 static struct key *load_key( struct key *base, const char *buffer, unsigned int options,
1102 int prefix_len, struct file_load_info *info )
1104 WCHAR *p, *name;
1105 int res, len, modif;
1107 len = strlen(buffer) * sizeof(WCHAR);
1108 if (!get_file_tmp_space( info, len )) return NULL;
1110 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1112 file_read_error( "Malformed key", info );
1113 return NULL;
1115 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = time(NULL);
1117 p = (WCHAR *)info->tmp;
1118 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1120 if (!*p)
1122 if (prefix_len > 1)
1124 file_read_error( "Malformed key", info );
1125 return NULL;
1127 /* empty key name, return base key */
1128 return (struct key *)grab_object( base );
1130 if (!(name = copy_path( p, len - ((char *)p - info->tmp) )))
1132 file_read_error( "Key is too long", info );
1133 return NULL;
1135 return create_key( base, name, NULL, options, modif, &res );
1138 /* parse a comma-separated list of hex digits */
1139 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1141 const char *p = buffer;
1142 int count = 0;
1143 while (isxdigit(*p))
1145 int val;
1146 char buf[3];
1147 memcpy( buf, p, 2 );
1148 buf[2] = 0;
1149 sscanf( buf, "%x", &val );
1150 if (count++ >= *len) return -1; /* dest buffer overflow */
1151 *dest++ = (unsigned char )val;
1152 p += 2;
1153 if (*p == ',') p++;
1155 *len = count;
1156 return p - buffer;
1159 /* parse a value name and create the corresponding value */
1160 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1161 struct file_load_info *info )
1163 int maxlen = strlen(buffer) * sizeof(WCHAR);
1164 if (!get_file_tmp_space( info, maxlen )) return NULL;
1165 if (buffer[0] == '@')
1167 info->tmp[0] = info->tmp[1] = 0;
1168 *len = 1;
1170 else
1172 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1173 (*len)++; /* for initial quote */
1175 while (isspace(buffer[*len])) (*len)++;
1176 if (buffer[*len] != '=') goto error;
1177 (*len)++;
1178 while (isspace(buffer[*len])) (*len)++;
1179 return insert_value( key, (WCHAR *)info->tmp );
1181 error:
1182 file_read_error( "Malformed value name", info );
1183 return NULL;
1186 /* load a value from the input file */
1187 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1189 DWORD dw;
1190 void *ptr, *newptr;
1191 int maxlen, len, res;
1192 int type, parse_type;
1193 struct key_value *value;
1195 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1196 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1197 buffer += len + res;
1199 switch(parse_type)
1201 case REG_SZ:
1202 len = strlen(buffer) * sizeof(WCHAR);
1203 if (!get_file_tmp_space( info, len )) return 0;
1204 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1205 ptr = info->tmp;
1206 break;
1207 case REG_DWORD:
1208 dw = strtoul( buffer, NULL, 16 );
1209 ptr = &dw;
1210 len = sizeof(dw);
1211 break;
1212 case REG_BINARY: /* hex digits */
1213 len = 0;
1214 for (;;)
1216 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1217 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1218 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1219 len += maxlen;
1220 buffer += res;
1221 while (isspace(*buffer)) buffer++;
1222 if (!*buffer) break;
1223 if (*buffer != '\\') goto error;
1224 if (read_next_line( info) != 1) goto error;
1225 buffer = info->buffer;
1226 while (isspace(*buffer)) buffer++;
1228 ptr = info->tmp;
1229 break;
1230 default:
1231 assert(0);
1232 ptr = NULL; /* keep compiler quiet */
1233 break;
1236 if (!len) newptr = NULL;
1237 else if (!(newptr = memdup( ptr, len ))) return 0;
1239 if (value->data) free( value->data );
1240 value->data = newptr;
1241 value->len = len;
1242 value->type = type;
1243 /* update the key level but not the modification time */
1244 key->level = max( key->level, current_level );
1245 return 1;
1247 error:
1248 file_read_error( "Malformed value", info );
1249 return 0;
1252 /* return the length (in path elements) of name that is part of the key name */
1253 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1254 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1256 WCHAR *p;
1257 int res;
1258 int len = strlen(name) * sizeof(WCHAR);
1259 if (!get_file_tmp_space( info, len )) return 0;
1261 if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
1263 file_read_error( "Malformed key", info );
1264 return 0;
1266 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
1267 *p = 0;
1268 for (res = 1; key != root_key; res++)
1270 if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
1271 key = key->parent;
1273 if (key == root_key) res = 0; /* no matching name */
1274 return res;
1277 /* load all the keys from the input file */
1278 static void load_keys( struct key *key, FILE *f )
1280 struct key *subkey = NULL;
1281 struct file_load_info info;
1282 char *p;
1283 unsigned int options = 0;
1284 int prefix_len = -1; /* number of key name prefixes to skip */
1286 if (key->flags & KEY_VOLATILE) options |= REG_OPTION_VOLATILE;
1288 info.file = f;
1289 info.len = 4;
1290 info.tmplen = 4;
1291 info.line = 0;
1292 if (!(info.buffer = mem_alloc( info.len ))) return;
1293 if (!(info.tmp = mem_alloc( info.tmplen )))
1295 free( info.buffer );
1296 return;
1299 if ((read_next_line( &info ) != 1) ||
1300 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1302 set_error( STATUS_NOT_REGISTRY_FILE );
1303 goto done;
1306 while (read_next_line( &info ) == 1)
1308 for (p = info.buffer; *p && isspace(*p); p++);
1309 switch(*p)
1311 case '[': /* new key */
1312 if (subkey) release_object( subkey );
1313 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1314 if (!(subkey = load_key( key, p + 1, options, prefix_len, &info )))
1315 file_read_error( "Error creating key", &info );
1316 break;
1317 case '@': /* default value */
1318 case '\"': /* value */
1319 if (subkey) load_value( subkey, p, &info );
1320 else file_read_error( "Value without key", &info );
1321 break;
1322 case '#': /* comment */
1323 case ';': /* comment */
1324 case 0: /* empty line */
1325 break;
1326 default:
1327 file_read_error( "Unrecognized input", &info );
1328 break;
1332 done:
1333 if (subkey) release_object( subkey );
1334 free( info.buffer );
1335 free( info.tmp );
1338 /* load a part of the registry from a file */
1339 static void load_registry( struct key *key, handle_t handle )
1341 struct object *obj;
1342 int fd;
1344 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return;
1345 fd = dup(obj->ops->get_fd( obj ));
1346 release_object( obj );
1347 if (fd != -1)
1349 FILE *f = fdopen( fd, "r" );
1350 if (f)
1352 load_keys( key, f );
1353 fclose( f );
1355 else file_set_error();
1359 /* registry initialisation */
1360 void init_registry(void)
1362 static const WCHAR root_name[] = { 0 };
1363 static const WCHAR config_name[] =
1364 { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
1365 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 };
1367 char *filename;
1368 const char *config;
1369 FILE *f;
1371 /* create the root key */
1372 root_key = alloc_key( root_name, time(NULL) );
1373 assert( root_key );
1374 root_key->flags |= KEY_ROOT;
1376 /* load the config file */
1377 config = get_config_dir();
1378 if (!(filename = malloc( strlen(config) + 8 ))) fatal_error( "out of memory\n" );
1379 strcpy( filename, config );
1380 strcat( filename, "/config" );
1381 if ((f = fopen( filename, "r" )))
1383 struct key *key;
1384 int dummy;
1386 /* create the config key */
1387 if (!(key = create_key( root_key, copy_path( config_name, sizeof(config_name) ),
1388 NULL, 0, time(NULL), &dummy )))
1389 fatal_error( "could not create config key\n" );
1390 key->flags |= KEY_VOLATILE;
1392 load_keys( key, f );
1393 fclose( f );
1394 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1395 fatal_error( "%s is not a valid registry file\n", filename );
1396 if (get_error())
1397 fatal_error( "loading %s failed with error %x\n", filename, get_error() );
1399 release_object( key );
1401 free( filename );
1404 /* update the level of the parents of a key (only needed for the old format) */
1405 static int update_level( struct key *key )
1407 int i;
1408 int max = key->level;
1409 for (i = 0; i <= key->last_subkey; i++)
1411 int sub = update_level( key->subkeys[i] );
1412 if (sub > max) max = sub;
1414 key->level = max;
1415 return max;
1418 /* save a registry branch to a file */
1419 static void save_all_subkeys( struct key *key, FILE *f )
1421 fprintf( f, "WINE REGISTRY Version 2\n" );
1422 fprintf( f, ";; All keys relative to " );
1423 dump_path( key, NULL, f );
1424 fprintf( f, "\n" );
1425 save_subkeys( key, key, f );
1428 /* save a registry branch to a file handle */
1429 static void save_registry( struct key *key, handle_t handle )
1431 struct object *obj;
1432 int fd;
1434 if (key->flags & KEY_DELETED)
1436 set_error( STATUS_KEY_DELETED );
1437 return;
1439 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return;
1440 fd = dup(obj->ops->get_fd( obj ));
1441 release_object( obj );
1442 if (fd != -1)
1444 FILE *f = fdopen( fd, "w" );
1445 if (f)
1447 save_all_subkeys( key, f );
1448 if (fclose( f )) file_set_error();
1450 else
1452 file_set_error();
1453 close( fd );
1458 /* register a key branch for being saved on exit */
1459 static void register_branch_for_saving( struct key *key, const char *path, size_t len )
1461 if (save_branch_count >= MAX_SAVE_BRANCH_INFO)
1463 set_error( STATUS_NO_MORE_ENTRIES );
1464 return;
1466 if (!len || !(save_branch_info[save_branch_count].path = memdup( path, len ))) return;
1467 save_branch_info[save_branch_count].path[len - 1] = 0;
1468 save_branch_info[save_branch_count].key = (struct key *)grab_object( key );
1469 save_branch_count++;
1472 /* save a registry branch to a file */
1473 static int save_branch( struct key *key, const char *path )
1475 char *p, *real, *tmp = NULL;
1476 int fd, count = 0, ret = 0;
1477 FILE *f;
1479 /* get the real path */
1481 if (!(real = malloc( PATH_MAX ))) return 0;
1482 if (!realpath( path, real ))
1484 free( real );
1485 real = NULL;
1487 else path = real;
1489 /* test the file type */
1491 if ((fd = open( path, O_WRONLY )) != -1)
1493 struct stat st;
1494 /* if file is not a regular file or has multiple links,
1495 write directly into it; otherwise use a temp file */
1496 if (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1498 ftruncate( fd, 0 );
1499 goto save;
1501 close( fd );
1504 /* create a temp file in the same directory */
1506 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1507 strcpy( tmp, path );
1508 if ((p = strrchr( tmp, '/' ))) p++;
1509 else p = tmp;
1510 for (;;)
1512 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1513 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1514 if (errno != EEXIST) goto done;
1515 close( fd );
1518 /* now save to it */
1520 save:
1521 if (!(f = fdopen( fd, "w" )))
1523 if (tmp) unlink( tmp );
1524 close( fd );
1525 goto done;
1528 if (debug_level > 1)
1530 fprintf( stderr, "%s: ", path );
1531 dump_operation( key, NULL, "saving" );
1534 save_all_subkeys( key, f );
1535 ret = !fclose(f);
1537 if (tmp)
1539 /* if successfully written, rename to final name */
1540 if (ret) ret = !rename( tmp, path );
1541 if (!ret) unlink( tmp );
1542 free( tmp );
1545 done:
1546 if (real) free( real );
1547 return ret;
1550 /* periodic saving of the registry */
1551 static void periodic_save( void *arg )
1553 int i;
1554 for (i = 0; i < save_branch_count; i++)
1555 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1556 add_timeout( &next_save_time, save_period );
1557 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1560 /* save the registry and close the top-level keys; used on server exit */
1561 void close_registry(void)
1563 int i;
1565 for (i = 0; i < save_branch_count; i++)
1567 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1569 fprintf( stderr, "wineserver: could not save registry branch to %s",
1570 save_branch_info[i].path );
1571 perror( " " );
1573 release_object( save_branch_info[i].key );
1575 release_object( root_key );
1579 /* create a registry key */
1580 DECL_HANDLER(create_key)
1582 struct key *key = NULL, *parent;
1583 unsigned int access = req->access;
1584 WCHAR *name, *class;
1585 size_t len;
1587 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1588 req->hkey = 0;
1589 if (!(name = copy_req_path( req, &len ))) return;
1590 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1592 if (len == get_req_data_size(req)) /* no class specified */
1594 key = create_key( parent, name, NULL, req->options, req->modif, &req->created );
1596 else
1598 const WCHAR *class_ptr = (WCHAR *)((char *)get_req_data(req) + len);
1600 if ((class = req_strdupW( req, class_ptr, get_req_data_size(req) - len )))
1602 key = create_key( parent, name, class, req->options,
1603 req->modif, &req->created );
1604 free( class );
1607 if (key)
1609 req->hkey = alloc_handle( current->process, key, access, 0 );
1610 release_object( key );
1612 release_object( parent );
1616 /* open a registry key */
1617 DECL_HANDLER(open_key)
1619 struct key *key, *parent;
1620 unsigned int access = req->access;
1622 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1623 req->hkey = 0;
1624 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1626 WCHAR *name = copy_path( get_req_data(req), get_req_data_size(req) );
1627 if (name && (key = open_key( parent, name )))
1629 req->hkey = alloc_handle( current->process, key, access, 0 );
1630 release_object( key );
1632 release_object( parent );
1636 /* delete a registry key */
1637 DECL_HANDLER(delete_key)
1639 struct key *key;
1641 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1643 delete_key( key );
1644 release_object( key );
1648 /* enumerate registry subkeys */
1649 DECL_HANDLER(enum_key)
1651 struct key *key;
1652 size_t len = 0;
1654 if ((key = get_hkey_obj( req->hkey,
1655 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1657 len = enum_key( key, req->index, req );
1658 release_object( key );
1660 set_req_data_size( req, len );
1663 /* set a value of a registry key */
1664 DECL_HANDLER(set_key_value)
1666 struct key *key;
1667 WCHAR *name;
1668 size_t len;
1670 if (!(name = copy_req_path( req, &len ))) return;
1671 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1673 size_t datalen = get_req_data_size(req) - len;
1674 const char *data = (char *)get_req_data(req) + len;
1676 set_value( key, name, req->type, req->total, req->offset, datalen, data );
1677 release_object( key );
1681 /* retrieve the value of a registry key */
1682 DECL_HANDLER(get_key_value)
1684 struct key *key;
1685 WCHAR *name;
1686 size_t len = 0, tmp;
1688 req->len = 0;
1689 if (!(name = copy_req_path( req, &tmp ))) return;
1690 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1692 len = get_value( key, name, req->offset, get_req_data_size(req),
1693 &req->type, &req->len, get_req_data(req) );
1694 release_object( key );
1696 set_req_data_size( req, len );
1699 /* enumerate the value of a registry key */
1700 DECL_HANDLER(enum_key_value)
1702 struct key *key;
1703 size_t len = 0;
1705 req->len = 0;
1706 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1708 len = enum_value( key, req->index, req->offset, get_req_data_size(req),
1709 &req->type, &req->len, get_req_data(req) );
1710 release_object( key );
1712 set_req_data_size( req, len );
1715 /* delete a value of a registry key */
1716 DECL_HANDLER(delete_key_value)
1718 WCHAR *name;
1719 struct key *key;
1721 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1723 if ((name = req_strdupW( req, get_req_data(req), get_req_data_size(req) )))
1725 delete_value( key, name );
1726 free( name );
1728 release_object( key );
1732 /* load a registry branch from a file */
1733 DECL_HANDLER(load_registry)
1735 struct key *key;
1737 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1739 /* FIXME: use subkey name */
1740 load_registry( key, req->file );
1741 release_object( key );
1745 /* save a registry branch to a file */
1746 DECL_HANDLER(save_registry)
1748 struct key *key;
1750 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1752 save_registry( key, req->file );
1753 release_object( key );
1757 /* set the current and saving level for the registry */
1758 DECL_HANDLER(set_registry_levels)
1760 current_level = req->current;
1761 saving_level = req->saving;
1763 /* set periodic save timer */
1765 if (save_timeout_user)
1767 remove_timeout_user( save_timeout_user );
1768 save_timeout_user = NULL;
1770 if ((save_period = req->period))
1772 if (save_period < 10000) save_period = 10000; /* limit rate */
1773 gettimeofday( &next_save_time, 0 );
1774 add_timeout( &next_save_time, save_period );
1775 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1779 /* save a registry branch at server exit */
1780 DECL_HANDLER(save_registry_atexit)
1782 struct key *key;
1784 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1786 register_branch_for_saving( key, get_req_data(req), get_req_data_size(req) );
1787 release_object( key );