Compile fix if HAVE_OSS is not defined.
[wine.git] / server / registry.c
blob4fa5a4ebf4ca19f48d5f1b30e73f524c7fc48928
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) (((h) >= HKEY_SPECIAL_ROOT_FIRST) && ((h) <= HKEY_SPECIAL_ROOT_LAST))
75 static struct key *special_root_keys[NB_SPECIAL_ROOT_KEYS];
77 /* the real root key */
78 static struct key *root_key;
80 /* the special root key names */
81 static const char * const special_root_names[NB_SPECIAL_ROOT_KEYS] =
83 "Machine\\Software\\Classes", /* HKEY_CLASSES_ROOT */
84 "User\\", /* we append the user name dynamically */ /* HKEY_CURRENT_USER */
85 "Machine", /* HKEY_LOCAL_MACHINE */
86 "User", /* HKEY_USERS */
87 "PerfData", /* HKEY_PERFORMANCE_DATA */
88 "Machine\\System\\CurrentControlSet\\HardwareProfiles\\Current", /* HKEY_CURRENT_CONFIG */
89 "DynData" /* HKEY_DYN_DATA */
93 /* keys saving level */
94 /* current_level is the level that is put into all newly created or modified keys */
95 /* saving_level is the minimum level that a key needs in order to get saved */
96 static int current_level;
97 static int saving_level;
99 static struct timeval next_save_time; /* absolute time of next periodic save */
100 static int save_period; /* delay between periodic saves (ms) */
101 static struct timeout_user *save_timeout_user; /* saving timer */
103 /* information about where to save a registry branch */
104 struct save_branch_info
106 struct key *key;
107 char *path;
110 #define MAX_SAVE_BRANCH_INFO 8
111 static int save_branch_count;
112 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
115 /* information about a file being loaded */
116 struct file_load_info
118 FILE *file; /* input file */
119 char *buffer; /* line buffer */
120 int len; /* buffer length */
121 int line; /* current input line */
122 char *tmp; /* temp buffer to use while parsing input */
123 int tmplen; /* length of temp buffer */
127 static void key_dump( struct object *obj, int verbose );
128 static void key_destroy( struct object *obj );
130 static const struct object_ops key_ops =
132 sizeof(struct key), /* size */
133 key_dump, /* dump */
134 no_add_queue, /* add_queue */
135 NULL, /* remove_queue */
136 NULL, /* signaled */
137 NULL, /* satisfied */
138 NULL, /* get_poll_events */
139 NULL, /* poll_event */
140 no_get_fd, /* get_fd */
141 no_flush, /* flush */
142 no_get_file_info, /* get_file_info */
143 key_destroy /* destroy */
148 * The registry text file format v2 used by this code is similar to the one
149 * used by REGEDIT import/export functionality, with the following differences:
150 * - strings and key names can contain \x escapes for Unicode
151 * - key names use escapes too in order to support Unicode
152 * - the modification time optionally follows the key name
153 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
156 static inline char to_hex( char ch )
158 if (isdigit(ch)) return ch - '0';
159 return tolower(ch) - 'a' + 10;
162 /* dump the full path of a key */
163 static void dump_path( struct key *key, struct key *base, FILE *f )
165 if (key->parent && key->parent != base)
167 dump_path( key->parent, base, f );
168 fprintf( f, "\\\\" );
170 dump_strW( key->name, strlenW(key->name), f, "[]" );
173 /* dump a value to a text file */
174 static void dump_value( struct key_value *value, FILE *f )
176 int i, count;
178 if (value->name[0])
180 fputc( '\"', f );
181 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
182 count += fprintf( f, "\"=" );
184 else count = fprintf( f, "@=" );
186 switch(value->type)
188 case REG_SZ:
189 case REG_EXPAND_SZ:
190 case REG_MULTI_SZ:
191 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
192 fputc( '\"', f );
193 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
194 fputc( '\"', f );
195 break;
196 case REG_DWORD:
197 if (value->len == sizeof(DWORD))
199 DWORD dw;
200 memcpy( &dw, value->data, sizeof(DWORD) );
201 fprintf( f, "dword:%08lx", dw );
202 break;
204 /* else fall through */
205 default:
206 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
207 else count += fprintf( f, "hex(%x):", value->type );
208 for (i = 0; i < value->len; i++)
210 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
211 if (i < value->len-1)
213 fputc( ',', f );
214 if (++count > 76)
216 fprintf( f, "\\\n " );
217 count = 2;
221 break;
223 fputc( '\n', f );
226 /* save a registry and all its subkeys to a text file */
227 static void save_subkeys( struct key *key, struct key *base, FILE *f )
229 int i;
231 if (key->flags & KEY_VOLATILE) return;
232 /* save key if it has the proper level, and has either some values or no subkeys */
233 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
234 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
236 fprintf( f, "\n[" );
237 if (key != base) dump_path( key, base, f );
238 fprintf( f, "] %ld\n", key->modif );
239 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
241 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
244 static void dump_operation( struct key *key, struct key_value *value, const char *op )
246 fprintf( stderr, "%s key ", op );
247 if (key) dump_path( key, NULL, stderr );
248 else fprintf( stderr, "ERROR" );
249 if (value)
251 fprintf( stderr, " value ");
252 dump_value( value, stderr );
254 else fprintf( stderr, "\n" );
257 static void key_dump( struct object *obj, int verbose )
259 struct key *key = (struct key *)obj;
260 assert( obj->ops == &key_ops );
261 fprintf( stderr, "Key flags=%x ", key->flags );
262 dump_path( key, NULL, stderr );
263 fprintf( stderr, "\n" );
266 static void key_destroy( struct object *obj )
268 int i;
269 struct key *key = (struct key *)obj;
270 assert( obj->ops == &key_ops );
272 if (key->name) free( key->name );
273 if (key->class) free( key->class );
274 for (i = 0; i <= key->last_value; i++)
276 free( key->values[i].name );
277 if (key->values[i].data) free( key->values[i].data );
279 for (i = 0; i <= key->last_subkey; i++)
281 key->subkeys[i]->parent = NULL;
282 release_object( key->subkeys[i] );
286 /* duplicate a key path from the request buffer */
287 /* returns a pointer to a static buffer, so only useable once per request */
288 static WCHAR *copy_path( const WCHAR *path, size_t len )
290 static WCHAR buffer[MAX_PATH+1];
292 if (len > sizeof(buffer)-sizeof(buffer[0]))
294 set_error( STATUS_BUFFER_OVERFLOW );
295 return NULL;
297 memcpy( buffer, path, len );
298 buffer[len / sizeof(WCHAR)] = 0;
299 return buffer;
302 /* copy a path from the request buffer, in cases where the length is stored in front of the path */
303 static WCHAR *copy_req_path( void *req, size_t *len )
305 const WCHAR *name_ptr = get_req_data(req);
306 if ((*len = sizeof(WCHAR) + *name_ptr++) > get_req_data_size(req))
308 fatal_protocol_error( current, "copy_req_path: invalid length %d/%d\n",
309 *len, get_req_data_size(req) );
310 return NULL;
312 return copy_path( name_ptr, *len - sizeof(WCHAR) );
315 /* return the next token in a given path */
316 /* returns a pointer to a static buffer, so only useable once per request */
317 static WCHAR *get_path_token( WCHAR *initpath )
319 static WCHAR *path;
320 WCHAR *ret;
322 if (initpath)
324 /* path cannot start with a backslash */
325 if (*initpath == '\\')
327 set_error( STATUS_OBJECT_PATH_INVALID );
328 return NULL;
330 path = initpath;
332 else while (*path == '\\') path++;
334 ret = path;
335 while (*path && *path != '\\') path++;
336 if (*path) *path++ = 0;
337 return ret;
340 /* duplicate a Unicode string from the request buffer */
341 static WCHAR *req_strdupW( const void *req, const WCHAR *str, size_t len )
343 WCHAR *name;
344 if ((name = mem_alloc( len + sizeof(WCHAR) )) != NULL)
346 memcpy( name, str, len );
347 name[len / sizeof(WCHAR)] = 0;
349 return name;
352 /* allocate a key object */
353 static struct key *alloc_key( const WCHAR *name, time_t modif )
355 struct key *key;
356 if ((key = (struct key *)alloc_object( &key_ops, -1 )))
358 key->class = NULL;
359 key->flags = 0;
360 key->last_subkey = -1;
361 key->nb_subkeys = 0;
362 key->subkeys = NULL;
363 key->nb_values = 0;
364 key->last_value = -1;
365 key->values = NULL;
366 key->level = current_level;
367 key->modif = modif;
368 key->parent = NULL;
369 if (!(key->name = strdupW( name )))
371 release_object( key );
372 key = NULL;
375 return key;
378 /* update key modification time */
379 static void touch_key( struct key *key )
381 key->modif = time(NULL);
382 key->level = max( key->level, current_level );
385 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
386 static int grow_subkeys( struct key *key )
388 struct key **new_subkeys;
389 int nb_subkeys;
391 if (key->nb_subkeys)
393 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
394 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
396 set_error( STATUS_NO_MEMORY );
397 return 0;
400 else
402 nb_subkeys = MIN_VALUES;
403 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
405 key->subkeys = new_subkeys;
406 key->nb_subkeys = nb_subkeys;
407 return 1;
410 /* allocate a subkey for a given key, and return its index */
411 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
413 struct key *key;
414 int i;
416 if (parent->last_subkey + 1 == parent->nb_subkeys)
418 /* need to grow the array */
419 if (!grow_subkeys( parent )) return NULL;
421 if ((key = alloc_key( name, modif )) != NULL)
423 key->parent = parent;
424 for (i = ++parent->last_subkey; i > index; i--)
425 parent->subkeys[i] = parent->subkeys[i-1];
426 parent->subkeys[index] = key;
428 return key;
431 /* free a subkey of a given key */
432 static void free_subkey( struct key *parent, int index )
434 struct key *key;
435 int i, nb_subkeys;
437 assert( index >= 0 );
438 assert( index <= parent->last_subkey );
440 key = parent->subkeys[index];
441 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
442 parent->last_subkey--;
443 key->flags |= KEY_DELETED;
444 key->parent = NULL;
445 release_object( key );
447 /* try to shrink the array */
448 nb_subkeys = key->nb_subkeys;
449 if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2)
451 struct key **new_subkeys;
452 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
453 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
454 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
455 key->subkeys = new_subkeys;
456 key->nb_subkeys = nb_subkeys;
460 /* find the named child of a given key and return its index */
461 static struct key *find_subkey( struct key *key, const WCHAR *name, int *index )
463 int i, min, max, res;
465 min = 0;
466 max = key->last_subkey;
467 while (min <= max)
469 i = (min + max) / 2;
470 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
472 *index = i;
473 return key->subkeys[i];
475 if (res > 0) max = i - 1;
476 else min = i + 1;
478 *index = min; /* this is where we should insert it */
479 return NULL;
482 /* open a subkey */
483 /* warning: the key name must be writeable (use copy_path) */
484 static struct key *open_key( struct key *key, WCHAR *name )
486 int index;
487 WCHAR *path;
489 if (!(path = get_path_token( name ))) return NULL;
490 while (*path)
492 if (!(key = find_subkey( key, path, &index )))
494 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
495 break;
497 path = get_path_token( NULL );
500 if (debug_level > 1) dump_operation( key, NULL, "Open" );
501 if (key) grab_object( key );
502 return key;
505 /* create a subkey */
506 /* warning: the key name must be writeable (use copy_path) */
507 static struct key *create_key( struct key *key, WCHAR *name, WCHAR *class,
508 unsigned int options, time_t modif, int *created )
510 struct key *base;
511 int base_idx, index, flags = 0;
512 WCHAR *path;
514 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
516 set_error( STATUS_KEY_DELETED );
517 return NULL;
519 if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE;
520 else if (key->flags & KEY_VOLATILE)
522 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
523 return NULL;
525 if (!modif) modif = time(NULL);
527 if (!(path = get_path_token( name ))) return NULL;
528 *created = 0;
529 while (*path)
531 struct key *subkey;
532 if (!(subkey = find_subkey( key, path, &index ))) break;
533 key = subkey;
534 path = get_path_token( NULL );
537 /* create the remaining part */
539 if (!*path) goto done;
540 *created = 1;
541 base = key;
542 base_idx = index;
543 key = alloc_subkey( key, path, index, modif );
544 while (key)
546 key->flags |= flags;
547 path = get_path_token( NULL );
548 if (!*path) goto done;
549 /* we know the index is always 0 in a new key */
550 key = alloc_subkey( key, path, 0, modif );
552 if (base_idx != -1) free_subkey( base, base_idx );
553 return NULL;
555 done:
556 if (debug_level > 1) dump_operation( key, NULL, "Create" );
557 if (class) key->class = strdupW(class);
558 grab_object( key );
559 return key;
562 /* query information about a key or a subkey */
563 static size_t enum_key( struct key *key, int index, struct enum_key_request *req )
565 int i;
566 size_t len, namelen, classlen;
567 int max_subkey = 0, max_class = 0;
568 int max_value = 0, max_data = 0;
569 WCHAR *data = get_req_data(req);
571 if (index != -1) /* -1 means use the specified key directly */
573 if ((index < 0) || (index > key->last_subkey))
575 set_error( STATUS_NO_MORE_ENTRIES );
576 return 0;
578 key = key->subkeys[index];
581 if (req->full)
583 for (i = 0; i <= key->last_subkey; i++)
585 struct key *subkey = key->subkeys[i];
586 len = strlenW( subkey->name );
587 if (len > max_subkey) max_subkey = len;
588 if (!subkey->class) continue;
589 len = strlenW( subkey->class );
590 if (len > max_class) max_class = len;
592 for (i = 0; i <= key->last_value; i++)
594 len = strlenW( key->values[i].name );
595 if (len > max_value) max_value = len;
596 len = key->values[i].len;
597 if (len > max_data) max_data = len;
599 req->max_subkey = max_subkey;
600 req->max_class = max_class;
601 req->max_value = max_value;
602 req->max_data = max_data;
604 else
606 req->max_subkey = 0;
607 req->max_class = 0;
608 req->max_value = 0;
609 req->max_data = 0;
611 req->subkeys = key->last_subkey + 1;
612 req->values = key->last_value + 1;
613 req->modif = key->modif;
615 namelen = strlenW(key->name) * sizeof(WCHAR);
616 classlen = key->class ? strlenW(key->class) * sizeof(WCHAR) : 0;
618 len = namelen + classlen + sizeof(WCHAR);
619 if (len > get_req_data_size(req))
621 len = get_req_data_size(req);
622 if (len < sizeof(WCHAR)) return 0;
625 *data++ = namelen;
626 len -= sizeof(WCHAR);
627 if (len > namelen)
629 memcpy( data, key->name, namelen );
630 memcpy( (char *)data + namelen, key->class, min(classlen,len-namelen) );
632 else memcpy( data, key->name, len );
634 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
635 return len + sizeof(WCHAR);
638 /* delete a key and its values */
639 static void delete_key( struct key *key )
641 int index;
642 struct key *parent;
644 /* must find parent and index */
645 if (key->flags & KEY_ROOT)
647 set_error( STATUS_ACCESS_DENIED );
648 return;
650 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
652 set_error( STATUS_KEY_DELETED );
653 return;
655 for (index = 0; index <= parent->last_subkey; index++)
656 if (parent->subkeys[index] == key) break;
657 assert( index <= parent->last_subkey );
659 /* we can only delete a key that has no subkeys (FIXME) */
660 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
662 set_error( STATUS_ACCESS_DENIED );
663 return;
665 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
666 free_subkey( parent, index );
667 touch_key( parent );
670 /* try to grow the array of values; return 1 if OK, 0 on error */
671 static int grow_values( struct key *key )
673 struct key_value *new_val;
674 int nb_values;
676 if (key->nb_values)
678 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
679 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
681 set_error( STATUS_NO_MEMORY );
682 return 0;
685 else
687 nb_values = MIN_VALUES;
688 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
690 key->values = new_val;
691 key->nb_values = nb_values;
692 return 1;
695 /* find the named value of a given key and return its index in the array */
696 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
698 int i, min, max, res;
700 min = 0;
701 max = key->last_value;
702 while (min <= max)
704 i = (min + max) / 2;
705 if (!(res = strcmpiW( key->values[i].name, name )))
707 *index = i;
708 return &key->values[i];
710 if (res > 0) max = i - 1;
711 else min = i + 1;
713 *index = min; /* this is where we should insert it */
714 return NULL;
717 /* insert a new value or return a pointer to an existing one */
718 static struct key_value *insert_value( struct key *key, const WCHAR *name )
720 struct key_value *value;
721 WCHAR *new_name;
722 int i, index;
724 if (!(value = find_value( key, name, &index )))
726 /* not found, add it */
727 if (key->last_value + 1 == key->nb_values)
729 if (!grow_values( key )) return NULL;
731 if (!(new_name = strdupW(name))) return NULL;
732 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
733 value = &key->values[index];
734 value->name = new_name;
735 value->len = 0;
736 value->data = NULL;
738 return value;
741 /* set a key value */
742 static void set_value( struct key *key, WCHAR *name, int type, unsigned int total_len,
743 unsigned int offset, unsigned int data_len, const void *data )
745 struct key_value *value;
746 void *ptr = NULL;
748 if (data_len + offset > total_len)
750 set_error( STATUS_INVALID_PARAMETER );
751 return;
754 if (offset) /* adding data to an existing value */
756 int index;
757 if (!(value = find_value( key, name, &index )))
759 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
760 return;
762 if (value->len != total_len)
764 set_error( STATUS_INVALID_PARAMETER );
765 return;
767 memcpy( (char *)value->data + offset, data, data_len );
768 if (debug_level > 1) dump_operation( key, value, "Set" );
769 return;
772 /* first copy the data */
773 if (total_len)
775 if (!(ptr = mem_alloc( total_len ))) return;
776 memcpy( ptr, data, data_len );
777 if (data_len < total_len) memset( (char *)ptr + data_len, 0, total_len - data_len );
780 if (!(value = insert_value( key, name )))
782 if (ptr) free( ptr );
783 return;
785 if (value->data) free( value->data ); /* already existing, free previous data */
786 value->type = type;
787 value->len = total_len;
788 value->data = ptr;
789 touch_key( key );
790 if (debug_level > 1) dump_operation( key, value, "Set" );
793 /* get a key value */
794 static size_t get_value( struct key *key, const WCHAR *name, unsigned int offset,
795 unsigned int maxlen, int *type, int *len, void *data )
797 struct key_value *value;
798 int index;
799 size_t ret = 0;
801 if ((value = find_value( key, name, &index )))
803 *type = value->type;
804 *len = value->len;
805 if (value->data && offset < value->len)
807 if (maxlen > value->len - offset) maxlen = value->len - offset;
808 memcpy( data, (char *)value->data + offset, maxlen );
809 ret = maxlen;
811 if (debug_level > 1) dump_operation( key, value, "Get" );
813 else
815 *type = -1;
816 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
818 return ret;
821 /* enumerate a key value */
822 static size_t enum_value( struct key *key, int i, unsigned int offset,
823 unsigned int maxlen, int *type, int *len, void *data )
825 struct key_value *value;
826 size_t ret = 0;
828 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
829 else
831 WCHAR *name_ptr = data;
832 value = &key->values[i];
833 *type = value->type;
834 *len = value->len;
836 if (maxlen >= sizeof(WCHAR))
838 size_t name_len = 0;
840 /* copy the name only the first time (offset==0),
841 * otherwise store an empty name in the buffer
843 maxlen -= sizeof(WCHAR);
844 ret += sizeof(WCHAR);
845 if (!offset)
847 name_len = strlenW( value->name ) * sizeof(WCHAR);
848 if (name_len > maxlen) name_len = maxlen;
850 *name_ptr++ = name_len;
851 memcpy( name_ptr, value->name, name_len );
852 maxlen -= name_len;
853 ret += name_len;
854 data = (char *)name_ptr + name_len;
856 if (value->data && offset < value->len)
858 if (maxlen > value->len - offset) maxlen = value->len - offset;
859 memcpy( data, (char *)value->data + offset, maxlen );
860 ret += maxlen;
863 if (debug_level > 1) dump_operation( key, value, "Enum" );
865 return ret;
868 /* delete a value */
869 static void delete_value( struct key *key, const WCHAR *name )
871 struct key_value *value;
872 int i, index, nb_values;
874 if (!(value = find_value( key, name, &index )))
876 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
877 return;
879 if (debug_level > 1) dump_operation( key, value, "Delete" );
880 free( value->name );
881 if (value->data) free( value->data );
882 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
883 key->last_value--;
884 touch_key( key );
886 /* try to shrink the array */
887 nb_values = key->nb_values;
888 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
890 struct key_value *new_val;
891 nb_values -= nb_values / 3; /* shrink by 33% */
892 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
893 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
894 key->values = new_val;
895 key->nb_values = nb_values;
899 static struct key *create_root_key( int hkey )
901 WCHAR keyname[80];
902 int i, dummy;
903 struct key *key;
904 const char *p;
906 p = special_root_names[hkey - HKEY_SPECIAL_ROOT_FIRST];
907 i = 0;
908 while (*p) keyname[i++] = *p++;
910 if (hkey == HKEY_CURRENT_USER) /* this one is special */
912 /* get the current user name */
913 char buffer[10];
914 struct passwd *pwd = getpwuid( getuid() );
916 if (pwd) p = pwd->pw_name;
917 else
919 sprintf( buffer, "%ld", (long) getuid() );
920 p = buffer;
922 while (*p && i < sizeof(keyname)/sizeof(WCHAR)-1) keyname[i++] = *p++;
924 keyname[i++] = 0;
926 if ((key = create_key( root_key, keyname, NULL, 0, time(NULL), &dummy )))
928 special_root_keys[hkey - HKEY_SPECIAL_ROOT_FIRST] = key;
929 key->flags |= KEY_ROOT;
931 return key;
934 /* get the registry key corresponding to an hkey handle */
935 static struct key *get_hkey_obj( int hkey, unsigned int access )
937 struct key *key;
939 if (!hkey) return (struct key *)grab_object( root_key );
940 if (IS_SPECIAL_ROOT_HKEY(hkey))
942 if (!(key = special_root_keys[hkey - HKEY_SPECIAL_ROOT_FIRST]))
943 key = create_root_key( hkey );
944 else
945 grab_object( key );
947 else
948 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
949 return key;
952 /* read a line from the input file */
953 static int read_next_line( struct file_load_info *info )
955 char *newbuf;
956 int newlen, pos = 0;
958 info->line++;
959 for (;;)
961 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
962 return (pos != 0); /* EOF */
963 pos = strlen(info->buffer);
964 if (info->buffer[pos-1] == '\n')
966 /* got a full line */
967 info->buffer[--pos] = 0;
968 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
969 return 1;
971 if (pos < info->len - 1) return 1; /* EOF but something was read */
973 /* need to enlarge the buffer */
974 newlen = info->len + info->len / 2;
975 if (!(newbuf = realloc( info->buffer, newlen )))
977 set_error( STATUS_NO_MEMORY );
978 return -1;
980 info->buffer = newbuf;
981 info->len = newlen;
985 /* make sure the temp buffer holds enough space */
986 static int get_file_tmp_space( struct file_load_info *info, int size )
988 char *tmp;
989 if (info->tmplen >= size) return 1;
990 if (!(tmp = realloc( info->tmp, size )))
992 set_error( STATUS_NO_MEMORY );
993 return 0;
995 info->tmp = tmp;
996 info->tmplen = size;
997 return 1;
1000 /* report an error while loading an input file */
1001 static void file_read_error( const char *err, struct file_load_info *info )
1003 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
1006 /* parse an escaped string back into Unicode */
1007 /* return the number of chars read from the input, or -1 on output overflow */
1008 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
1010 int count = sizeof(WCHAR); /* for terminating null */
1011 const char *p = src;
1012 while (*p && *p != endchar)
1014 if (*p != '\\') *dest = (WCHAR)*p++;
1015 else
1017 p++;
1018 switch(*p)
1020 case 'a': *dest = '\a'; p++; break;
1021 case 'b': *dest = '\b'; p++; break;
1022 case 'e': *dest = '\e'; p++; break;
1023 case 'f': *dest = '\f'; p++; break;
1024 case 'n': *dest = '\n'; p++; break;
1025 case 'r': *dest = '\r'; p++; break;
1026 case 't': *dest = '\t'; p++; break;
1027 case 'v': *dest = '\v'; p++; break;
1028 case 'x': /* hex escape */
1029 p++;
1030 if (!isxdigit(*p)) *dest = 'x';
1031 else
1033 *dest = to_hex(*p++);
1034 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1035 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1036 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1038 break;
1039 case '0':
1040 case '1':
1041 case '2':
1042 case '3':
1043 case '4':
1044 case '5':
1045 case '6':
1046 case '7': /* octal escape */
1047 *dest = *p++ - '0';
1048 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1049 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1050 break;
1051 default:
1052 *dest = (WCHAR)*p++;
1053 break;
1056 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1057 dest++;
1059 *dest = 0;
1060 if (!*p) return -1; /* delimiter not found */
1061 *len = count;
1062 return p + 1 - src;
1065 /* convert a data type tag to a value type */
1066 static int get_data_type( const char *buffer, int *type, int *parse_type )
1068 struct data_type { const char *tag; int len; int type; int parse_type; };
1070 static const struct data_type data_types[] =
1071 { /* actual type */ /* type to assume for parsing */
1072 { "\"", 1, REG_SZ, REG_SZ },
1073 { "str:\"", 5, REG_SZ, REG_SZ },
1074 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1075 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1076 { "hex:", 4, REG_BINARY, REG_BINARY },
1077 { "dword:", 6, REG_DWORD, REG_DWORD },
1078 { "hex(", 4, -1, REG_BINARY },
1079 { NULL, 0, 0, 0 }
1082 const struct data_type *ptr;
1083 char *end;
1085 for (ptr = data_types; ptr->tag; ptr++)
1087 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1088 *parse_type = ptr->parse_type;
1089 if ((*type = ptr->type) != -1) return ptr->len;
1090 /* "hex(xx):" is special */
1091 *type = (int)strtoul( buffer + 4, &end, 16 );
1092 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1093 return end + 2 - buffer;
1095 return 0;
1098 /* load and create a key from the input file */
1099 static struct key *load_key( struct key *base, const char *buffer, unsigned int options,
1100 int prefix_len, struct file_load_info *info )
1102 WCHAR *p, *name;
1103 int res, len, modif;
1105 len = strlen(buffer) * sizeof(WCHAR);
1106 if (!get_file_tmp_space( info, len )) return NULL;
1108 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1110 file_read_error( "Malformed key", info );
1111 return NULL;
1113 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = time(NULL);
1115 p = (WCHAR *)info->tmp;
1116 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1118 if (!*p)
1120 if (prefix_len > 1)
1122 file_read_error( "Malformed key", info );
1123 return NULL;
1125 /* empty key name, return base key */
1126 return (struct key *)grab_object( base );
1128 if (!(name = copy_path( p, len - ((char *)p - info->tmp) )))
1130 file_read_error( "Key is too long", info );
1131 return NULL;
1133 return create_key( base, name, NULL, options, modif, &res );
1136 /* parse a comma-separated list of hex digits */
1137 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1139 const char *p = buffer;
1140 int count = 0;
1141 while (isxdigit(*p))
1143 int val;
1144 char buf[3];
1145 memcpy( buf, p, 2 );
1146 buf[2] = 0;
1147 sscanf( buf, "%x", &val );
1148 if (count++ >= *len) return -1; /* dest buffer overflow */
1149 *dest++ = (unsigned char )val;
1150 p += 2;
1151 if (*p == ',') p++;
1153 *len = count;
1154 return p - buffer;
1157 /* parse a value name and create the corresponding value */
1158 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1159 struct file_load_info *info )
1161 int maxlen = strlen(buffer) * sizeof(WCHAR);
1162 if (!get_file_tmp_space( info, maxlen )) return NULL;
1163 if (buffer[0] == '@')
1165 info->tmp[0] = info->tmp[1] = 0;
1166 *len = 1;
1168 else
1170 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1171 (*len)++; /* for initial quote */
1173 while (isspace(buffer[*len])) (*len)++;
1174 if (buffer[*len] != '=') goto error;
1175 (*len)++;
1176 while (isspace(buffer[*len])) (*len)++;
1177 return insert_value( key, (WCHAR *)info->tmp );
1179 error:
1180 file_read_error( "Malformed value name", info );
1181 return NULL;
1184 /* load a value from the input file */
1185 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1187 DWORD dw;
1188 void *ptr, *newptr;
1189 int maxlen, len, res;
1190 int type, parse_type;
1191 struct key_value *value;
1193 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1194 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1195 buffer += len + res;
1197 switch(parse_type)
1199 case REG_SZ:
1200 len = strlen(buffer) * sizeof(WCHAR);
1201 if (!get_file_tmp_space( info, len )) return 0;
1202 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1203 ptr = info->tmp;
1204 break;
1205 case REG_DWORD:
1206 dw = strtoul( buffer, NULL, 16 );
1207 ptr = &dw;
1208 len = sizeof(dw);
1209 break;
1210 case REG_BINARY: /* hex digits */
1211 len = 0;
1212 for (;;)
1214 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1215 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1216 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1217 len += maxlen;
1218 buffer += res;
1219 while (isspace(*buffer)) buffer++;
1220 if (!*buffer) break;
1221 if (*buffer != '\\') goto error;
1222 if (read_next_line( info) != 1) goto error;
1223 buffer = info->buffer;
1224 while (isspace(*buffer)) buffer++;
1226 ptr = info->tmp;
1227 break;
1228 default:
1229 assert(0);
1230 ptr = NULL; /* keep compiler quiet */
1231 break;
1234 if (!len) newptr = NULL;
1235 else if (!(newptr = memdup( ptr, len ))) return 0;
1237 if (value->data) free( value->data );
1238 value->data = newptr;
1239 value->len = len;
1240 value->type = type;
1241 /* update the key level but not the modification time */
1242 key->level = max( key->level, current_level );
1243 return 1;
1245 error:
1246 file_read_error( "Malformed value", info );
1247 return 0;
1250 /* return the length (in path elements) of name that is part of the key name */
1251 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1252 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1254 WCHAR *p;
1255 int res;
1256 int len = strlen(name) * sizeof(WCHAR);
1257 if (!get_file_tmp_space( info, len )) return 0;
1259 if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
1261 file_read_error( "Malformed key", info );
1262 return 0;
1264 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
1265 *p = 0;
1266 for (res = 1; key != root_key; res++)
1268 if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
1269 key = key->parent;
1271 if (key == root_key) res = 0; /* no matching name */
1272 return res;
1275 /* load all the keys from the input file */
1276 static void load_keys( struct key *key, FILE *f )
1278 struct key *subkey = NULL;
1279 struct file_load_info info;
1280 char *p;
1281 unsigned int options = 0;
1282 int prefix_len = -1; /* number of key name prefixes to skip */
1284 if (key->flags & KEY_VOLATILE) options |= REG_OPTION_VOLATILE;
1286 info.file = f;
1287 info.len = 4;
1288 info.tmplen = 4;
1289 info.line = 0;
1290 if (!(info.buffer = mem_alloc( info.len ))) return;
1291 if (!(info.tmp = mem_alloc( info.tmplen )))
1293 free( info.buffer );
1294 return;
1297 if ((read_next_line( &info ) != 1) ||
1298 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1300 set_error( STATUS_NOT_REGISTRY_FILE );
1301 goto done;
1304 while (read_next_line( &info ) == 1)
1306 for (p = info.buffer; *p && isspace(*p); p++);
1307 switch(*p)
1309 case '[': /* new key */
1310 if (subkey) release_object( subkey );
1311 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1312 if (!(subkey = load_key( key, p + 1, options, prefix_len, &info )))
1313 file_read_error( "Error creating key", &info );
1314 break;
1315 case '@': /* default value */
1316 case '\"': /* value */
1317 if (subkey) load_value( subkey, p, &info );
1318 else file_read_error( "Value without key", &info );
1319 break;
1320 case '#': /* comment */
1321 case ';': /* comment */
1322 case 0: /* empty line */
1323 break;
1324 default:
1325 file_read_error( "Unrecognized input", &info );
1326 break;
1330 done:
1331 if (subkey) release_object( subkey );
1332 free( info.buffer );
1333 free( info.tmp );
1336 /* load a part of the registry from a file */
1337 static void load_registry( struct key *key, int handle )
1339 struct object *obj;
1340 int fd;
1342 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return;
1343 fd = dup(obj->ops->get_fd( obj ));
1344 release_object( obj );
1345 if (fd != -1)
1347 FILE *f = fdopen( fd, "r" );
1348 if (f)
1350 load_keys( key, f );
1351 fclose( f );
1353 else file_set_error();
1357 /* registry initialisation */
1358 void init_registry(void)
1360 static const WCHAR root_name[] = { 0 };
1361 static const WCHAR config_name[] =
1362 { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
1363 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 };
1365 char *filename;
1366 const char *config;
1367 FILE *f;
1369 /* create the root key */
1370 root_key = alloc_key( root_name, time(NULL) );
1371 assert( root_key );
1372 root_key->flags |= KEY_ROOT;
1374 /* load the config file */
1375 config = get_config_dir();
1376 if (!(filename = malloc( strlen(config) + 8 ))) fatal_error( "out of memory\n" );
1377 strcpy( filename, config );
1378 strcat( filename, "/config" );
1379 if ((f = fopen( filename, "r" )))
1381 struct key *key;
1382 int dummy;
1384 /* create the config key */
1385 if (!(key = create_key( root_key, copy_path( config_name, sizeof(config_name) ),
1386 NULL, 0, time(NULL), &dummy )))
1387 fatal_error( "could not create config key\n" );
1388 key->flags |= KEY_VOLATILE;
1390 load_keys( key, f );
1391 fclose( f );
1392 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1393 fatal_error( "%s is not a valid registry file\n", filename );
1394 if (get_error())
1395 fatal_error( "loading %s failed with error %x\n", filename, get_error() );
1397 release_object( key );
1399 free( filename );
1402 /* update the level of the parents of a key (only needed for the old format) */
1403 static int update_level( struct key *key )
1405 int i;
1406 int max = key->level;
1407 for (i = 0; i <= key->last_subkey; i++)
1409 int sub = update_level( key->subkeys[i] );
1410 if (sub > max) max = sub;
1412 key->level = max;
1413 return max;
1416 /* save a registry branch to a file */
1417 static void save_all_subkeys( struct key *key, FILE *f )
1419 fprintf( f, "WINE REGISTRY Version 2\n" );
1420 fprintf( f, ";; All keys relative to " );
1421 dump_path( key, NULL, f );
1422 fprintf( f, "\n" );
1423 save_subkeys( key, key, f );
1426 /* save a registry branch to a file handle */
1427 static void save_registry( struct key *key, int handle )
1429 struct object *obj;
1430 int fd;
1432 if (key->flags & KEY_DELETED)
1434 set_error( STATUS_KEY_DELETED );
1435 return;
1437 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return;
1438 fd = dup(obj->ops->get_fd( obj ));
1439 release_object( obj );
1440 if (fd != -1)
1442 FILE *f = fdopen( fd, "w" );
1443 if (f)
1445 save_all_subkeys( key, f );
1446 if (fclose( f )) file_set_error();
1448 else
1450 file_set_error();
1451 close( fd );
1456 /* register a key branch for being saved on exit */
1457 static void register_branch_for_saving( struct key *key, const char *path, size_t len )
1459 if (save_branch_count >= MAX_SAVE_BRANCH_INFO)
1461 set_error( STATUS_NO_MORE_ENTRIES );
1462 return;
1464 if (!len || !(save_branch_info[save_branch_count].path = memdup( path, len ))) return;
1465 save_branch_info[save_branch_count].path[len - 1] = 0;
1466 save_branch_info[save_branch_count].key = (struct key *)grab_object( key );
1467 save_branch_count++;
1470 /* save a registry branch to a file */
1471 static int save_branch( struct key *key, const char *path )
1473 char *p, *real, *tmp = NULL;
1474 int fd, count = 0, ret = 0;
1475 FILE *f;
1477 /* get the real path */
1479 if (!(real = malloc( PATH_MAX ))) return 0;
1480 if (!realpath( path, real ))
1482 free( real );
1483 real = NULL;
1485 else path = real;
1487 /* test the file type */
1489 if ((fd = open( path, O_WRONLY )) != -1)
1491 struct stat st;
1492 /* if file is not a regular file or has multiple links,
1493 write directly into it; otherwise use a temp file */
1494 if (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1496 ftruncate( fd, 0 );
1497 goto save;
1499 close( fd );
1502 /* create a temp file in the same directory */
1504 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1505 strcpy( tmp, path );
1506 if ((p = strrchr( tmp, '/' ))) p++;
1507 else p = tmp;
1508 for (;;)
1510 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1511 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1512 if (errno != EEXIST) goto done;
1513 close( fd );
1516 /* now save to it */
1518 save:
1519 if (!(f = fdopen( fd, "w" )))
1521 if (tmp) unlink( tmp );
1522 close( fd );
1523 goto done;
1526 if (debug_level > 1)
1528 fprintf( stderr, "%s: ", path );
1529 dump_operation( key, NULL, "saving" );
1532 save_all_subkeys( key, f );
1533 ret = !fclose(f);
1535 if (tmp)
1537 /* if successfully written, rename to final name */
1538 if (ret) ret = !rename( tmp, path );
1539 if (!ret) unlink( tmp );
1540 free( tmp );
1543 done:
1544 if (real) free( real );
1545 return ret;
1548 /* periodic saving of the registry */
1549 static void periodic_save( void *arg )
1551 int i;
1552 for (i = 0; i < save_branch_count; i++)
1553 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1554 add_timeout( &next_save_time, save_period );
1555 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1558 /* save the registry and close the top-level keys; used on server exit */
1559 void close_registry(void)
1561 int i;
1563 for (i = 0; i < save_branch_count; i++)
1565 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1567 fprintf( stderr, "wineserver: could not save registry branch to %s",
1568 save_branch_info[i].path );
1569 perror( " " );
1571 release_object( save_branch_info[i].key );
1573 release_object( root_key );
1577 /* create a registry key */
1578 DECL_HANDLER(create_key)
1580 struct key *key = NULL, *parent;
1581 unsigned int access = req->access;
1582 WCHAR *name, *class;
1583 size_t len;
1585 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1586 req->hkey = -1;
1587 if (!(name = copy_req_path( req, &len ))) return;
1588 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1590 if (len == get_req_data_size(req)) /* no class specified */
1592 key = create_key( parent, name, NULL, req->options, req->modif, &req->created );
1594 else
1596 const WCHAR *class_ptr = (WCHAR *)((char *)get_req_data(req) + len);
1598 if ((class = req_strdupW( req, class_ptr, get_req_data_size(req) - len )))
1600 key = create_key( parent, name, class, req->options,
1601 req->modif, &req->created );
1602 free( class );
1605 if (key)
1607 req->hkey = alloc_handle( current->process, key, access, 0 );
1608 release_object( key );
1610 release_object( parent );
1614 /* open a registry key */
1615 DECL_HANDLER(open_key)
1617 struct key *key, *parent;
1618 unsigned int access = req->access;
1620 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1621 req->hkey = -1;
1622 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1624 WCHAR *name = copy_path( get_req_data(req), get_req_data_size(req) );
1625 if (name && (key = open_key( parent, name )))
1627 req->hkey = alloc_handle( current->process, key, access, 0 );
1628 release_object( key );
1630 release_object( parent );
1634 /* delete a registry key */
1635 DECL_HANDLER(delete_key)
1637 struct key *key;
1639 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1641 delete_key( key );
1642 release_object( key );
1646 /* enumerate registry subkeys */
1647 DECL_HANDLER(enum_key)
1649 struct key *key;
1650 size_t len = 0;
1652 if ((key = get_hkey_obj( req->hkey,
1653 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1655 len = enum_key( key, req->index, req );
1656 release_object( key );
1658 set_req_data_size( req, len );
1661 /* set a value of a registry key */
1662 DECL_HANDLER(set_key_value)
1664 struct key *key;
1665 WCHAR *name;
1666 size_t len;
1668 if (!(name = copy_req_path( req, &len ))) return;
1669 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1671 size_t datalen = get_req_data_size(req) - len;
1672 const char *data = (char *)get_req_data(req) + len;
1674 set_value( key, name, req->type, req->total, req->offset, datalen, data );
1675 release_object( key );
1679 /* retrieve the value of a registry key */
1680 DECL_HANDLER(get_key_value)
1682 struct key *key;
1683 WCHAR *name;
1684 size_t len = 0, tmp;
1686 req->len = 0;
1687 if (!(name = copy_req_path( req, &tmp ))) return;
1688 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1690 len = get_value( key, name, req->offset, get_req_data_size(req),
1691 &req->type, &req->len, get_req_data(req) );
1692 release_object( key );
1694 set_req_data_size( req, len );
1697 /* enumerate the value of a registry key */
1698 DECL_HANDLER(enum_key_value)
1700 struct key *key;
1701 size_t len = 0;
1703 req->len = 0;
1704 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1706 len = enum_value( key, req->index, req->offset, get_req_data_size(req),
1707 &req->type, &req->len, get_req_data(req) );
1708 release_object( key );
1710 set_req_data_size( req, len );
1713 /* delete a value of a registry key */
1714 DECL_HANDLER(delete_key_value)
1716 WCHAR *name;
1717 struct key *key;
1719 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1721 if ((name = req_strdupW( req, get_req_data(req), get_req_data_size(req) )))
1723 delete_value( key, name );
1724 free( name );
1726 release_object( key );
1730 /* load a registry branch from a file */
1731 DECL_HANDLER(load_registry)
1733 struct key *key;
1735 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1737 /* FIXME: use subkey name */
1738 load_registry( key, req->file );
1739 release_object( key );
1743 /* save a registry branch to a file */
1744 DECL_HANDLER(save_registry)
1746 struct key *key;
1748 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1750 save_registry( key, req->file );
1751 release_object( key );
1755 /* set the current and saving level for the registry */
1756 DECL_HANDLER(set_registry_levels)
1758 current_level = req->current;
1759 saving_level = req->saving;
1761 /* set periodic save timer */
1763 if (save_timeout_user)
1765 remove_timeout_user( save_timeout_user );
1766 save_timeout_user = NULL;
1768 if ((save_period = req->period))
1770 if (save_period < 10000) save_period = 10000; /* limit rate */
1771 gettimeofday( &next_save_time, 0 );
1772 add_timeout( &next_save_time, save_period );
1773 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1777 /* save a registry branch at server exit */
1778 DECL_HANDLER(save_registry_atexit)
1780 struct key *key;
1782 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1784 register_branch_for_saving( key, get_req_data(req), get_req_data_size(req) );
1785 release_object( key );