Load system.reg and userdef.reg at server init time.
[wine/multimedia.git] / server / registry.c
blob6f85f5721cf843cd6aff303c720f2f3649ad9abc
1 /*
2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* To do:
22 * - behavior with deleted keys
23 * - values larger than request buffer
24 * - symbolic links
27 #include "config.h"
28 #include "wine/port.h"
30 #include <assert.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
42 #include "object.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "request.h"
46 #include "unicode.h"
48 #include "winbase.h"
49 #include "winreg.h"
50 #include "winternl.h"
51 #include "wine/library.h"
53 struct notify
55 struct list entry; /* entry in list of notifications */
56 struct event *event; /* event to set when changing this key */
57 int subtree; /* true if subtree notification */
58 unsigned int filter; /* which events to notify on */
59 obj_handle_t hkey; /* hkey associated with this notification */
62 /* a registry key */
63 struct key
65 struct object obj; /* object header */
66 WCHAR *name; /* key name */
67 WCHAR *class; /* key class */
68 struct key *parent; /* parent key */
69 int last_subkey; /* last in use subkey */
70 int nb_subkeys; /* count of allocated subkeys */
71 struct key **subkeys; /* subkeys array */
72 int last_value; /* last in use value */
73 int nb_values; /* count of allocated values in array */
74 struct key_value *values; /* values array */
75 unsigned int flags; /* flags */
76 time_t modif; /* last modification time */
77 struct list notify_list; /* list of notifications */
80 /* key flags */
81 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
82 #define KEY_DELETED 0x0002 /* key has been deleted */
83 #define KEY_DIRTY 0x0004 /* key has been modified */
85 /* a key value */
86 struct key_value
88 WCHAR *name; /* value name */
89 int type; /* value type */
90 size_t len; /* value data length in bytes */
91 void *data; /* pointer to value data */
94 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
95 #define MIN_VALUES 8 /* min. number of allocated values per key */
98 /* the root of the registry tree */
99 static struct key *root_key;
101 static const int save_period = 30000; /* delay between periodic saves (in ms) */
102 static struct timeout_user *save_timeout_user; /* saving timer */
104 static void set_periodic_save_timer(void);
106 /* information about where to save a registry branch */
107 struct save_branch_info
109 struct key *key;
110 char *path;
113 #define MAX_SAVE_BRANCH_INFO 3
114 static int save_branch_count;
115 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
118 /* information about a file being loaded */
119 struct file_load_info
121 FILE *file; /* input file */
122 char *buffer; /* line buffer */
123 int len; /* buffer length */
124 int line; /* current input line */
125 char *tmp; /* temp buffer to use while parsing input */
126 int tmplen; /* length of temp buffer */
130 static void key_dump( struct object *obj, int verbose );
131 static void key_destroy( struct object *obj );
133 static const struct object_ops key_ops =
135 sizeof(struct key), /* size */
136 key_dump, /* dump */
137 no_add_queue, /* add_queue */
138 NULL, /* remove_queue */
139 NULL, /* signaled */
140 NULL, /* satisfied */
141 no_get_fd, /* get_fd */
142 key_destroy /* destroy */
147 * The registry text file format v2 used by this code is similar to the one
148 * used by REGEDIT import/export functionality, with the following differences:
149 * - strings and key names can contain \x escapes for Unicode
150 * - key names use escapes too in order to support Unicode
151 * - the modification time optionally follows the key name
152 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
155 static inline char to_hex( char ch )
157 if (isdigit(ch)) return ch - '0';
158 return tolower(ch) - 'a' + 10;
161 /* dump the full path of a key */
162 static void dump_path( const struct key *key, const struct key *base, FILE *f )
164 if (key->parent && key->parent != base)
166 dump_path( key->parent, base, f );
167 fprintf( f, "\\\\" );
169 dump_strW( key->name, strlenW(key->name), f, "[]" );
172 /* dump a value to a text file */
173 static void dump_value( const struct key_value *value, FILE *f )
175 unsigned int i;
176 int 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( const struct key *key, const struct key *base, FILE *f )
229 int i;
231 if (key->flags & KEY_VOLATILE) return;
232 /* save key if it has either some values or no subkeys */
233 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
234 if ((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", (long)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( const struct key *key, const 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 /* notify waiter and maybe delete the notification */
267 static void do_notification( struct key *key, struct notify *notify, int del )
269 if( notify->event )
271 set_event( notify->event );
272 release_object( notify->event );
273 notify->event = NULL;
275 if (del)
277 list_remove( &notify->entry );
278 free( notify );
282 static struct notify *find_notify( struct key *key, obj_handle_t hkey)
284 struct notify *notify;
286 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
288 if (notify->hkey == hkey) return notify;
290 return NULL;
293 /* close the notification associated with a handle */
294 void registry_close_handle( struct object *obj, obj_handle_t hkey )
296 struct key * key = (struct key *) obj;
297 struct notify *notify;
299 if( obj->ops != &key_ops )
300 return;
301 notify = find_notify( key, hkey );
302 if( !notify )
303 return;
304 do_notification( key, notify, 1 );
307 static void key_destroy( struct object *obj )
309 int i;
310 struct list *ptr;
311 struct key *key = (struct key *)obj;
312 assert( obj->ops == &key_ops );
314 if (key->name) free( key->name );
315 if (key->class) free( key->class );
316 for (i = 0; i <= key->last_value; i++)
318 free( key->values[i].name );
319 if (key->values[i].data) free( key->values[i].data );
321 for (i = 0; i <= key->last_subkey; i++)
323 key->subkeys[i]->parent = NULL;
324 release_object( key->subkeys[i] );
326 /* unconditionally notify everything waiting on this key */
327 while ((ptr = list_head( &key->notify_list )))
329 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
330 do_notification( key, notify, 1 );
334 /* duplicate a key path */
335 /* returns a pointer to a static buffer, so only useable once per request */
336 static WCHAR *copy_path( const WCHAR *path, size_t len, int skip_root )
338 static WCHAR buffer[MAX_PATH+1];
339 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\',0 };
341 if (len > sizeof(buffer)-sizeof(buffer[0]))
343 set_error( STATUS_BUFFER_OVERFLOW );
344 return NULL;
346 memcpy( buffer, path, len );
347 buffer[len / sizeof(WCHAR)] = 0;
348 if (skip_root && !strncmpiW( buffer, root_name, 10 )) return buffer + 10;
349 return buffer;
352 /* copy a path from the request buffer */
353 static WCHAR *copy_req_path( size_t len, int skip_root )
355 const WCHAR *name_ptr = get_req_data();
356 if (len > get_req_data_size())
358 fatal_protocol_error( current, "copy_req_path: invalid length %d/%d\n",
359 len, get_req_data_size() );
360 return NULL;
362 return copy_path( name_ptr, len, skip_root );
365 /* return the next token in a given path */
366 /* returns a pointer to a static buffer, so only useable once per request */
367 static WCHAR *get_path_token( WCHAR *initpath )
369 static WCHAR *path;
370 WCHAR *ret;
372 if (initpath)
374 /* path cannot start with a backslash */
375 if (*initpath == '\\')
377 set_error( STATUS_OBJECT_PATH_INVALID );
378 return NULL;
380 path = initpath;
382 else while (*path == '\\') path++;
384 ret = path;
385 while (*path && *path != '\\') path++;
386 if (*path) *path++ = 0;
387 return ret;
390 /* duplicate a Unicode string from the request buffer */
391 static WCHAR *req_strdupW( const void *req, const WCHAR *str, size_t len )
393 WCHAR *name;
394 if ((name = mem_alloc( len + sizeof(WCHAR) )) != NULL)
396 memcpy( name, str, len );
397 name[len / sizeof(WCHAR)] = 0;
399 return name;
402 /* allocate a key object */
403 static struct key *alloc_key( const WCHAR *name, time_t modif )
405 struct key *key;
406 if ((key = alloc_object( &key_ops )))
408 key->class = NULL;
409 key->flags = 0;
410 key->last_subkey = -1;
411 key->nb_subkeys = 0;
412 key->subkeys = NULL;
413 key->nb_values = 0;
414 key->last_value = -1;
415 key->values = NULL;
416 key->modif = modif;
417 key->parent = NULL;
418 list_init( &key->notify_list );
419 if (!(key->name = strdupW( name )))
421 release_object( key );
422 key = NULL;
425 return key;
428 /* mark a key and all its parents as dirty (modified) */
429 static void make_dirty( struct key *key )
431 while (key)
433 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
434 key->flags |= KEY_DIRTY;
435 key = key->parent;
439 /* mark a key and all its subkeys as clean (not modified) */
440 static void make_clean( struct key *key )
442 int i;
444 if (key->flags & KEY_VOLATILE) return;
445 if (!(key->flags & KEY_DIRTY)) return;
446 key->flags &= ~KEY_DIRTY;
447 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
450 /* go through all the notifications and send them if necessary */
451 void check_notify( struct key *key, unsigned int change, int not_subtree )
453 struct list *ptr, *next;
455 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
457 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
458 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
459 do_notification( key, n, 0 );
463 /* update key modification time */
464 static void touch_key( struct key *key, unsigned int change )
466 struct key *k;
468 key->modif = time(NULL);
469 make_dirty( key );
471 /* do notifications */
472 check_notify( key, change, 1 );
473 for ( k = key->parent; k; k = k->parent )
474 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
477 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
478 static int grow_subkeys( struct key *key )
480 struct key **new_subkeys;
481 int nb_subkeys;
483 if (key->nb_subkeys)
485 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
486 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
488 set_error( STATUS_NO_MEMORY );
489 return 0;
492 else
494 nb_subkeys = MIN_VALUES;
495 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
497 key->subkeys = new_subkeys;
498 key->nb_subkeys = nb_subkeys;
499 return 1;
502 /* allocate a subkey for a given key, and return its index */
503 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
505 struct key *key;
506 int i;
508 if (parent->last_subkey + 1 == parent->nb_subkeys)
510 /* need to grow the array */
511 if (!grow_subkeys( parent )) return NULL;
513 if ((key = alloc_key( name, modif )) != NULL)
515 key->parent = parent;
516 for (i = ++parent->last_subkey; i > index; i--)
517 parent->subkeys[i] = parent->subkeys[i-1];
518 parent->subkeys[index] = key;
520 return key;
523 /* free a subkey of a given key */
524 static void free_subkey( struct key *parent, int index )
526 struct key *key;
527 int i, nb_subkeys;
529 assert( index >= 0 );
530 assert( index <= parent->last_subkey );
532 key = parent->subkeys[index];
533 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
534 parent->last_subkey--;
535 key->flags |= KEY_DELETED;
536 key->parent = NULL;
537 release_object( key );
539 /* try to shrink the array */
540 nb_subkeys = parent->nb_subkeys;
541 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
543 struct key **new_subkeys;
544 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
545 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
546 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
547 parent->subkeys = new_subkeys;
548 parent->nb_subkeys = nb_subkeys;
552 /* find the named child of a given key and return its index */
553 static struct key *find_subkey( const struct key *key, const WCHAR *name, int *index )
555 int i, min, max, res;
557 min = 0;
558 max = key->last_subkey;
559 while (min <= max)
561 i = (min + max) / 2;
562 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
564 *index = i;
565 return key->subkeys[i];
567 if (res > 0) max = i - 1;
568 else min = i + 1;
570 *index = min; /* this is where we should insert it */
571 return NULL;
574 /* open a subkey */
575 /* warning: the key name must be writeable (use copy_path) */
576 static struct key *open_key( struct key *key, WCHAR *name )
578 int index;
579 WCHAR *path;
581 if (!(path = get_path_token( name ))) return NULL;
582 while (*path)
584 if (!(key = find_subkey( key, path, &index )))
586 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
587 break;
589 path = get_path_token( NULL );
592 if (debug_level > 1) dump_operation( key, NULL, "Open" );
593 if (key) grab_object( key );
594 return key;
597 /* create a subkey */
598 /* warning: the key name must be writeable (use copy_path) */
599 static struct key *create_key( struct key *key, WCHAR *name, WCHAR *class,
600 int flags, time_t modif, int *created )
602 struct key *base;
603 int base_idx, index;
604 WCHAR *path;
606 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
608 set_error( STATUS_KEY_DELETED );
609 return NULL;
611 if (!(flags & KEY_VOLATILE) && (key->flags & KEY_VOLATILE))
613 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
614 return NULL;
616 if (!modif) modif = time(NULL);
618 if (!(path = get_path_token( name ))) return NULL;
619 *created = 0;
620 while (*path)
622 struct key *subkey;
623 if (!(subkey = find_subkey( key, path, &index ))) break;
624 key = subkey;
625 path = get_path_token( NULL );
628 /* create the remaining part */
630 if (!*path) goto done;
631 *created = 1;
632 if (flags & KEY_DIRTY) make_dirty( key );
633 base = key;
634 base_idx = index;
635 key = alloc_subkey( key, path, index, modif );
636 while (key)
638 key->flags |= flags;
639 path = get_path_token( NULL );
640 if (!*path) goto done;
641 /* we know the index is always 0 in a new key */
642 key = alloc_subkey( key, path, 0, modif );
644 if (base_idx != -1) free_subkey( base, base_idx );
645 return NULL;
647 done:
648 if (debug_level > 1) dump_operation( key, NULL, "Create" );
649 if (class) key->class = strdupW(class);
650 grab_object( key );
651 return key;
654 /* query information about a key or a subkey */
655 static void enum_key( const struct key *key, int index, int info_class,
656 struct enum_key_reply *reply )
658 int i;
659 size_t len, namelen, classlen;
660 int max_subkey = 0, max_class = 0;
661 int max_value = 0, max_data = 0;
662 WCHAR *data;
664 if (index != -1) /* -1 means use the specified key directly */
666 if ((index < 0) || (index > key->last_subkey))
668 set_error( STATUS_NO_MORE_ENTRIES );
669 return;
671 key = key->subkeys[index];
674 namelen = strlenW(key->name) * sizeof(WCHAR);
675 classlen = key->class ? strlenW(key->class) * sizeof(WCHAR) : 0;
677 switch(info_class)
679 case KeyBasicInformation:
680 classlen = 0; /* only return the name */
681 /* fall through */
682 case KeyNodeInformation:
683 reply->max_subkey = 0;
684 reply->max_class = 0;
685 reply->max_value = 0;
686 reply->max_data = 0;
687 break;
688 case KeyFullInformation:
689 for (i = 0; i <= key->last_subkey; i++)
691 struct key *subkey = key->subkeys[i];
692 len = strlenW( subkey->name );
693 if (len > max_subkey) max_subkey = len;
694 if (!subkey->class) continue;
695 len = strlenW( subkey->class );
696 if (len > max_class) max_class = len;
698 for (i = 0; i <= key->last_value; i++)
700 len = strlenW( key->values[i].name );
701 if (len > max_value) max_value = len;
702 len = key->values[i].len;
703 if (len > max_data) max_data = len;
705 reply->max_subkey = max_subkey;
706 reply->max_class = max_class;
707 reply->max_value = max_value;
708 reply->max_data = max_data;
709 namelen = 0; /* only return the class */
710 break;
711 default:
712 set_error( STATUS_INVALID_PARAMETER );
713 return;
715 reply->subkeys = key->last_subkey + 1;
716 reply->values = key->last_value + 1;
717 reply->modif = key->modif;
718 reply->total = namelen + classlen;
720 len = min( reply->total, get_reply_max_size() );
721 if (len && (data = set_reply_data_size( len )))
723 if (len > namelen)
725 reply->namelen = namelen;
726 memcpy( data, key->name, namelen );
727 memcpy( (char *)data + namelen, key->class, len - namelen );
729 else
731 reply->namelen = len;
732 memcpy( data, key->name, len );
735 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
738 /* delete a key and its values */
739 static int delete_key( struct key *key, int recurse )
741 int index;
742 struct key *parent;
744 /* must find parent and index */
745 if (key == root_key)
747 set_error( STATUS_ACCESS_DENIED );
748 return -1;
750 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
752 set_error( STATUS_KEY_DELETED );
753 return -1;
756 while (recurse && (key->last_subkey>=0))
757 if(0>delete_key(key->subkeys[key->last_subkey], 1))
758 return -1;
760 for (index = 0; index <= parent->last_subkey; index++)
761 if (parent->subkeys[index] == key) break;
762 assert( index <= parent->last_subkey );
764 /* we can only delete a key that has no subkeys */
765 if (key->last_subkey >= 0)
767 set_error( STATUS_ACCESS_DENIED );
768 return -1;
771 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
772 free_subkey( parent, index );
773 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
774 return 0;
777 /* try to grow the array of values; return 1 if OK, 0 on error */
778 static int grow_values( struct key *key )
780 struct key_value *new_val;
781 int nb_values;
783 if (key->nb_values)
785 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
786 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
788 set_error( STATUS_NO_MEMORY );
789 return 0;
792 else
794 nb_values = MIN_VALUES;
795 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
797 key->values = new_val;
798 key->nb_values = nb_values;
799 return 1;
802 /* find the named value of a given key and return its index in the array */
803 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
805 int i, min, max, res;
807 min = 0;
808 max = key->last_value;
809 while (min <= max)
811 i = (min + max) / 2;
812 if (!(res = strcmpiW( key->values[i].name, name )))
814 *index = i;
815 return &key->values[i];
817 if (res > 0) max = i - 1;
818 else min = i + 1;
820 *index = min; /* this is where we should insert it */
821 return NULL;
824 /* insert a new value; the index must have been returned by find_value */
825 static struct key_value *insert_value( struct key *key, const WCHAR *name, int index )
827 struct key_value *value;
828 WCHAR *new_name;
829 int i;
831 if (key->last_value + 1 == key->nb_values)
833 if (!grow_values( key )) return NULL;
835 if (!(new_name = strdupW(name))) return NULL;
836 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
837 value = &key->values[index];
838 value->name = new_name;
839 value->len = 0;
840 value->data = NULL;
841 return value;
844 /* set a key value */
845 static void set_value( struct key *key, WCHAR *name, int type, const void *data, size_t len )
847 struct key_value *value;
848 void *ptr = NULL;
849 int index;
851 if ((value = find_value( key, name, &index )))
853 /* check if the new value is identical to the existing one */
854 if (value->type == type && value->len == len &&
855 value->data && !memcmp( value->data, data, len ))
857 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
858 return;
862 if (len && !(ptr = memdup( data, len ))) return;
864 if (!value)
866 if (!(value = insert_value( key, name, index )))
868 if (ptr) free( ptr );
869 return;
872 else if (value->data) free( value->data ); /* already existing, free previous data */
874 value->type = type;
875 value->len = len;
876 value->data = ptr;
877 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
878 if (debug_level > 1) dump_operation( key, value, "Set" );
881 /* get a key value */
882 static void get_value( struct key *key, const WCHAR *name, int *type, int *len )
884 struct key_value *value;
885 int index;
887 if ((value = find_value( key, name, &index )))
889 *type = value->type;
890 *len = value->len;
891 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
892 if (debug_level > 1) dump_operation( key, value, "Get" );
894 else
896 *type = -1;
897 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
901 /* enumerate a key value */
902 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
904 struct key_value *value;
906 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
907 else
909 void *data;
910 size_t namelen, maxlen;
912 value = &key->values[i];
913 reply->type = value->type;
914 namelen = strlenW( value->name ) * sizeof(WCHAR);
916 switch(info_class)
918 case KeyValueBasicInformation:
919 reply->total = namelen;
920 break;
921 case KeyValueFullInformation:
922 reply->total = namelen + value->len;
923 break;
924 case KeyValuePartialInformation:
925 reply->total = value->len;
926 namelen = 0;
927 break;
928 default:
929 set_error( STATUS_INVALID_PARAMETER );
930 return;
933 maxlen = min( reply->total, get_reply_max_size() );
934 if (maxlen && ((data = set_reply_data_size( maxlen ))))
936 if (maxlen > namelen)
938 reply->namelen = namelen;
939 memcpy( data, value->name, namelen );
940 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
942 else
944 reply->namelen = maxlen;
945 memcpy( data, value->name, maxlen );
948 if (debug_level > 1) dump_operation( key, value, "Enum" );
952 /* delete a value */
953 static void delete_value( struct key *key, const WCHAR *name )
955 struct key_value *value;
956 int i, index, nb_values;
958 if (!(value = find_value( key, name, &index )))
960 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
961 return;
963 if (debug_level > 1) dump_operation( key, value, "Delete" );
964 free( value->name );
965 if (value->data) free( value->data );
966 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
967 key->last_value--;
968 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
970 /* try to shrink the array */
971 nb_values = key->nb_values;
972 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
974 struct key_value *new_val;
975 nb_values -= nb_values / 3; /* shrink by 33% */
976 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
977 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
978 key->values = new_val;
979 key->nb_values = nb_values;
983 /* get the registry key corresponding to an hkey handle */
984 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
986 if (!hkey) return (struct key *)grab_object( root_key );
987 return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
990 /* read a line from the input file */
991 static int read_next_line( struct file_load_info *info )
993 char *newbuf;
994 int newlen, pos = 0;
996 info->line++;
997 for (;;)
999 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1000 return (pos != 0); /* EOF */
1001 pos = strlen(info->buffer);
1002 if (info->buffer[pos-1] == '\n')
1004 /* got a full line */
1005 info->buffer[--pos] = 0;
1006 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1007 return 1;
1009 if (pos < info->len - 1) return 1; /* EOF but something was read */
1011 /* need to enlarge the buffer */
1012 newlen = info->len + info->len / 2;
1013 if (!(newbuf = realloc( info->buffer, newlen )))
1015 set_error( STATUS_NO_MEMORY );
1016 return -1;
1018 info->buffer = newbuf;
1019 info->len = newlen;
1023 /* make sure the temp buffer holds enough space */
1024 static int get_file_tmp_space( struct file_load_info *info, int size )
1026 char *tmp;
1027 if (info->tmplen >= size) return 1;
1028 if (!(tmp = realloc( info->tmp, size )))
1030 set_error( STATUS_NO_MEMORY );
1031 return 0;
1033 info->tmp = tmp;
1034 info->tmplen = size;
1035 return 1;
1038 /* report an error while loading an input file */
1039 static void file_read_error( const char *err, struct file_load_info *info )
1041 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
1044 /* parse an escaped string back into Unicode */
1045 /* return the number of chars read from the input, or -1 on output overflow */
1046 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
1048 int count = sizeof(WCHAR); /* for terminating null */
1049 const char *p = src;
1050 while (*p && *p != endchar)
1052 if (*p != '\\') *dest = (WCHAR)*p++;
1053 else
1055 p++;
1056 switch(*p)
1058 case 'a': *dest = '\a'; p++; break;
1059 case 'b': *dest = '\b'; p++; break;
1060 case 'e': *dest = '\e'; p++; break;
1061 case 'f': *dest = '\f'; p++; break;
1062 case 'n': *dest = '\n'; p++; break;
1063 case 'r': *dest = '\r'; p++; break;
1064 case 't': *dest = '\t'; p++; break;
1065 case 'v': *dest = '\v'; p++; break;
1066 case 'x': /* hex escape */
1067 p++;
1068 if (!isxdigit(*p)) *dest = 'x';
1069 else
1071 *dest = to_hex(*p++);
1072 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1073 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1074 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1076 break;
1077 case '0':
1078 case '1':
1079 case '2':
1080 case '3':
1081 case '4':
1082 case '5':
1083 case '6':
1084 case '7': /* octal escape */
1085 *dest = *p++ - '0';
1086 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1087 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1088 break;
1089 default:
1090 *dest = (WCHAR)*p++;
1091 break;
1094 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1095 dest++;
1097 *dest = 0;
1098 if (!*p) return -1; /* delimiter not found */
1099 *len = count;
1100 return p + 1 - src;
1103 /* convert a data type tag to a value type */
1104 static int get_data_type( const char *buffer, int *type, int *parse_type )
1106 struct data_type { const char *tag; int len; int type; int parse_type; };
1108 static const struct data_type data_types[] =
1109 { /* actual type */ /* type to assume for parsing */
1110 { "\"", 1, REG_SZ, REG_SZ },
1111 { "str:\"", 5, REG_SZ, REG_SZ },
1112 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1113 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1114 { "hex:", 4, REG_BINARY, REG_BINARY },
1115 { "dword:", 6, REG_DWORD, REG_DWORD },
1116 { "hex(", 4, -1, REG_BINARY },
1117 { NULL, 0, 0, 0 }
1120 const struct data_type *ptr;
1121 char *end;
1123 for (ptr = data_types; ptr->tag; ptr++)
1125 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1126 *parse_type = ptr->parse_type;
1127 if ((*type = ptr->type) != -1) return ptr->len;
1128 /* "hex(xx):" is special */
1129 *type = (int)strtoul( buffer + 4, &end, 16 );
1130 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1131 return end + 2 - buffer;
1133 return 0;
1136 /* load and create a key from the input file */
1137 static struct key *load_key( struct key *base, const char *buffer, int flags,
1138 int prefix_len, struct file_load_info *info,
1139 int default_modif )
1141 WCHAR *p, *name;
1142 int res, len, modif;
1144 len = strlen(buffer) * sizeof(WCHAR);
1145 if (!get_file_tmp_space( info, len )) return NULL;
1147 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1149 file_read_error( "Malformed key", info );
1150 return NULL;
1152 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = default_modif;
1154 p = (WCHAR *)info->tmp;
1155 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1157 if (!*p)
1159 if (prefix_len > 1)
1161 file_read_error( "Malformed key", info );
1162 return NULL;
1164 /* empty key name, return base key */
1165 return (struct key *)grab_object( base );
1167 if (!(name = copy_path( p, len - ((char *)p - info->tmp), 0 )))
1169 file_read_error( "Key is too long", info );
1170 return NULL;
1172 return create_key( base, name, NULL, flags, modif, &res );
1175 /* parse a comma-separated list of hex digits */
1176 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1178 const char *p = buffer;
1179 int count = 0;
1180 while (isxdigit(*p))
1182 int val;
1183 char buf[3];
1184 memcpy( buf, p, 2 );
1185 buf[2] = 0;
1186 sscanf( buf, "%x", &val );
1187 if (count++ >= *len) return -1; /* dest buffer overflow */
1188 *dest++ = (unsigned char )val;
1189 p += 2;
1190 if (*p == ',') p++;
1192 *len = count;
1193 return p - buffer;
1196 /* parse a value name and create the corresponding value */
1197 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1198 struct file_load_info *info )
1200 struct key_value *value;
1201 int index, maxlen;
1203 maxlen = strlen(buffer) * sizeof(WCHAR);
1204 if (!get_file_tmp_space( info, maxlen )) return NULL;
1205 if (buffer[0] == '@')
1207 info->tmp[0] = info->tmp[1] = 0;
1208 *len = 1;
1210 else
1212 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1213 (*len)++; /* for initial quote */
1215 while (isspace(buffer[*len])) (*len)++;
1216 if (buffer[*len] != '=') goto error;
1217 (*len)++;
1218 while (isspace(buffer[*len])) (*len)++;
1219 if (!(value = find_value( key, (WCHAR *)info->tmp, &index )))
1220 value = insert_value( key, (WCHAR *)info->tmp, index );
1221 return value;
1223 error:
1224 file_read_error( "Malformed value name", info );
1225 return NULL;
1228 /* load a value from the input file */
1229 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1231 DWORD dw;
1232 void *ptr, *newptr;
1233 int maxlen, len, res;
1234 int type, parse_type;
1235 struct key_value *value;
1237 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1238 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1239 buffer += len + res;
1241 switch(parse_type)
1243 case REG_SZ:
1244 len = strlen(buffer) * sizeof(WCHAR);
1245 if (!get_file_tmp_space( info, len )) return 0;
1246 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1247 ptr = info->tmp;
1248 break;
1249 case REG_DWORD:
1250 dw = strtoul( buffer, NULL, 16 );
1251 ptr = &dw;
1252 len = sizeof(dw);
1253 break;
1254 case REG_BINARY: /* hex digits */
1255 len = 0;
1256 for (;;)
1258 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1259 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1260 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1261 len += maxlen;
1262 buffer += res;
1263 while (isspace(*buffer)) buffer++;
1264 if (!*buffer) break;
1265 if (*buffer != '\\') goto error;
1266 if (read_next_line( info) != 1) goto error;
1267 buffer = info->buffer;
1268 while (isspace(*buffer)) buffer++;
1270 ptr = info->tmp;
1271 break;
1272 default:
1273 assert(0);
1274 ptr = NULL; /* keep compiler quiet */
1275 break;
1278 if (!len) newptr = NULL;
1279 else if (!(newptr = memdup( ptr, len ))) return 0;
1281 if (value->data) free( value->data );
1282 value->data = newptr;
1283 value->len = len;
1284 value->type = type;
1285 make_dirty( key );
1286 return 1;
1288 error:
1289 file_read_error( "Malformed value", info );
1290 return 0;
1293 /* return the length (in path elements) of name that is part of the key name */
1294 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1295 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1297 WCHAR *p;
1298 int res;
1299 int len = strlen(name) * sizeof(WCHAR);
1300 if (!get_file_tmp_space( info, len )) return 0;
1302 if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
1304 file_read_error( "Malformed key", info );
1305 return 0;
1307 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
1308 *p = 0;
1309 for (res = 1; key != root_key; res++)
1311 if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
1312 key = key->parent;
1314 if (key == root_key) res = 0; /* no matching name */
1315 return res;
1318 /* load all the keys from the input file */
1319 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1320 static void load_keys( struct key *key, FILE *f, int prefix_len )
1322 struct key *subkey = NULL;
1323 struct file_load_info info;
1324 char *p;
1325 int default_modif = time(NULL);
1326 int flags = (key->flags & KEY_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1328 info.file = f;
1329 info.len = 4;
1330 info.tmplen = 4;
1331 info.line = 0;
1332 if (!(info.buffer = mem_alloc( info.len ))) return;
1333 if (!(info.tmp = mem_alloc( info.tmplen )))
1335 free( info.buffer );
1336 return;
1339 if ((read_next_line( &info ) != 1) ||
1340 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1342 set_error( STATUS_NOT_REGISTRY_FILE );
1343 goto done;
1346 while (read_next_line( &info ) == 1)
1348 p = info.buffer;
1349 while (*p && isspace(*p)) p++;
1350 switch(*p)
1352 case '[': /* new key */
1353 if (subkey) release_object( subkey );
1354 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1355 if (!(subkey = load_key( key, p + 1, flags, prefix_len, &info, default_modif )))
1356 file_read_error( "Error creating key", &info );
1357 break;
1358 case '@': /* default value */
1359 case '\"': /* value */
1360 if (subkey) load_value( subkey, p, &info );
1361 else file_read_error( "Value without key", &info );
1362 break;
1363 case '#': /* comment */
1364 case ';': /* comment */
1365 case 0: /* empty line */
1366 break;
1367 default:
1368 file_read_error( "Unrecognized input", &info );
1369 break;
1373 done:
1374 if (subkey) release_object( subkey );
1375 free( info.buffer );
1376 free( info.tmp );
1379 /* load a part of the registry from a file */
1380 static void load_registry( struct key *key, obj_handle_t handle )
1382 struct file *file;
1383 int fd;
1385 if (!(file = get_file_obj( current->process, handle, GENERIC_READ ))) return;
1386 fd = dup( get_file_unix_fd( file ) );
1387 release_object( file );
1388 if (fd != -1)
1390 FILE *f = fdopen( fd, "r" );
1391 if (f)
1393 load_keys( key, f, -1 );
1394 fclose( f );
1396 else file_set_error();
1400 /* load one of the initial registry files */
1401 static void load_init_registry_from_file( const char *filename, struct key *key )
1403 FILE *f;
1405 if ((f = fopen( filename, "r" )))
1407 load_keys( key, f, 0 );
1408 fclose( f );
1409 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1410 fatal_error( "%s is not a valid registry file\n", filename );
1411 if (get_error())
1412 fatal_error( "loading %s failed with error %x\n", filename, get_error() );
1415 if (!(key->flags & KEY_VOLATILE))
1417 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1419 if ((save_branch_info[save_branch_count].path = strdup( filename )))
1420 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1424 /* load the user registry files */
1425 static void load_user_registries( struct key *key_current_user )
1427 const char *config = wine_get_config_dir();
1428 char *filename;
1430 /* load user.reg into HKEY_CURRENT_USER */
1432 if (!(filename = mem_alloc( strlen(config) + sizeof("/user.reg") ))) return;
1433 strcpy( filename, config );
1434 strcat( filename, "/user.reg" );
1435 load_init_registry_from_file( filename, key_current_user );
1436 free( filename );
1438 /* start the periodic save timer */
1439 set_periodic_save_timer();
1442 /* registry initialisation */
1443 void init_registry(void)
1445 static const WCHAR root_name[] = { 0 };
1446 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1447 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1448 static const WCHAR config_name[] =
1449 { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
1450 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 };
1452 const char *config = wine_get_config_dir();
1453 char *p, *filename;
1454 struct key *key;
1455 int dummy;
1457 /* create the root key */
1458 root_key = alloc_key( root_name, time(NULL) );
1459 assert( root_key );
1461 /* load the config file */
1462 if (!(filename = malloc( strlen(config) + 16 ))) fatal_error( "out of memory\n" );
1463 strcpy( filename, config );
1464 p = filename + strlen(filename);
1466 /* load system.reg into Registry\Machine */
1468 if (!(key = create_key( root_key, copy_path( HKLM, sizeof(HKLM), 0 ),
1469 NULL, 0, time(NULL), &dummy )))
1470 fatal_error( "could not create Machine registry key\n" );
1472 strcpy( p, "/system.reg" );
1473 load_init_registry_from_file( filename, key );
1474 release_object( key );
1476 /* load userdef.reg into Registry\User\.Default */
1478 if (!(key = create_key( root_key, copy_path( HKU_default, sizeof(HKU_default), 0 ),
1479 NULL, 0, time(NULL), &dummy )))
1480 fatal_error( "could not create User\\.Default registry key\n" );
1482 strcpy( p, "/userdef.reg" );
1483 load_init_registry_from_file( filename, key );
1484 release_object( key );
1486 /* load config into Registry\Machine\Software\Wine\Wine\Config */
1488 if (!(key = create_key( root_key, copy_path( config_name, sizeof(config_name), 0 ),
1489 NULL, 0, time(NULL), &dummy )))
1490 fatal_error( "could not create Config registry key\n" );
1492 key->flags |= KEY_VOLATILE;
1493 strcpy( p, "/config" );
1494 load_init_registry_from_file( filename, key );
1495 release_object( key );
1497 free( filename );
1500 /* save a registry branch to a file */
1501 static void save_all_subkeys( struct key *key, FILE *f )
1503 fprintf( f, "WINE REGISTRY Version 2\n" );
1504 fprintf( f, ";; All keys relative to " );
1505 dump_path( key, NULL, f );
1506 fprintf( f, "\n" );
1507 save_subkeys( key, key, f );
1510 /* save a registry branch to a file handle */
1511 static void save_registry( struct key *key, obj_handle_t handle )
1513 struct file *file;
1514 int fd;
1516 if (key->flags & KEY_DELETED)
1518 set_error( STATUS_KEY_DELETED );
1519 return;
1521 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE ))) return;
1522 fd = dup( get_file_unix_fd( file ) );
1523 release_object( file );
1524 if (fd != -1)
1526 FILE *f = fdopen( fd, "w" );
1527 if (f)
1529 save_all_subkeys( key, f );
1530 if (fclose( f )) file_set_error();
1532 else
1534 file_set_error();
1535 close( fd );
1540 /* save a registry branch to a file */
1541 static int save_branch( struct key *key, const char *path )
1543 struct stat st;
1544 char *p, *real, *tmp = NULL;
1545 int fd, count = 0, ret = 0, by_symlink;
1546 FILE *f;
1548 if (!(key->flags & KEY_DIRTY))
1550 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1551 return 1;
1554 /* get the real path */
1556 by_symlink = (!lstat(path, &st) && S_ISLNK (st.st_mode));
1557 if (!(real = malloc( PATH_MAX ))) return 0;
1558 if (!realpath( path, real ))
1560 free( real );
1561 real = NULL;
1563 else path = real;
1565 /* test the file type */
1567 if ((fd = open( path, O_WRONLY )) != -1)
1569 /* if file is not a regular file or has multiple links or is accessed
1570 * via symbolic links, write directly into it; otherwise use a temp file */
1571 if (by_symlink ||
1572 (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)))
1574 ftruncate( fd, 0 );
1575 goto save;
1577 close( fd );
1580 /* create a temp file in the same directory */
1582 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1583 strcpy( tmp, path );
1584 if ((p = strrchr( tmp, '/' ))) p++;
1585 else p = tmp;
1586 for (;;)
1588 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1589 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1590 if (errno != EEXIST) goto done;
1591 close( fd );
1594 /* now save to it */
1596 save:
1597 if (!(f = fdopen( fd, "w" )))
1599 if (tmp) unlink( tmp );
1600 close( fd );
1601 goto done;
1604 if (debug_level > 1)
1606 fprintf( stderr, "%s: ", path );
1607 dump_operation( key, NULL, "saving" );
1610 save_all_subkeys( key, f );
1611 ret = !fclose(f);
1613 if (tmp)
1615 /* if successfully written, rename to final name */
1616 if (ret) ret = !rename( tmp, path );
1617 if (!ret) unlink( tmp );
1620 done:
1621 free( tmp );
1622 free( real );
1623 if (ret) make_clean( key );
1624 return ret;
1627 /* periodic saving of the registry */
1628 static void periodic_save( void *arg )
1630 int i;
1632 save_timeout_user = NULL;
1633 for (i = 0; i < save_branch_count; i++)
1634 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1635 set_periodic_save_timer();
1638 /* start the periodic save timer */
1639 static void set_periodic_save_timer(void)
1641 struct timeval next;
1643 gettimeofday( &next, 0 );
1644 add_timeout( &next, save_period );
1645 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1646 save_timeout_user = add_timeout_user( &next, periodic_save, 0 );
1649 /* save the modified registry branches to disk */
1650 void flush_registry(void)
1652 int i;
1654 for (i = 0; i < save_branch_count; i++)
1656 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1658 fprintf( stderr, "wineserver: could not save registry branch to %s",
1659 save_branch_info[i].path );
1660 perror( " " );
1665 /* close the top-level keys; used on server exit */
1666 void close_registry(void)
1668 int i;
1670 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1671 save_timeout_user = NULL;
1672 for (i = 0; i < save_branch_count; i++) release_object( save_branch_info[i].key );
1673 release_object( root_key );
1677 /* create a registry key */
1678 DECL_HANDLER(create_key)
1680 struct key *key = NULL, *parent;
1681 unsigned int access = req->access;
1682 WCHAR *name, *class;
1684 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1685 reply->hkey = 0;
1686 if (!(name = copy_req_path( req->namelen, !req->parent ))) return;
1687 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1689 int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1691 if (req->namelen == get_req_data_size()) /* no class specified */
1693 key = create_key( parent, name, NULL, flags, req->modif, &reply->created );
1695 else
1697 const WCHAR *class_ptr = (const WCHAR *)((const char *)get_req_data() + req->namelen);
1699 if ((class = req_strdupW( req, class_ptr, get_req_data_size() - req->namelen )))
1701 key = create_key( parent, name, class, flags, req->modif, &reply->created );
1702 free( class );
1705 if (key)
1707 reply->hkey = alloc_handle( current->process, key, access, 0 );
1708 release_object( key );
1710 release_object( parent );
1714 /* open a registry key */
1715 DECL_HANDLER(open_key)
1717 struct key *key, *parent;
1718 unsigned int access = req->access;
1720 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1721 reply->hkey = 0;
1722 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1724 WCHAR *name = copy_path( get_req_data(), get_req_data_size(), !req->parent );
1725 if (name && (key = open_key( parent, name )))
1727 reply->hkey = alloc_handle( current->process, key, access, 0 );
1728 release_object( key );
1730 release_object( parent );
1734 /* delete a registry key */
1735 DECL_HANDLER(delete_key)
1737 struct key *key;
1739 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1741 delete_key( key, 0);
1742 release_object( key );
1746 /* flush a registry key */
1747 DECL_HANDLER(flush_key)
1749 struct key *key = get_hkey_obj( req->hkey, 0 );
1750 if (key)
1752 /* we don't need to do anything here with the current implementation */
1753 release_object( key );
1757 /* enumerate registry subkeys */
1758 DECL_HANDLER(enum_key)
1760 struct key *key;
1762 if ((key = get_hkey_obj( req->hkey,
1763 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1765 enum_key( key, req->index, req->info_class, reply );
1766 release_object( key );
1770 /* set a value of a registry key */
1771 DECL_HANDLER(set_key_value)
1773 struct key *key;
1774 WCHAR *name;
1776 if (!(name = copy_req_path( req->namelen, 0 ))) return;
1777 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1779 size_t datalen = get_req_data_size() - req->namelen;
1780 const char *data = (const char *)get_req_data() + req->namelen;
1782 set_value( key, name, req->type, data, datalen );
1783 release_object( key );
1787 /* retrieve the value of a registry key */
1788 DECL_HANDLER(get_key_value)
1790 struct key *key;
1791 WCHAR *name;
1793 reply->total = 0;
1794 if (!(name = copy_path( get_req_data(), get_req_data_size(), 0 ))) return;
1795 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1797 get_value( key, name, &reply->type, &reply->total );
1798 release_object( key );
1802 /* enumerate the value of a registry key */
1803 DECL_HANDLER(enum_key_value)
1805 struct key *key;
1807 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1809 enum_value( key, req->index, req->info_class, reply );
1810 release_object( key );
1814 /* delete a value of a registry key */
1815 DECL_HANDLER(delete_key_value)
1817 WCHAR *name;
1818 struct key *key;
1820 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1822 if ((name = req_strdupW( req, get_req_data(), get_req_data_size() )))
1824 delete_value( key, name );
1825 free( name );
1827 release_object( key );
1831 /* load a registry branch from a file */
1832 DECL_HANDLER(load_registry)
1834 struct key *key, *parent;
1836 if ((parent = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1838 int dummy;
1839 WCHAR *name = copy_path( get_req_data(), get_req_data_size(), !req->hkey );
1840 if (name && (key = create_key( parent, name, NULL, KEY_DIRTY, time(NULL), &dummy )))
1842 load_registry( key, req->file );
1843 release_object( key );
1845 release_object( parent );
1849 DECL_HANDLER(unload_registry)
1851 struct key *key;
1853 if ((key = get_hkey_obj( req->hkey, 0 )))
1855 delete_key( key, 1 ); /* FIXME */
1856 release_object( key );
1860 /* save a registry branch to a file */
1861 DECL_HANDLER(save_registry)
1863 struct key *key;
1865 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1867 save_registry( key, req->file );
1868 release_object( key );
1872 /* load the user registry files */
1873 DECL_HANDLER(load_user_registries)
1875 struct key *key;
1877 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1879 load_user_registries( key );
1880 release_object( key );
1884 /* add a registry key change notification */
1885 DECL_HANDLER(set_registry_notification)
1887 struct key *key;
1888 struct event *event;
1889 struct notify *notify;
1891 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
1892 if( key )
1894 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
1895 if( event )
1897 notify = find_notify( key, req->hkey );
1898 if( notify )
1900 release_object( notify->event );
1901 grab_object( event );
1902 notify->event = event;
1904 else
1906 notify = mem_alloc( sizeof(*notify) );
1907 if( notify )
1909 grab_object( event );
1910 notify->event = event;
1911 notify->subtree = req->subtree;
1912 notify->filter = req->filter;
1913 notify->hkey = req->hkey;
1914 list_add_head( &key->notify_list, &notify->entry );
1917 release_object( event );
1919 release_object( key );