GetUpdateRgn should clip the returned region to the client area.
[wine/multimedia.git] / server / registry.c
blob7796e59860bebb7f6fb0ded79b47c64daed84e40
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 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 notify *next; /* list of notifications */
60 struct notify *prev; /* list of notifications */
63 /* a registry key */
64 struct key
66 struct object obj; /* object header */
67 WCHAR *name; /* key name */
68 WCHAR *class; /* key class */
69 struct key *parent; /* parent key */
70 int last_subkey; /* last in use subkey */
71 int nb_subkeys; /* count of allocated subkeys */
72 struct key **subkeys; /* subkeys array */
73 int last_value; /* last in use value */
74 int nb_values; /* count of allocated values in array */
75 struct key_value *values; /* values array */
76 short flags; /* flags */
77 short level; /* saving level */
78 time_t modif; /* last modification time */
79 struct notify *first_notify; /* list of notifications */
80 struct notify *last_notify; /* list of notifications */
83 /* key flags */
84 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
85 #define KEY_DELETED 0x0002 /* key has been deleted */
86 #define KEY_DIRTY 0x0004 /* key has been modified */
88 /* a key value */
89 struct key_value
91 WCHAR *name; /* value name */
92 int type; /* value type */
93 size_t len; /* value data length in bytes */
94 void *data; /* pointer to value data */
97 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
98 #define MIN_VALUES 8 /* min. number of allocated values per key */
101 /* the root of the registry tree */
102 static struct key *root_key;
104 /* keys saving level */
105 /* current_level is the level that is put into all newly created or modified keys */
106 /* saving_level is the minimum level that a key needs in order to get saved */
107 static int current_level;
108 static int saving_level;
110 static struct timeval next_save_time; /* absolute time of next periodic save */
111 static int save_period; /* delay between periodic saves (ms) */
112 static struct timeout_user *save_timeout_user; /* saving timer */
114 /* information about where to save a registry branch */
115 struct save_branch_info
117 struct key *key;
118 char *path;
121 #define MAX_SAVE_BRANCH_INFO 3
122 static int save_branch_count;
123 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
126 /* information about a file being loaded */
127 struct file_load_info
129 FILE *file; /* input file */
130 char *buffer; /* line buffer */
131 int len; /* buffer length */
132 int line; /* current input line */
133 char *tmp; /* temp buffer to use while parsing input */
134 int tmplen; /* length of temp buffer */
138 static void key_dump( struct object *obj, int verbose );
139 static void key_destroy( struct object *obj );
141 static const struct object_ops key_ops =
143 sizeof(struct key), /* size */
144 key_dump, /* dump */
145 no_add_queue, /* add_queue */
146 NULL, /* remove_queue */
147 NULL, /* signaled */
148 NULL, /* satisfied */
149 no_get_fd, /* get_fd */
150 key_destroy /* destroy */
155 * The registry text file format v2 used by this code is similar to the one
156 * used by REGEDIT import/export functionality, with the following differences:
157 * - strings and key names can contain \x escapes for Unicode
158 * - key names use escapes too in order to support Unicode
159 * - the modification time optionally follows the key name
160 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
163 static inline char to_hex( char ch )
165 if (isdigit(ch)) return ch - '0';
166 return tolower(ch) - 'a' + 10;
169 /* dump the full path of a key */
170 static void dump_path( const struct key *key, const struct key *base, FILE *f )
172 if (key->parent && key->parent != base)
174 dump_path( key->parent, base, f );
175 fprintf( f, "\\\\" );
177 dump_strW( key->name, strlenW(key->name), f, "[]" );
180 /* dump a value to a text file */
181 static void dump_value( const struct key_value *value, FILE *f )
183 int i, count;
185 if (value->name[0])
187 fputc( '\"', f );
188 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
189 count += fprintf( f, "\"=" );
191 else count = fprintf( f, "@=" );
193 switch(value->type)
195 case REG_SZ:
196 case REG_EXPAND_SZ:
197 case REG_MULTI_SZ:
198 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
199 fputc( '\"', f );
200 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
201 fputc( '\"', f );
202 break;
203 case REG_DWORD:
204 if (value->len == sizeof(DWORD))
206 DWORD dw;
207 memcpy( &dw, value->data, sizeof(DWORD) );
208 fprintf( f, "dword:%08lx", dw );
209 break;
211 /* else fall through */
212 default:
213 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
214 else count += fprintf( f, "hex(%x):", value->type );
215 for (i = 0; i < value->len; i++)
217 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
218 if (i < value->len-1)
220 fputc( ',', f );
221 if (++count > 76)
223 fprintf( f, "\\\n " );
224 count = 2;
228 break;
230 fputc( '\n', f );
233 /* save a registry and all its subkeys to a text file */
234 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
236 int i;
238 if (key->flags & KEY_VOLATILE) return;
239 /* save key if it has the proper level, and has either some values or no subkeys */
240 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
241 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
243 fprintf( f, "\n[" );
244 if (key != base) dump_path( key, base, f );
245 fprintf( f, "] %ld\n", (long)key->modif );
246 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
248 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
251 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
253 fprintf( stderr, "%s key ", op );
254 if (key) dump_path( key, NULL, stderr );
255 else fprintf( stderr, "ERROR" );
256 if (value)
258 fprintf( stderr, " value ");
259 dump_value( value, stderr );
261 else fprintf( stderr, "\n" );
264 static void key_dump( struct object *obj, int verbose )
266 struct key *key = (struct key *)obj;
267 assert( obj->ops == &key_ops );
268 fprintf( stderr, "Key flags=%x ", key->flags );
269 dump_path( key, NULL, stderr );
270 fprintf( stderr, "\n" );
273 /* notify waiter and maybe delete the notification */
274 static void do_notification( struct key *key, struct notify *notify, int del )
276 if( notify->event )
278 set_event( notify->event );
279 release_object( notify->event );
280 notify->event = NULL;
283 if ( !del )
284 return;
285 if( notify->next )
286 notify->next->prev = notify->prev;
287 else
288 key->last_notify = notify->prev;
289 if( notify->prev )
290 notify->prev->next = notify->next;
291 else
292 key->first_notify = notify->next;
293 free( notify );
296 static struct notify *find_notify( struct key *key, obj_handle_t hkey)
298 struct notify *n;
300 for( n=key->first_notify; n; n = n->next)
301 if( n->hkey == hkey )
302 break;
303 return n;
306 /* close the notification associated with a handle */
307 void registry_close_handle( struct object *obj, obj_handle_t hkey )
309 struct key * key = (struct key *) obj;
310 struct notify *notify;
312 if( obj->ops != &key_ops )
313 return;
314 notify = find_notify( key, hkey );
315 if( !notify )
316 return;
317 do_notification( key, notify, 1 );
320 static void key_destroy( struct object *obj )
322 int i;
323 struct key *key = (struct key *)obj;
324 assert( obj->ops == &key_ops );
326 if (key->name) free( key->name );
327 if (key->class) free( key->class );
328 for (i = 0; i <= key->last_value; i++)
330 free( key->values[i].name );
331 if (key->values[i].data) free( key->values[i].data );
333 for (i = 0; i <= key->last_subkey; i++)
335 key->subkeys[i]->parent = NULL;
336 release_object( key->subkeys[i] );
338 /* unconditionally notify everything waiting on this key */
339 while ( key->first_notify )
340 do_notification( key, key->first_notify, 1 );
343 /* duplicate a key path */
344 /* returns a pointer to a static buffer, so only useable once per request */
345 static WCHAR *copy_path( const WCHAR *path, size_t len, int skip_root )
347 static WCHAR buffer[MAX_PATH+1];
348 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\',0 };
350 if (len > sizeof(buffer)-sizeof(buffer[0]))
352 set_error( STATUS_BUFFER_OVERFLOW );
353 return NULL;
355 memcpy( buffer, path, len );
356 buffer[len / sizeof(WCHAR)] = 0;
357 if (skip_root && !strncmpiW( buffer, root_name, 10 )) return buffer + 10;
358 return buffer;
361 /* copy a path from the request buffer */
362 static WCHAR *copy_req_path( size_t len, int skip_root )
364 const WCHAR *name_ptr = get_req_data();
365 if (len > get_req_data_size())
367 fatal_protocol_error( current, "copy_req_path: invalid length %d/%d\n",
368 len, get_req_data_size() );
369 return NULL;
371 return copy_path( name_ptr, len, skip_root );
374 /* return the next token in a given path */
375 /* returns a pointer to a static buffer, so only useable once per request */
376 static WCHAR *get_path_token( WCHAR *initpath )
378 static WCHAR *path;
379 WCHAR *ret;
381 if (initpath)
383 /* path cannot start with a backslash */
384 if (*initpath == '\\')
386 set_error( STATUS_OBJECT_PATH_INVALID );
387 return NULL;
389 path = initpath;
391 else while (*path == '\\') path++;
393 ret = path;
394 while (*path && *path != '\\') path++;
395 if (*path) *path++ = 0;
396 return ret;
399 /* duplicate a Unicode string from the request buffer */
400 static WCHAR *req_strdupW( const void *req, const WCHAR *str, size_t len )
402 WCHAR *name;
403 if ((name = mem_alloc( len + sizeof(WCHAR) )) != NULL)
405 memcpy( name, str, len );
406 name[len / sizeof(WCHAR)] = 0;
408 return name;
411 /* allocate a key object */
412 static struct key *alloc_key( const WCHAR *name, time_t modif )
414 struct key *key;
415 if ((key = alloc_object( &key_ops )))
417 key->class = NULL;
418 key->flags = 0;
419 key->last_subkey = -1;
420 key->nb_subkeys = 0;
421 key->subkeys = NULL;
422 key->nb_values = 0;
423 key->last_value = -1;
424 key->values = NULL;
425 key->level = current_level;
426 key->modif = modif;
427 key->parent = NULL;
428 key->first_notify = NULL;
429 key->last_notify = NULL;
430 if (!(key->name = strdupW( name )))
432 release_object( key );
433 key = NULL;
436 return key;
439 /* mark a key and all its parents as dirty (modified) */
440 static void make_dirty( struct key *key )
442 while (key)
444 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
445 key->flags |= KEY_DIRTY;
446 key = key->parent;
450 /* mark a key and all its subkeys as clean (not modified) */
451 static void make_clean( struct key *key )
453 int i;
455 if (key->flags & KEY_VOLATILE) return;
456 if (!(key->flags & KEY_DIRTY)) return;
457 key->flags &= ~KEY_DIRTY;
458 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
461 /* go through all the notifications and send them if necessary */
462 void check_notify( struct key *key, unsigned int change, int not_subtree )
464 struct notify *n = key->first_notify;
465 while (n)
467 struct notify *next = n->next;
468 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
469 do_notification( key, n, 0 );
470 n = next;
474 /* update key modification time */
475 static void touch_key( struct key *key, unsigned int change )
477 struct key *k;
479 key->modif = time(NULL);
480 key->level = max( key->level, current_level );
481 make_dirty( key );
483 /* do notifications */
484 check_notify( key, change, 1 );
485 for ( k = key->parent; k; k = k->parent )
486 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
489 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
490 static int grow_subkeys( struct key *key )
492 struct key **new_subkeys;
493 int nb_subkeys;
495 if (key->nb_subkeys)
497 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
498 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
500 set_error( STATUS_NO_MEMORY );
501 return 0;
504 else
506 nb_subkeys = MIN_VALUES;
507 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
509 key->subkeys = new_subkeys;
510 key->nb_subkeys = nb_subkeys;
511 return 1;
514 /* allocate a subkey for a given key, and return its index */
515 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
517 struct key *key;
518 int i;
520 if (parent->last_subkey + 1 == parent->nb_subkeys)
522 /* need to grow the array */
523 if (!grow_subkeys( parent )) return NULL;
525 if ((key = alloc_key( name, modif )) != NULL)
527 key->parent = parent;
528 for (i = ++parent->last_subkey; i > index; i--)
529 parent->subkeys[i] = parent->subkeys[i-1];
530 parent->subkeys[index] = key;
532 return key;
535 /* free a subkey of a given key */
536 static void free_subkey( struct key *parent, int index )
538 struct key *key;
539 int i, nb_subkeys;
541 assert( index >= 0 );
542 assert( index <= parent->last_subkey );
544 key = parent->subkeys[index];
545 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
546 parent->last_subkey--;
547 key->flags |= KEY_DELETED;
548 key->parent = NULL;
549 release_object( key );
551 /* try to shrink the array */
552 nb_subkeys = parent->nb_subkeys;
553 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
555 struct key **new_subkeys;
556 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
557 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
558 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
559 parent->subkeys = new_subkeys;
560 parent->nb_subkeys = nb_subkeys;
564 /* find the named child of a given key and return its index */
565 static struct key *find_subkey( const struct key *key, const WCHAR *name, int *index )
567 int i, min, max, res;
569 min = 0;
570 max = key->last_subkey;
571 while (min <= max)
573 i = (min + max) / 2;
574 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
576 *index = i;
577 return key->subkeys[i];
579 if (res > 0) max = i - 1;
580 else min = i + 1;
582 *index = min; /* this is where we should insert it */
583 return NULL;
586 /* open a subkey */
587 /* warning: the key name must be writeable (use copy_path) */
588 static struct key *open_key( struct key *key, WCHAR *name )
590 int index;
591 WCHAR *path;
593 if (!(path = get_path_token( name ))) return NULL;
594 while (*path)
596 if (!(key = find_subkey( key, path, &index )))
598 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
599 break;
601 path = get_path_token( NULL );
604 if (debug_level > 1) dump_operation( key, NULL, "Open" );
605 if (key) grab_object( key );
606 return key;
609 /* create a subkey */
610 /* warning: the key name must be writeable (use copy_path) */
611 static struct key *create_key( struct key *key, WCHAR *name, WCHAR *class,
612 int flags, time_t modif, int *created )
614 struct key *base;
615 int base_idx, index;
616 WCHAR *path;
618 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
620 set_error( STATUS_KEY_DELETED );
621 return NULL;
623 if (!(flags & KEY_VOLATILE) && (key->flags & KEY_VOLATILE))
625 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
626 return NULL;
628 if (!modif) modif = time(NULL);
630 if (!(path = get_path_token( name ))) return NULL;
631 *created = 0;
632 while (*path)
634 struct key *subkey;
635 if (!(subkey = find_subkey( key, path, &index ))) break;
636 key = subkey;
637 path = get_path_token( NULL );
640 /* create the remaining part */
642 if (!*path) goto done;
643 *created = 1;
644 if (flags & KEY_DIRTY) make_dirty( key );
645 base = key;
646 base_idx = index;
647 key = alloc_subkey( key, path, index, modif );
648 while (key)
650 key->flags |= flags;
651 path = get_path_token( NULL );
652 if (!*path) goto done;
653 /* we know the index is always 0 in a new key */
654 key = alloc_subkey( key, path, 0, modif );
656 if (base_idx != -1) free_subkey( base, base_idx );
657 return NULL;
659 done:
660 if (debug_level > 1) dump_operation( key, NULL, "Create" );
661 if (class) key->class = strdupW(class);
662 grab_object( key );
663 return key;
666 /* query information about a key or a subkey */
667 static void enum_key( const struct key *key, int index, int info_class,
668 struct enum_key_reply *reply )
670 int i;
671 size_t len, namelen, classlen;
672 int max_subkey = 0, max_class = 0;
673 int max_value = 0, max_data = 0;
674 WCHAR *data;
676 if (index != -1) /* -1 means use the specified key directly */
678 if ((index < 0) || (index > key->last_subkey))
680 set_error( STATUS_NO_MORE_ENTRIES );
681 return;
683 key = key->subkeys[index];
686 namelen = strlenW(key->name) * sizeof(WCHAR);
687 classlen = key->class ? strlenW(key->class) * sizeof(WCHAR) : 0;
689 switch(info_class)
691 case KeyBasicInformation:
692 classlen = 0; /* only return the name */
693 /* fall through */
694 case KeyNodeInformation:
695 reply->max_subkey = 0;
696 reply->max_class = 0;
697 reply->max_value = 0;
698 reply->max_data = 0;
699 break;
700 case KeyFullInformation:
701 for (i = 0; i <= key->last_subkey; i++)
703 struct key *subkey = key->subkeys[i];
704 len = strlenW( subkey->name );
705 if (len > max_subkey) max_subkey = len;
706 if (!subkey->class) continue;
707 len = strlenW( subkey->class );
708 if (len > max_class) max_class = len;
710 for (i = 0; i <= key->last_value; i++)
712 len = strlenW( key->values[i].name );
713 if (len > max_value) max_value = len;
714 len = key->values[i].len;
715 if (len > max_data) max_data = len;
717 reply->max_subkey = max_subkey;
718 reply->max_class = max_class;
719 reply->max_value = max_value;
720 reply->max_data = max_data;
721 namelen = 0; /* only return the class */
722 break;
723 default:
724 set_error( STATUS_INVALID_PARAMETER );
725 return;
727 reply->subkeys = key->last_subkey + 1;
728 reply->values = key->last_value + 1;
729 reply->modif = key->modif;
730 reply->total = namelen + classlen;
732 len = min( reply->total, get_reply_max_size() );
733 if (len && (data = set_reply_data_size( len )))
735 if (len > namelen)
737 reply->namelen = namelen;
738 memcpy( data, key->name, namelen );
739 memcpy( (char *)data + namelen, key->class, len - namelen );
741 else
743 reply->namelen = len;
744 memcpy( data, key->name, len );
747 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
750 /* delete a key and its values */
751 static int delete_key( struct key *key, int recurse )
753 int index;
754 struct key *parent;
756 /* must find parent and index */
757 if (key == root_key)
759 set_error( STATUS_ACCESS_DENIED );
760 return -1;
762 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
764 set_error( STATUS_KEY_DELETED );
765 return -1;
768 while (recurse && (key->last_subkey>=0))
769 if(0>delete_key(key->subkeys[key->last_subkey], 1))
770 return -1;
772 for (index = 0; index <= parent->last_subkey; index++)
773 if (parent->subkeys[index] == key) break;
774 assert( index <= parent->last_subkey );
776 /* we can only delete a key that has no subkeys */
777 if (key->last_subkey >= 0)
779 set_error( STATUS_ACCESS_DENIED );
780 return -1;
783 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
784 free_subkey( parent, index );
785 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
786 return 0;
789 /* try to grow the array of values; return 1 if OK, 0 on error */
790 static int grow_values( struct key *key )
792 struct key_value *new_val;
793 int nb_values;
795 if (key->nb_values)
797 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
798 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
800 set_error( STATUS_NO_MEMORY );
801 return 0;
804 else
806 nb_values = MIN_VALUES;
807 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
809 key->values = new_val;
810 key->nb_values = nb_values;
811 return 1;
814 /* find the named value of a given key and return its index in the array */
815 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
817 int i, min, max, res;
819 min = 0;
820 max = key->last_value;
821 while (min <= max)
823 i = (min + max) / 2;
824 if (!(res = strcmpiW( key->values[i].name, name )))
826 *index = i;
827 return &key->values[i];
829 if (res > 0) max = i - 1;
830 else min = i + 1;
832 *index = min; /* this is where we should insert it */
833 return NULL;
836 /* insert a new value; the index must have been returned by find_value */
837 static struct key_value *insert_value( struct key *key, const WCHAR *name, int index )
839 struct key_value *value;
840 WCHAR *new_name;
841 int i;
843 if (key->last_value + 1 == key->nb_values)
845 if (!grow_values( key )) return NULL;
847 if (!(new_name = strdupW(name))) return NULL;
848 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
849 value = &key->values[index];
850 value->name = new_name;
851 value->len = 0;
852 value->data = NULL;
853 return value;
856 /* set a key value */
857 static void set_value( struct key *key, WCHAR *name, int type, const void *data, size_t len )
859 struct key_value *value;
860 void *ptr = NULL;
861 int index;
863 if ((value = find_value( key, name, &index )))
865 /* check if the new value is identical to the existing one */
866 if (value->type == type && value->len == len &&
867 value->data && !memcmp( value->data, data, len ))
869 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
870 return;
874 if (len && !(ptr = memdup( data, len ))) return;
876 if (!value)
878 if (!(value = insert_value( key, name, index )))
880 if (ptr) free( ptr );
881 return;
884 else if (value->data) free( value->data ); /* already existing, free previous data */
886 value->type = type;
887 value->len = len;
888 value->data = ptr;
889 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
890 if (debug_level > 1) dump_operation( key, value, "Set" );
893 /* get a key value */
894 static void get_value( struct key *key, const WCHAR *name, int *type, int *len )
896 struct key_value *value;
897 int index;
899 if ((value = find_value( key, name, &index )))
901 *type = value->type;
902 *len = value->len;
903 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
904 if (debug_level > 1) dump_operation( key, value, "Get" );
906 else
908 *type = -1;
909 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
913 /* enumerate a key value */
914 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
916 struct key_value *value;
918 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
919 else
921 void *data;
922 size_t namelen, maxlen;
924 value = &key->values[i];
925 reply->type = value->type;
926 namelen = strlenW( value->name ) * sizeof(WCHAR);
928 switch(info_class)
930 case KeyValueBasicInformation:
931 reply->total = namelen;
932 break;
933 case KeyValueFullInformation:
934 reply->total = namelen + value->len;
935 break;
936 case KeyValuePartialInformation:
937 reply->total = value->len;
938 namelen = 0;
939 break;
940 default:
941 set_error( STATUS_INVALID_PARAMETER );
942 return;
945 maxlen = min( reply->total, get_reply_max_size() );
946 if (maxlen && ((data = set_reply_data_size( maxlen ))))
948 if (maxlen > namelen)
950 reply->namelen = namelen;
951 memcpy( data, value->name, namelen );
952 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
954 else
956 reply->namelen = maxlen;
957 memcpy( data, value->name, maxlen );
960 if (debug_level > 1) dump_operation( key, value, "Enum" );
964 /* delete a value */
965 static void delete_value( struct key *key, const WCHAR *name )
967 struct key_value *value;
968 int i, index, nb_values;
970 if (!(value = find_value( key, name, &index )))
972 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
973 return;
975 if (debug_level > 1) dump_operation( key, value, "Delete" );
976 free( value->name );
977 if (value->data) free( value->data );
978 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
979 key->last_value--;
980 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
982 /* try to shrink the array */
983 nb_values = key->nb_values;
984 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
986 struct key_value *new_val;
987 nb_values -= nb_values / 3; /* shrink by 33% */
988 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
989 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
990 key->values = new_val;
991 key->nb_values = nb_values;
995 /* get the registry key corresponding to an hkey handle */
996 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
998 if (!hkey) return (struct key *)grab_object( root_key );
999 return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1002 /* read a line from the input file */
1003 static int read_next_line( struct file_load_info *info )
1005 char *newbuf;
1006 int newlen, pos = 0;
1008 info->line++;
1009 for (;;)
1011 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1012 return (pos != 0); /* EOF */
1013 pos = strlen(info->buffer);
1014 if (info->buffer[pos-1] == '\n')
1016 /* got a full line */
1017 info->buffer[--pos] = 0;
1018 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1019 return 1;
1021 if (pos < info->len - 1) return 1; /* EOF but something was read */
1023 /* need to enlarge the buffer */
1024 newlen = info->len + info->len / 2;
1025 if (!(newbuf = realloc( info->buffer, newlen )))
1027 set_error( STATUS_NO_MEMORY );
1028 return -1;
1030 info->buffer = newbuf;
1031 info->len = newlen;
1035 /* make sure the temp buffer holds enough space */
1036 static int get_file_tmp_space( struct file_load_info *info, int size )
1038 char *tmp;
1039 if (info->tmplen >= size) return 1;
1040 if (!(tmp = realloc( info->tmp, size )))
1042 set_error( STATUS_NO_MEMORY );
1043 return 0;
1045 info->tmp = tmp;
1046 info->tmplen = size;
1047 return 1;
1050 /* report an error while loading an input file */
1051 static void file_read_error( const char *err, struct file_load_info *info )
1053 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
1056 /* parse an escaped string back into Unicode */
1057 /* return the number of chars read from the input, or -1 on output overflow */
1058 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
1060 int count = sizeof(WCHAR); /* for terminating null */
1061 const char *p = src;
1062 while (*p && *p != endchar)
1064 if (*p != '\\') *dest = (WCHAR)*p++;
1065 else
1067 p++;
1068 switch(*p)
1070 case 'a': *dest = '\a'; p++; break;
1071 case 'b': *dest = '\b'; p++; break;
1072 case 'e': *dest = '\e'; p++; break;
1073 case 'f': *dest = '\f'; p++; break;
1074 case 'n': *dest = '\n'; p++; break;
1075 case 'r': *dest = '\r'; p++; break;
1076 case 't': *dest = '\t'; p++; break;
1077 case 'v': *dest = '\v'; p++; break;
1078 case 'x': /* hex escape */
1079 p++;
1080 if (!isxdigit(*p)) *dest = 'x';
1081 else
1083 *dest = to_hex(*p++);
1084 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1085 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1086 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1088 break;
1089 case '0':
1090 case '1':
1091 case '2':
1092 case '3':
1093 case '4':
1094 case '5':
1095 case '6':
1096 case '7': /* octal escape */
1097 *dest = *p++ - '0';
1098 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1099 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1100 break;
1101 default:
1102 *dest = (WCHAR)*p++;
1103 break;
1106 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1107 dest++;
1109 *dest = 0;
1110 if (!*p) return -1; /* delimiter not found */
1111 *len = count;
1112 return p + 1 - src;
1115 /* convert a data type tag to a value type */
1116 static int get_data_type( const char *buffer, int *type, int *parse_type )
1118 struct data_type { const char *tag; int len; int type; int parse_type; };
1120 static const struct data_type data_types[] =
1121 { /* actual type */ /* type to assume for parsing */
1122 { "\"", 1, REG_SZ, REG_SZ },
1123 { "str:\"", 5, REG_SZ, REG_SZ },
1124 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1125 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1126 { "hex:", 4, REG_BINARY, REG_BINARY },
1127 { "dword:", 6, REG_DWORD, REG_DWORD },
1128 { "hex(", 4, -1, REG_BINARY },
1129 { NULL, 0, 0, 0 }
1132 const struct data_type *ptr;
1133 char *end;
1135 for (ptr = data_types; ptr->tag; ptr++)
1137 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1138 *parse_type = ptr->parse_type;
1139 if ((*type = ptr->type) != -1) return ptr->len;
1140 /* "hex(xx):" is special */
1141 *type = (int)strtoul( buffer + 4, &end, 16 );
1142 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1143 return end + 2 - buffer;
1145 return 0;
1148 /* load and create a key from the input file */
1149 static struct key *load_key( struct key *base, const char *buffer, int flags,
1150 int prefix_len, struct file_load_info *info,
1151 int default_modif )
1153 WCHAR *p, *name;
1154 int res, len, modif;
1156 len = strlen(buffer) * sizeof(WCHAR);
1157 if (!get_file_tmp_space( info, len )) return NULL;
1159 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1161 file_read_error( "Malformed key", info );
1162 return NULL;
1164 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = default_modif;
1166 p = (WCHAR *)info->tmp;
1167 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1169 if (!*p)
1171 if (prefix_len > 1)
1173 file_read_error( "Malformed key", info );
1174 return NULL;
1176 /* empty key name, return base key */
1177 return (struct key *)grab_object( base );
1179 if (!(name = copy_path( p, len - ((char *)p - info->tmp), 0 )))
1181 file_read_error( "Key is too long", info );
1182 return NULL;
1184 return create_key( base, name, NULL, flags, modif, &res );
1187 /* parse a comma-separated list of hex digits */
1188 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1190 const char *p = buffer;
1191 int count = 0;
1192 while (isxdigit(*p))
1194 int val;
1195 char buf[3];
1196 memcpy( buf, p, 2 );
1197 buf[2] = 0;
1198 sscanf( buf, "%x", &val );
1199 if (count++ >= *len) return -1; /* dest buffer overflow */
1200 *dest++ = (unsigned char )val;
1201 p += 2;
1202 if (*p == ',') p++;
1204 *len = count;
1205 return p - buffer;
1208 /* parse a value name and create the corresponding value */
1209 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1210 struct file_load_info *info )
1212 struct key_value *value;
1213 int index, maxlen;
1215 maxlen = strlen(buffer) * sizeof(WCHAR);
1216 if (!get_file_tmp_space( info, maxlen )) return NULL;
1217 if (buffer[0] == '@')
1219 info->tmp[0] = info->tmp[1] = 0;
1220 *len = 1;
1222 else
1224 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1225 (*len)++; /* for initial quote */
1227 while (isspace(buffer[*len])) (*len)++;
1228 if (buffer[*len] != '=') goto error;
1229 (*len)++;
1230 while (isspace(buffer[*len])) (*len)++;
1231 if (!(value = find_value( key, (WCHAR *)info->tmp, &index )))
1232 value = insert_value( key, (WCHAR *)info->tmp, index );
1233 return value;
1235 error:
1236 file_read_error( "Malformed value name", info );
1237 return NULL;
1240 /* load a value from the input file */
1241 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1243 DWORD dw;
1244 void *ptr, *newptr;
1245 int maxlen, len, res;
1246 int type, parse_type;
1247 struct key_value *value;
1249 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1250 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1251 buffer += len + res;
1253 switch(parse_type)
1255 case REG_SZ:
1256 len = strlen(buffer) * sizeof(WCHAR);
1257 if (!get_file_tmp_space( info, len )) return 0;
1258 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1259 ptr = info->tmp;
1260 break;
1261 case REG_DWORD:
1262 dw = strtoul( buffer, NULL, 16 );
1263 ptr = &dw;
1264 len = sizeof(dw);
1265 break;
1266 case REG_BINARY: /* hex digits */
1267 len = 0;
1268 for (;;)
1270 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1271 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1272 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1273 len += maxlen;
1274 buffer += res;
1275 while (isspace(*buffer)) buffer++;
1276 if (!*buffer) break;
1277 if (*buffer != '\\') goto error;
1278 if (read_next_line( info) != 1) goto error;
1279 buffer = info->buffer;
1280 while (isspace(*buffer)) buffer++;
1282 ptr = info->tmp;
1283 break;
1284 default:
1285 assert(0);
1286 ptr = NULL; /* keep compiler quiet */
1287 break;
1290 if (!len) newptr = NULL;
1291 else if (!(newptr = memdup( ptr, len ))) return 0;
1293 if (value->data) free( value->data );
1294 value->data = newptr;
1295 value->len = len;
1296 value->type = type;
1297 /* update the key level but not the modification time */
1298 key->level = max( key->level, current_level );
1299 make_dirty( key );
1300 return 1;
1302 error:
1303 file_read_error( "Malformed value", info );
1304 return 0;
1307 /* return the length (in path elements) of name that is part of the key name */
1308 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1309 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1311 WCHAR *p;
1312 int res;
1313 int len = strlen(name) * sizeof(WCHAR);
1314 if (!get_file_tmp_space( info, len )) return 0;
1316 if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
1318 file_read_error( "Malformed key", info );
1319 return 0;
1321 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
1322 *p = 0;
1323 for (res = 1; key != root_key; res++)
1325 if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
1326 key = key->parent;
1328 if (key == root_key) res = 0; /* no matching name */
1329 return res;
1332 /* load all the keys from the input file */
1333 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1334 static void load_keys( struct key *key, FILE *f, int prefix_len )
1336 struct key *subkey = NULL;
1337 struct file_load_info info;
1338 char *p;
1339 int default_modif = time(NULL);
1340 int flags = (key->flags & KEY_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1342 info.file = f;
1343 info.len = 4;
1344 info.tmplen = 4;
1345 info.line = 0;
1346 if (!(info.buffer = mem_alloc( info.len ))) return;
1347 if (!(info.tmp = mem_alloc( info.tmplen )))
1349 free( info.buffer );
1350 return;
1353 if ((read_next_line( &info ) != 1) ||
1354 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1356 set_error( STATUS_NOT_REGISTRY_FILE );
1357 goto done;
1360 while (read_next_line( &info ) == 1)
1362 p = info.buffer;
1363 while (*p && isspace(*p)) p++;
1364 switch(*p)
1366 case '[': /* new key */
1367 if (subkey) release_object( subkey );
1368 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1369 if (!(subkey = load_key( key, p + 1, flags, prefix_len, &info, default_modif )))
1370 file_read_error( "Error creating key", &info );
1371 break;
1372 case '@': /* default value */
1373 case '\"': /* value */
1374 if (subkey) load_value( subkey, p, &info );
1375 else file_read_error( "Value without key", &info );
1376 break;
1377 case '#': /* comment */
1378 case ';': /* comment */
1379 case 0: /* empty line */
1380 break;
1381 default:
1382 file_read_error( "Unrecognized input", &info );
1383 break;
1387 done:
1388 if (subkey) release_object( subkey );
1389 free( info.buffer );
1390 free( info.tmp );
1393 /* load a part of the registry from a file */
1394 static void load_registry( struct key *key, obj_handle_t handle )
1396 struct file *file;
1397 int fd;
1399 if (!(file = get_file_obj( current->process, handle, GENERIC_READ ))) return;
1400 fd = dup( get_file_unix_fd( file ) );
1401 release_object( file );
1402 if (fd != -1)
1404 FILE *f = fdopen( fd, "r" );
1405 if (f)
1407 load_keys( key, f, -1 );
1408 fclose( f );
1410 else file_set_error();
1414 /* load one of the initial registry files */
1415 static void load_init_registry_from_file( const char *filename, struct key *key )
1417 FILE *f;
1419 if ((f = fopen( filename, "r" )))
1421 load_keys( key, f, 0 );
1422 fclose( f );
1423 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1424 fatal_error( "%s is not a valid registry file\n", filename );
1425 if (get_error())
1426 fatal_error( "loading %s failed with error %x\n", filename, get_error() );
1429 if (!(key->flags & KEY_VOLATILE))
1431 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1433 if ((save_branch_info[save_branch_count].path = strdup( filename )))
1434 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1438 /* load the user registry files */
1439 static void load_user_registries( struct key *key_current_user )
1441 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1442 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1444 const char *config = wine_get_config_dir();
1445 char *p, *filename;
1446 struct key *key;
1447 int dummy;
1449 if (!(filename = mem_alloc( strlen(config) + 16 ))) return;
1450 strcpy( filename, config );
1451 p = filename + strlen(filename);
1453 /* load system.reg into Registry\Machine */
1455 if (!(key = create_key( root_key, copy_path( HKLM, sizeof(HKLM), 0 ),
1456 NULL, 0, time(NULL), &dummy )))
1457 fatal_error( "could not create Machine registry key\n" );
1459 strcpy( p, "/system.reg" );
1460 load_init_registry_from_file( filename, key );
1461 release_object( key );
1463 /* load userdef.reg into Registry\User\.Default */
1465 if (!(key = create_key( root_key, copy_path( HKU_default, sizeof(HKU_default), 0 ),
1466 NULL, 0, time(NULL), &dummy )))
1467 fatal_error( "could not create User\\.Default registry key\n" );
1469 strcpy( p, "/userdef.reg" );
1470 load_init_registry_from_file( filename, key );
1471 release_object( key );
1473 /* load user.reg into HKEY_CURRENT_USER */
1475 strcpy( p, "/user.reg" );
1476 load_init_registry_from_file( filename, key_current_user );
1478 free( filename );
1481 /* registry initialisation */
1482 void init_registry(void)
1484 static const WCHAR root_name[] = { 0 };
1485 static const WCHAR config_name[] =
1486 { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
1487 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 };
1489 const char *config = wine_get_config_dir();
1490 char *filename;
1491 struct key *key;
1492 int dummy;
1494 /* create the root key */
1495 root_key = alloc_key( root_name, time(NULL) );
1496 assert( root_key );
1498 /* load the config file */
1499 if (!(filename = malloc( strlen(config) + sizeof("/config") ))) fatal_error( "out of memory\n" );
1500 strcpy( filename, config );
1501 strcat( filename, "/config" );
1503 if (!(key = create_key( root_key, copy_path( config_name, sizeof(config_name), 0 ),
1504 NULL, 0, time(NULL), &dummy )))
1505 fatal_error( "could not create Config registry key\n" );
1507 key->flags |= KEY_VOLATILE;
1508 load_init_registry_from_file( filename, key );
1509 release_object( key );
1511 free( filename );
1514 /* update the level of the parents of a key (only needed for the old format) */
1515 static int update_level( struct key *key )
1517 int i;
1518 int max = key->level;
1519 for (i = 0; i <= key->last_subkey; i++)
1521 int sub = update_level( key->subkeys[i] );
1522 if (sub > max) max = sub;
1524 key->level = max;
1525 return max;
1528 /* save a registry branch to a file */
1529 static void save_all_subkeys( struct key *key, FILE *f )
1531 fprintf( f, "WINE REGISTRY Version 2\n" );
1532 fprintf( f, ";; All keys relative to " );
1533 dump_path( key, NULL, f );
1534 fprintf( f, "\n" );
1535 save_subkeys( key, key, f );
1538 /* save a registry branch to a file handle */
1539 static void save_registry( struct key *key, obj_handle_t handle )
1541 struct file *file;
1542 int fd;
1544 if (key->flags & KEY_DELETED)
1546 set_error( STATUS_KEY_DELETED );
1547 return;
1549 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE ))) return;
1550 fd = dup( get_file_unix_fd( file ) );
1551 release_object( file );
1552 if (fd != -1)
1554 FILE *f = fdopen( fd, "w" );
1555 if (f)
1557 save_all_subkeys( key, f );
1558 if (fclose( f )) file_set_error();
1560 else
1562 file_set_error();
1563 close( fd );
1568 /* save a registry branch to a file */
1569 static int save_branch( struct key *key, const char *path )
1571 struct stat st;
1572 char *p, *real, *tmp = NULL;
1573 int fd, count = 0, ret = 0, by_symlink;
1574 FILE *f;
1576 if (!(key->flags & KEY_DIRTY))
1578 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1579 return 1;
1582 /* get the real path */
1584 by_symlink = (!lstat(path, &st) && S_ISLNK (st.st_mode));
1585 if (!(real = malloc( PATH_MAX ))) return 0;
1586 if (!realpath( path, real ))
1588 free( real );
1589 real = NULL;
1591 else path = real;
1593 /* test the file type */
1595 if ((fd = open( path, O_WRONLY )) != -1)
1597 /* if file is not a regular file or has multiple links or is accessed
1598 * via symbolic links, write directly into it; otherwise use a temp file */
1599 if (by_symlink ||
1600 (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)))
1602 ftruncate( fd, 0 );
1603 goto save;
1605 close( fd );
1608 /* create a temp file in the same directory */
1610 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1611 strcpy( tmp, path );
1612 if ((p = strrchr( tmp, '/' ))) p++;
1613 else p = tmp;
1614 for (;;)
1616 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1617 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1618 if (errno != EEXIST) goto done;
1619 close( fd );
1622 /* now save to it */
1624 save:
1625 if (!(f = fdopen( fd, "w" )))
1627 if (tmp) unlink( tmp );
1628 close( fd );
1629 goto done;
1632 if (debug_level > 1)
1634 fprintf( stderr, "%s: ", path );
1635 dump_operation( key, NULL, "saving" );
1638 save_all_subkeys( key, f );
1639 ret = !fclose(f);
1641 if (tmp)
1643 /* if successfully written, rename to final name */
1644 if (ret) ret = !rename( tmp, path );
1645 if (!ret) unlink( tmp );
1646 free( tmp );
1649 done:
1650 if (real) free( real );
1651 if (ret) make_clean( key );
1652 return ret;
1655 /* periodic saving of the registry */
1656 static void periodic_save( void *arg )
1658 int i;
1659 for (i = 0; i < save_branch_count; i++)
1660 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1661 add_timeout( &next_save_time, save_period );
1662 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1665 /* save the modified registry branches to disk */
1666 void flush_registry(void)
1668 int i;
1670 for (i = 0; i < save_branch_count; i++)
1672 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1674 fprintf( stderr, "wineserver: could not save registry branch to %s",
1675 save_branch_info[i].path );
1676 perror( " " );
1681 /* close the top-level keys; used on server exit */
1682 void close_registry(void)
1684 int i;
1686 for (i = 0; i < save_branch_count; i++) release_object( save_branch_info[i].key );
1687 release_object( root_key );
1691 /* create a registry key */
1692 DECL_HANDLER(create_key)
1694 struct key *key = NULL, *parent;
1695 unsigned int access = req->access;
1696 WCHAR *name, *class;
1698 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1699 reply->hkey = 0;
1700 if (!(name = copy_req_path( req->namelen, !req->parent ))) return;
1701 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1703 int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1705 if (req->namelen == get_req_data_size()) /* no class specified */
1707 key = create_key( parent, name, NULL, flags, req->modif, &reply->created );
1709 else
1711 const WCHAR *class_ptr = (WCHAR *)((char *)get_req_data() + req->namelen);
1713 if ((class = req_strdupW( req, class_ptr, get_req_data_size() - req->namelen )))
1715 key = create_key( parent, name, class, flags, req->modif, &reply->created );
1716 free( class );
1719 if (key)
1721 reply->hkey = alloc_handle( current->process, key, access, 0 );
1722 release_object( key );
1724 release_object( parent );
1728 /* open a registry key */
1729 DECL_HANDLER(open_key)
1731 struct key *key, *parent;
1732 unsigned int access = req->access;
1734 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1735 reply->hkey = 0;
1736 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1738 WCHAR *name = copy_path( get_req_data(), get_req_data_size(), !req->parent );
1739 if (name && (key = open_key( parent, name )))
1741 reply->hkey = alloc_handle( current->process, key, access, 0 );
1742 release_object( key );
1744 release_object( parent );
1748 /* delete a registry key */
1749 DECL_HANDLER(delete_key)
1751 struct key *key;
1753 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1755 delete_key( key, 0);
1756 release_object( key );
1760 /* flush a registry key */
1761 DECL_HANDLER(flush_key)
1763 struct key *key = get_hkey_obj( req->hkey, 0 );
1764 if (key)
1766 /* we don't need to do anything here with the current implementation */
1767 release_object( key );
1771 /* enumerate registry subkeys */
1772 DECL_HANDLER(enum_key)
1774 struct key *key;
1776 if ((key = get_hkey_obj( req->hkey,
1777 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1779 enum_key( key, req->index, req->info_class, reply );
1780 release_object( key );
1784 /* set a value of a registry key */
1785 DECL_HANDLER(set_key_value)
1787 struct key *key;
1788 WCHAR *name;
1790 if (!(name = copy_req_path( req->namelen, 0 ))) return;
1791 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1793 size_t datalen = get_req_data_size() - req->namelen;
1794 const char *data = (char *)get_req_data() + req->namelen;
1796 set_value( key, name, req->type, data, datalen );
1797 release_object( key );
1801 /* retrieve the value of a registry key */
1802 DECL_HANDLER(get_key_value)
1804 struct key *key;
1805 WCHAR *name;
1807 reply->total = 0;
1808 if (!(name = copy_path( get_req_data(), get_req_data_size(), 0 ))) return;
1809 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1811 get_value( key, name, &reply->type, &reply->total );
1812 release_object( key );
1816 /* enumerate the value of a registry key */
1817 DECL_HANDLER(enum_key_value)
1819 struct key *key;
1821 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1823 enum_value( key, req->index, req->info_class, reply );
1824 release_object( key );
1828 /* delete a value of a registry key */
1829 DECL_HANDLER(delete_key_value)
1831 WCHAR *name;
1832 struct key *key;
1834 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1836 if ((name = req_strdupW( req, get_req_data(), get_req_data_size() )))
1838 delete_value( key, name );
1839 free( name );
1841 release_object( key );
1845 /* load a registry branch from a file */
1846 DECL_HANDLER(load_registry)
1848 struct key *key;
1850 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1852 /* FIXME: use subkey name */
1853 load_registry( key, req->file );
1854 release_object( key );
1858 DECL_HANDLER(unload_registry)
1860 struct key *key;
1862 if ((key = get_hkey_obj( req->hkey, 0 )))
1864 delete_key( key, 1 ); /* FIXME */
1865 release_object( key );
1869 /* save a registry branch to a file */
1870 DECL_HANDLER(save_registry)
1872 struct key *key;
1874 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1876 save_registry( key, req->file );
1877 release_object( key );
1881 /* load the user registry files */
1882 DECL_HANDLER(load_user_registries)
1884 struct key *key;
1886 current_level = 1;
1887 saving_level = req->saving;
1889 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1891 load_user_registries( key );
1892 release_object( key );
1895 /* set periodic save timer */
1897 if (save_timeout_user)
1899 remove_timeout_user( save_timeout_user );
1900 save_timeout_user = NULL;
1902 if ((save_period = req->period))
1904 if (save_period < 10000) save_period = 10000; /* limit rate */
1905 gettimeofday( &next_save_time, 0 );
1906 add_timeout( &next_save_time, save_period );
1907 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1911 /* add a registry key change notification */
1912 DECL_HANDLER(set_registry_notification)
1914 struct key *key;
1915 struct event *event;
1916 struct notify *notify;
1918 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
1919 if( key )
1921 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
1922 if( event )
1924 notify = find_notify( key, req->hkey );
1925 if( notify )
1927 release_object( notify->event );
1928 grab_object( event );
1929 notify->event = event;
1931 else
1933 notify = (struct notify *) malloc (sizeof(*notify));
1934 if( notify )
1936 grab_object( event );
1937 notify->event = event;
1938 notify->subtree = req->subtree;
1939 notify->filter = req->filter;
1940 notify->hkey = req->hkey;
1942 /* add to linked list */
1943 notify->prev = NULL;
1944 notify->next = key->first_notify;
1945 if ( notify->next )
1946 notify->next->prev = notify;
1947 else
1948 key->last_notify = notify;
1949 key->first_notify = notify;
1951 else
1952 set_error( STATUS_NO_MEMORY );
1954 release_object( event );
1956 release_object( key );