First creation of registry entries missed AppData.
[wine/gsoc_dplay.git] / server / registry.c
blobb34301d5e5e9172e3b776837ea4aa89a02c86b90
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"
47 #include "security.h"
49 #include "winternl.h"
50 #include "wine/library.h"
52 struct notify
54 struct list entry; /* entry in list of notifications */
55 struct event *event; /* event to set when changing this key */
56 int subtree; /* true if subtree notification */
57 unsigned int filter; /* which events to notify on */
58 obj_handle_t hkey; /* hkey associated with this notification */
59 struct process *process; /* process in which the hkey is valid */
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 WCHAR *tmp; /* temp buffer to use while parsing input */
126 size_t tmplen; /* length of temp buffer */
130 static void key_dump( struct object *obj, int verbose );
131 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
132 static void key_destroy( struct object *obj );
134 static const struct object_ops key_ops =
136 sizeof(struct key), /* size */
137 key_dump, /* dump */
138 no_add_queue, /* add_queue */
139 NULL, /* remove_queue */
140 NULL, /* signaled */
141 NULL, /* satisfied */
142 no_signal, /* signal */
143 no_get_fd, /* get_fd */
144 key_close_handle, /* close_handle */
145 key_destroy /* destroy */
150 * The registry text file format v2 used by this code is similar to the one
151 * used by REGEDIT import/export functionality, with the following differences:
152 * - strings and key names can contain \x escapes for Unicode
153 * - key names use escapes too in order to support Unicode
154 * - the modification time optionally follows the key name
155 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
158 static inline char to_hex( char ch )
160 if (isdigit(ch)) return ch - '0';
161 return tolower(ch) - 'a' + 10;
164 /* dump the full path of a key */
165 static void dump_path( const struct key *key, const struct key *base, FILE *f )
167 if (key->parent && key->parent != base)
169 dump_path( key->parent, base, f );
170 fprintf( f, "\\\\" );
172 dump_strW( key->name, strlenW(key->name), f, "[]" );
175 /* dump a value to a text file */
176 static void dump_value( const struct key_value *value, FILE *f )
178 unsigned int i;
179 int count;
181 if (value->name[0])
183 fputc( '\"', f );
184 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
185 count += fprintf( f, "\"=" );
187 else count = fprintf( f, "@=" );
189 switch(value->type)
191 case REG_SZ:
192 case REG_EXPAND_SZ:
193 case REG_MULTI_SZ:
194 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
195 fputc( '\"', f );
196 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
197 fputc( '\"', f );
198 break;
199 case REG_DWORD:
200 if (value->len == sizeof(DWORD))
202 DWORD dw;
203 memcpy( &dw, value->data, sizeof(DWORD) );
204 fprintf( f, "dword:%08lx", dw );
205 break;
207 /* else fall through */
208 default:
209 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
210 else count += fprintf( f, "hex(%x):", value->type );
211 for (i = 0; i < value->len; i++)
213 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
214 if (i < value->len-1)
216 fputc( ',', f );
217 if (++count > 76)
219 fprintf( f, "\\\n " );
220 count = 2;
224 break;
226 fputc( '\n', f );
229 /* save a registry and all its subkeys to a text file */
230 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
232 int i;
234 if (key->flags & KEY_VOLATILE) return;
235 /* save key if it has either some values or no subkeys */
236 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
237 if ((key->last_value >= 0) || (key->last_subkey == -1))
239 fprintf( f, "\n[" );
240 if (key != base) dump_path( key, base, f );
241 fprintf( f, "] %ld\n", (long)key->modif );
242 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
244 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
247 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
249 fprintf( stderr, "%s key ", op );
250 if (key) dump_path( key, NULL, stderr );
251 else fprintf( stderr, "ERROR" );
252 if (value)
254 fprintf( stderr, " value ");
255 dump_value( value, stderr );
257 else fprintf( stderr, "\n" );
260 static void key_dump( struct object *obj, int verbose )
262 struct key *key = (struct key *)obj;
263 assert( obj->ops == &key_ops );
264 fprintf( stderr, "Key flags=%x ", key->flags );
265 dump_path( key, NULL, stderr );
266 fprintf( stderr, "\n" );
269 /* notify waiter and maybe delete the notification */
270 static void do_notification( struct key *key, struct notify *notify, int del )
272 if (notify->event)
274 set_event( notify->event );
275 release_object( notify->event );
276 notify->event = NULL;
278 if (del)
280 list_remove( &notify->entry );
281 free( notify );
285 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
287 struct notify *notify;
289 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
291 if (notify->process == process && notify->hkey == hkey) return notify;
293 return NULL;
296 /* close the notification associated with a handle */
297 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
299 struct key * key = (struct key *) obj;
300 struct notify *notify = find_notify( key, process, handle );
301 if (notify) do_notification( key, notify, 1 );
302 return 1; /* ok to close */
305 static void key_destroy( struct object *obj )
307 int i;
308 struct list *ptr;
309 struct key *key = (struct key *)obj;
310 assert( obj->ops == &key_ops );
312 if (key->name) free( key->name );
313 if (key->class) free( key->class );
314 for (i = 0; i <= key->last_value; i++)
316 free( key->values[i].name );
317 if (key->values[i].data) free( key->values[i].data );
319 if (key->values) free( key->values );
320 for (i = 0; i <= key->last_subkey; i++)
322 key->subkeys[i]->parent = NULL;
323 release_object( key->subkeys[i] );
325 if (key->subkeys) free( key->subkeys );
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 static 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, unsigned 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, size_t size )
1026 WCHAR *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( 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 = 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 - (p - info->tmp) * sizeof(WCHAR), 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] = 0;
1208 *len = 1;
1210 else
1212 if ((*len = parse_strW( 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, info->tmp, &index )))
1220 value = insert_value( key, 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( 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( (unsigned char *)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( info->tmp, &len, name, ']' )) == -1)
1304 file_read_error( "Malformed key", info );
1305 return 0;
1307 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1308 *p = 0;
1309 for (res = 1; key != root_key; res++)
1311 if (!strcmpiW( 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 static WCHAR *format_user_registry_path( const SID *sid )
1426 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1427 static const WCHAR formatW[] = {'-','%','u',0};
1428 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1429 WCHAR *p = buffer;
1430 unsigned int i;
1432 strcpyW( p, prefixW );
1433 p += strlenW( prefixW );
1434 p += sprintfW( p, formatW, sid->Revision );
1435 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1436 sid->IdentifierAuthority.Value[4] ),
1437 MAKEWORD( sid->IdentifierAuthority.Value[3],
1438 sid->IdentifierAuthority.Value[2] )));
1439 for (i = 0; i < sid->SubAuthorityCount; i++)
1440 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1442 return memdup( buffer, (p + 1 - buffer) * sizeof(WCHAR) );
1445 /* registry initialisation */
1446 void init_registry(void)
1448 static const WCHAR root_name[] = { 0 };
1449 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1450 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1451 WCHAR *current_user_path;
1453 const char *config = wine_get_config_dir();
1454 char *p, *filename;
1455 struct key *key;
1456 int dummy;
1458 /* create the root key */
1459 root_key = alloc_key( root_name, time(NULL) );
1460 assert( root_key );
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 user.reg into HKEY_CURRENT_USER */
1488 /* FIXME: match default user in token.c. should get from process token instead */
1489 current_user_path = format_user_registry_path( security_interactive_sid );
1490 if (!current_user_path ||
1491 !(key = create_key( root_key, current_user_path, NULL, 0, time(NULL), &dummy )))
1492 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1493 free( current_user_path );
1494 strcpy( p, "/user.reg" );
1495 load_init_registry_from_file( filename, key );
1496 release_object( key );
1498 free( filename );
1500 /* start the periodic save timer */
1501 set_periodic_save_timer();
1504 /* save a registry branch to a file */
1505 static void save_all_subkeys( struct key *key, FILE *f )
1507 fprintf( f, "WINE REGISTRY Version 2\n" );
1508 fprintf( f, ";; All keys relative to " );
1509 dump_path( key, NULL, f );
1510 fprintf( f, "\n" );
1511 save_subkeys( key, key, f );
1514 /* save a registry branch to a file handle */
1515 static void save_registry( struct key *key, obj_handle_t handle )
1517 struct file *file;
1518 int fd;
1520 if (key->flags & KEY_DELETED)
1522 set_error( STATUS_KEY_DELETED );
1523 return;
1525 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE ))) return;
1526 fd = dup( get_file_unix_fd( file ) );
1527 release_object( file );
1528 if (fd != -1)
1530 FILE *f = fdopen( fd, "w" );
1531 if (f)
1533 save_all_subkeys( key, f );
1534 if (fclose( f )) file_set_error();
1536 else
1538 file_set_error();
1539 close( fd );
1544 /* save a registry branch to a file */
1545 static int save_branch( struct key *key, const char *path )
1547 struct stat st;
1548 char *p, *real, *tmp = NULL;
1549 int fd, count = 0, ret = 0, by_symlink;
1550 FILE *f;
1552 if (!(key->flags & KEY_DIRTY))
1554 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1555 return 1;
1558 /* get the real path */
1560 by_symlink = (!lstat(path, &st) && S_ISLNK (st.st_mode));
1561 if (!(real = malloc( PATH_MAX ))) return 0;
1562 if (!realpath( path, real ))
1564 free( real );
1565 real = NULL;
1567 else path = real;
1569 /* test the file type */
1571 if ((fd = open( path, O_WRONLY )) != -1)
1573 /* if file is not a regular file or has multiple links or is accessed
1574 * via symbolic links, write directly into it; otherwise use a temp file */
1575 if (by_symlink ||
1576 (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)))
1578 ftruncate( fd, 0 );
1579 goto save;
1581 close( fd );
1584 /* create a temp file in the same directory */
1586 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1587 strcpy( tmp, path );
1588 if ((p = strrchr( tmp, '/' ))) p++;
1589 else p = tmp;
1590 for (;;)
1592 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1593 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1594 if (errno != EEXIST) goto done;
1595 close( fd );
1598 /* now save to it */
1600 save:
1601 if (!(f = fdopen( fd, "w" )))
1603 if (tmp) unlink( tmp );
1604 close( fd );
1605 goto done;
1608 if (debug_level > 1)
1610 fprintf( stderr, "%s: ", path );
1611 dump_operation( key, NULL, "saving" );
1614 save_all_subkeys( key, f );
1615 ret = !fclose(f);
1617 if (tmp)
1619 /* if successfully written, rename to final name */
1620 if (ret) ret = !rename( tmp, path );
1621 if (!ret) unlink( tmp );
1624 done:
1625 free( tmp );
1626 free( real );
1627 if (ret) make_clean( key );
1628 return ret;
1631 /* periodic saving of the registry */
1632 static void periodic_save( void *arg )
1634 int i;
1636 save_timeout_user = NULL;
1637 for (i = 0; i < save_branch_count; i++)
1638 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1639 set_periodic_save_timer();
1642 /* start the periodic save timer */
1643 static void set_periodic_save_timer(void)
1645 struct timeval next;
1647 gettimeofday( &next, NULL );
1648 add_timeout( &next, save_period );
1649 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1650 save_timeout_user = add_timeout_user( &next, periodic_save, NULL );
1653 /* save the modified registry branches to disk */
1654 void flush_registry(void)
1656 int i;
1658 for (i = 0; i < save_branch_count; i++)
1660 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1662 fprintf( stderr, "wineserver: could not save registry branch to %s",
1663 save_branch_info[i].path );
1664 perror( " " );
1669 /* close the top-level keys; used on server exit */
1670 void close_registry(void)
1672 int i;
1674 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1675 save_timeout_user = NULL;
1676 for (i = 0; i < save_branch_count; i++)
1678 release_object( save_branch_info[i].key );
1679 free( save_branch_info[i].path );
1681 release_object( root_key );
1685 /* create a registry key */
1686 DECL_HANDLER(create_key)
1688 struct key *key = NULL, *parent;
1689 unsigned int access = req->access;
1690 WCHAR *name, *class;
1692 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1693 reply->hkey = 0;
1694 if (!(name = copy_req_path( req->namelen, !req->parent ))) return;
1695 /* NOTE: no access rights are required from the parent handle to create a key */
1696 if ((parent = get_hkey_obj( req->parent, 0 )))
1698 int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1700 if (req->namelen == get_req_data_size()) /* no class specified */
1702 key = create_key( parent, name, NULL, flags, req->modif, &reply->created );
1704 else
1706 const WCHAR *class_ptr = (const WCHAR *)((const char *)get_req_data() + req->namelen);
1708 if ((class = req_strdupW( req, class_ptr, get_req_data_size() - req->namelen )))
1710 key = create_key( parent, name, class, flags, req->modif, &reply->created );
1711 free( class );
1714 if (key)
1716 reply->hkey = alloc_handle( current->process, key, access, 0 );
1717 release_object( key );
1719 release_object( parent );
1723 /* open a registry key */
1724 DECL_HANDLER(open_key)
1726 struct key *key, *parent;
1727 unsigned int access = req->access;
1729 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1730 reply->hkey = 0;
1731 /* NOTE: no access rights are required to open the parent key, only the child key */
1732 if ((parent = get_hkey_obj( req->parent, 0 )))
1734 WCHAR *name = copy_path( get_req_data(), get_req_data_size(), !req->parent );
1735 if (name && (key = open_key( parent, name )))
1737 reply->hkey = alloc_handle( current->process, key, access, 0 );
1738 release_object( key );
1740 release_object( parent );
1744 /* delete a registry key */
1745 DECL_HANDLER(delete_key)
1747 struct key *key;
1749 if ((key = get_hkey_obj( req->hkey, DELETE )))
1751 delete_key( key, 0);
1752 release_object( key );
1756 /* flush a registry key */
1757 DECL_HANDLER(flush_key)
1759 struct key *key = get_hkey_obj( req->hkey, 0 );
1760 if (key)
1762 /* we don't need to do anything here with the current implementation */
1763 release_object( key );
1767 /* enumerate registry subkeys */
1768 DECL_HANDLER(enum_key)
1770 struct key *key;
1772 if ((key = get_hkey_obj( req->hkey,
1773 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1775 enum_key( key, req->index, req->info_class, reply );
1776 release_object( key );
1780 /* set a value of a registry key */
1781 DECL_HANDLER(set_key_value)
1783 struct key *key;
1784 WCHAR *name;
1786 if (!(name = copy_req_path( req->namelen, 0 ))) return;
1787 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1789 size_t datalen = get_req_data_size() - req->namelen;
1790 const char *data = (const char *)get_req_data() + req->namelen;
1792 set_value( key, name, req->type, data, datalen );
1793 release_object( key );
1797 /* retrieve the value of a registry key */
1798 DECL_HANDLER(get_key_value)
1800 struct key *key;
1801 WCHAR *name;
1803 reply->total = 0;
1804 if (!(name = copy_path( get_req_data(), get_req_data_size(), 0 ))) return;
1805 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1807 get_value( key, name, &reply->type, &reply->total );
1808 release_object( key );
1812 /* enumerate the value of a registry key */
1813 DECL_HANDLER(enum_key_value)
1815 struct key *key;
1817 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1819 enum_value( key, req->index, req->info_class, reply );
1820 release_object( key );
1824 /* delete a value of a registry key */
1825 DECL_HANDLER(delete_key_value)
1827 WCHAR *name;
1828 struct key *key;
1830 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1832 if ((name = req_strdupW( req, get_req_data(), get_req_data_size() )))
1834 delete_value( key, name );
1835 free( name );
1837 release_object( key );
1841 /* load a registry branch from a file */
1842 DECL_HANDLER(load_registry)
1844 struct key *key, *parent;
1845 struct token *token = thread_get_impersonation_token( current );
1847 const LUID_AND_ATTRIBUTES privs[] =
1849 { SeBackupPrivilege, 0 },
1850 { SeRestorePrivilege, 0 },
1853 if (!token || !token_check_privileges( token, TRUE, privs,
1854 sizeof(privs)/sizeof(privs[0]), NULL ))
1856 set_error( STATUS_PRIVILEGE_NOT_HELD );
1857 return;
1860 if ((parent = get_hkey_obj( req->hkey, 0 )))
1862 int dummy;
1863 WCHAR *name = copy_path( get_req_data(), get_req_data_size(), !req->hkey );
1864 if (name && (key = create_key( parent, name, NULL, KEY_DIRTY, time(NULL), &dummy )))
1866 load_registry( key, req->file );
1867 release_object( key );
1869 release_object( parent );
1873 DECL_HANDLER(unload_registry)
1875 struct key *key;
1876 struct token *token = thread_get_impersonation_token( current );
1878 const LUID_AND_ATTRIBUTES privs[] =
1880 { SeBackupPrivilege, 0 },
1881 { SeRestorePrivilege, 0 },
1884 if (!token || !token_check_privileges( token, TRUE, privs,
1885 sizeof(privs)/sizeof(privs[0]), NULL ))
1887 set_error( STATUS_PRIVILEGE_NOT_HELD );
1888 return;
1891 if ((key = get_hkey_obj( req->hkey, 0 )))
1893 delete_key( key, 1 ); /* FIXME */
1894 release_object( key );
1898 /* save a registry branch to a file */
1899 DECL_HANDLER(save_registry)
1901 struct key *key;
1903 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
1905 set_error( STATUS_PRIVILEGE_NOT_HELD );
1906 return;
1909 if ((key = get_hkey_obj( req->hkey, 0 )))
1911 save_registry( key, req->file );
1912 release_object( key );
1916 /* add a registry key change notification */
1917 DECL_HANDLER(set_registry_notification)
1919 struct key *key;
1920 struct event *event;
1921 struct notify *notify;
1923 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
1924 if (key)
1926 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
1927 if (event)
1929 notify = find_notify( key, current->process, req->hkey );
1930 if (notify)
1932 release_object( notify->event );
1933 grab_object( event );
1934 notify->event = event;
1936 else
1938 notify = mem_alloc( sizeof(*notify) );
1939 if (notify)
1941 grab_object( event );
1942 notify->event = event;
1943 notify->subtree = req->subtree;
1944 notify->filter = req->filter;
1945 notify->hkey = req->hkey;
1946 notify->process = current->process;
1947 list_add_head( &key->notify_list, &notify->entry );
1950 release_object( event );
1952 release_object( key );