Release 20031118.
[wine/multimedia.git] / server / registry.c
blob5b2344d4c651bc5762f5d0b7d089fab5a3ba9e94
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 */
87 #define KEY_ROOT 0x0008 /* key is a root key */
89 /* a key value */
90 struct key_value
92 WCHAR *name; /* value name */
93 int type; /* value type */
94 size_t len; /* value data length in bytes */
95 void *data; /* pointer to value data */
98 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
99 #define MIN_VALUES 8 /* min. number of allocated values per key */
102 /* the special root keys */
103 #define HKEY_SPECIAL_ROOT_FIRST ((unsigned int)HKEY_CLASSES_ROOT)
104 #define HKEY_SPECIAL_ROOT_LAST ((unsigned int)HKEY_DYN_DATA)
105 #define NB_SPECIAL_ROOT_KEYS (HKEY_SPECIAL_ROOT_LAST - HKEY_SPECIAL_ROOT_FIRST + 1)
106 #define IS_SPECIAL_ROOT_HKEY(h) (((unsigned int)(h) >= HKEY_SPECIAL_ROOT_FIRST) && \
107 ((unsigned int)(h) <= HKEY_SPECIAL_ROOT_LAST))
109 static struct key *special_root_keys[NB_SPECIAL_ROOT_KEYS];
111 /* the real root key */
112 static struct key *root_key;
114 /* the special root key names */
115 static const char * const special_root_names[NB_SPECIAL_ROOT_KEYS] =
117 "Machine\\Software\\Classes", /* HKEY_CLASSES_ROOT */
118 "User\\", /* we append the user name dynamically */ /* HKEY_CURRENT_USER */
119 "Machine", /* HKEY_LOCAL_MACHINE */
120 "User", /* HKEY_USERS */
121 "PerfData", /* HKEY_PERFORMANCE_DATA */
122 "Machine\\System\\CurrentControlSet\\HardwareProfiles\\Current", /* HKEY_CURRENT_CONFIG */
123 "DynData" /* HKEY_DYN_DATA */
127 /* keys saving level */
128 /* current_level is the level that is put into all newly created or modified keys */
129 /* saving_level is the minimum level that a key needs in order to get saved */
130 static int current_level;
131 static int saving_level;
133 static struct timeval next_save_time; /* absolute time of next periodic save */
134 static int save_period; /* delay between periodic saves (ms) */
135 static struct timeout_user *save_timeout_user; /* saving timer */
137 /* information about where to save a registry branch */
138 struct save_branch_info
140 struct key *key;
141 char *path;
144 #define MAX_SAVE_BRANCH_INFO 8
145 static int save_branch_count;
146 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
149 /* information about a file being loaded */
150 struct file_load_info
152 FILE *file; /* input file */
153 char *buffer; /* line buffer */
154 int len; /* buffer length */
155 int line; /* current input line */
156 char *tmp; /* temp buffer to use while parsing input */
157 int tmplen; /* length of temp buffer */
161 static void key_dump( struct object *obj, int verbose );
162 static void key_destroy( struct object *obj );
164 static const struct object_ops key_ops =
166 sizeof(struct key), /* size */
167 key_dump, /* dump */
168 no_add_queue, /* add_queue */
169 NULL, /* remove_queue */
170 NULL, /* signaled */
171 NULL, /* satisfied */
172 no_get_fd, /* get_fd */
173 key_destroy /* destroy */
178 * The registry text file format v2 used by this code is similar to the one
179 * used by REGEDIT import/export functionality, with the following differences:
180 * - strings and key names can contain \x escapes for Unicode
181 * - key names use escapes too in order to support Unicode
182 * - the modification time optionally follows the key name
183 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
186 static inline char to_hex( char ch )
188 if (isdigit(ch)) return ch - '0';
189 return tolower(ch) - 'a' + 10;
192 /* dump the full path of a key */
193 static void dump_path( const struct key *key, const struct key *base, FILE *f )
195 if (key->parent && key->parent != base)
197 dump_path( key->parent, base, f );
198 fprintf( f, "\\\\" );
200 dump_strW( key->name, strlenW(key->name), f, "[]" );
203 /* dump a value to a text file */
204 static void dump_value( const struct key_value *value, FILE *f )
206 int i, count;
208 if (value->name[0])
210 fputc( '\"', f );
211 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
212 count += fprintf( f, "\"=" );
214 else count = fprintf( f, "@=" );
216 switch(value->type)
218 case REG_SZ:
219 case REG_EXPAND_SZ:
220 case REG_MULTI_SZ:
221 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
222 fputc( '\"', f );
223 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
224 fputc( '\"', f );
225 break;
226 case REG_DWORD:
227 if (value->len == sizeof(DWORD))
229 DWORD dw;
230 memcpy( &dw, value->data, sizeof(DWORD) );
231 fprintf( f, "dword:%08lx", dw );
232 break;
234 /* else fall through */
235 default:
236 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
237 else count += fprintf( f, "hex(%x):", value->type );
238 for (i = 0; i < value->len; i++)
240 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
241 if (i < value->len-1)
243 fputc( ',', f );
244 if (++count > 76)
246 fprintf( f, "\\\n " );
247 count = 2;
251 break;
253 fputc( '\n', f );
256 /* save a registry and all its subkeys to a text file */
257 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
259 int i;
261 if (key->flags & KEY_VOLATILE) return;
262 /* save key if it has the proper level, and has either some values or no subkeys */
263 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
264 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
266 fprintf( f, "\n[" );
267 if (key != base) dump_path( key, base, f );
268 fprintf( f, "] %ld\n", (long)key->modif );
269 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
271 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
274 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
276 fprintf( stderr, "%s key ", op );
277 if (key) dump_path( key, NULL, stderr );
278 else fprintf( stderr, "ERROR" );
279 if (value)
281 fprintf( stderr, " value ");
282 dump_value( value, stderr );
284 else fprintf( stderr, "\n" );
287 static void key_dump( struct object *obj, int verbose )
289 struct key *key = (struct key *)obj;
290 assert( obj->ops == &key_ops );
291 fprintf( stderr, "Key flags=%x ", key->flags );
292 dump_path( key, NULL, stderr );
293 fprintf( stderr, "\n" );
296 /* notify waiter and maybe delete the notification */
297 static void do_notification( struct key *key, struct notify *notify, int del )
299 if( notify->event )
301 set_event( notify->event );
302 release_object( notify->event );
303 notify->event = NULL;
306 if ( !del )
307 return;
308 if( notify->next )
309 notify->next->prev = notify->prev;
310 else
311 key->last_notify = notify->prev;
312 if( notify->prev )
313 notify->prev->next = notify->next;
314 else
315 key->first_notify = notify->next;
316 free( notify );
319 static struct notify *find_notify( struct key *key, obj_handle_t hkey)
321 struct notify *n;
323 for( n=key->first_notify; n; n = n->next)
324 if( n->hkey == hkey )
325 break;
326 return n;
329 /* close the notification associated with a handle */
330 void registry_close_handle( struct object *obj, obj_handle_t hkey )
332 struct key * key = (struct key *) obj;
333 struct notify *notify;
335 if( obj->ops != &key_ops )
336 return;
337 notify = find_notify( key, hkey );
338 if( !notify )
339 return;
340 do_notification( key, notify, 1 );
343 static void key_destroy( struct object *obj )
345 int i;
346 struct key *key = (struct key *)obj;
347 assert( obj->ops == &key_ops );
349 if (key->name) free( key->name );
350 if (key->class) free( key->class );
351 for (i = 0; i <= key->last_value; i++)
353 free( key->values[i].name );
354 if (key->values[i].data) free( key->values[i].data );
356 for (i = 0; i <= key->last_subkey; i++)
358 key->subkeys[i]->parent = NULL;
359 release_object( key->subkeys[i] );
361 /* unconditionally notify everything waiting on this key */
362 while ( key->first_notify )
363 do_notification( key, key->first_notify, 1 );
366 /* duplicate a key path */
367 /* returns a pointer to a static buffer, so only useable once per request */
368 static WCHAR *copy_path( const WCHAR *path, size_t len, int skip_root )
370 static WCHAR buffer[MAX_PATH+1];
371 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\',0 };
373 if (len > sizeof(buffer)-sizeof(buffer[0]))
375 set_error( STATUS_BUFFER_OVERFLOW );
376 return NULL;
378 memcpy( buffer, path, len );
379 buffer[len / sizeof(WCHAR)] = 0;
380 if (skip_root && !strncmpiW( buffer, root_name, 10 )) return buffer + 10;
381 return buffer;
384 /* copy a path from the request buffer */
385 static WCHAR *copy_req_path( size_t len, int skip_root )
387 const WCHAR *name_ptr = get_req_data();
388 if (len > get_req_data_size())
390 fatal_protocol_error( current, "copy_req_path: invalid length %d/%d\n",
391 len, get_req_data_size() );
392 return NULL;
394 return copy_path( name_ptr, len, skip_root );
397 /* return the next token in a given path */
398 /* returns a pointer to a static buffer, so only useable once per request */
399 static WCHAR *get_path_token( WCHAR *initpath )
401 static WCHAR *path;
402 WCHAR *ret;
404 if (initpath)
406 /* path cannot start with a backslash */
407 if (*initpath == '\\')
409 set_error( STATUS_OBJECT_PATH_INVALID );
410 return NULL;
412 path = initpath;
414 else while (*path == '\\') path++;
416 ret = path;
417 while (*path && *path != '\\') path++;
418 if (*path) *path++ = 0;
419 return ret;
422 /* duplicate a Unicode string from the request buffer */
423 static WCHAR *req_strdupW( const void *req, const WCHAR *str, size_t len )
425 WCHAR *name;
426 if ((name = mem_alloc( len + sizeof(WCHAR) )) != NULL)
428 memcpy( name, str, len );
429 name[len / sizeof(WCHAR)] = 0;
431 return name;
434 /* allocate a key object */
435 static struct key *alloc_key( const WCHAR *name, time_t modif )
437 struct key *key;
438 if ((key = alloc_object( &key_ops )))
440 key->class = NULL;
441 key->flags = 0;
442 key->last_subkey = -1;
443 key->nb_subkeys = 0;
444 key->subkeys = NULL;
445 key->nb_values = 0;
446 key->last_value = -1;
447 key->values = NULL;
448 key->level = current_level;
449 key->modif = modif;
450 key->parent = NULL;
451 key->first_notify = NULL;
452 key->last_notify = NULL;
453 if (!(key->name = strdupW( name )))
455 release_object( key );
456 key = NULL;
459 return key;
462 /* mark a key and all its parents as dirty (modified) */
463 static void make_dirty( struct key *key )
465 while (key)
467 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
468 key->flags |= KEY_DIRTY;
469 key = key->parent;
473 /* mark a key and all its subkeys as clean (not modified) */
474 static void make_clean( struct key *key )
476 int i;
478 if (key->flags & KEY_VOLATILE) return;
479 if (!(key->flags & KEY_DIRTY)) return;
480 key->flags &= ~KEY_DIRTY;
481 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
484 /* go through all the notifications and send them if necessary */
485 void check_notify( struct key *key, unsigned int change, int not_subtree )
487 struct notify *n = key->first_notify;
488 while (n)
490 struct notify *next = n->next;
491 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
492 do_notification( key, n, 0 );
493 n = next;
497 /* update key modification time */
498 static void touch_key( struct key *key, unsigned int change )
500 struct key *k;
502 key->modif = time(NULL);
503 key->level = max( key->level, current_level );
504 make_dirty( key );
506 /* do notifications */
507 check_notify( key, change, 1 );
508 for ( k = key->parent; k; k = k->parent )
509 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
512 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
513 static int grow_subkeys( struct key *key )
515 struct key **new_subkeys;
516 int nb_subkeys;
518 if (key->nb_subkeys)
520 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
521 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
523 set_error( STATUS_NO_MEMORY );
524 return 0;
527 else
529 nb_subkeys = MIN_VALUES;
530 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
532 key->subkeys = new_subkeys;
533 key->nb_subkeys = nb_subkeys;
534 return 1;
537 /* allocate a subkey for a given key, and return its index */
538 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
540 struct key *key;
541 int i;
543 if (parent->last_subkey + 1 == parent->nb_subkeys)
545 /* need to grow the array */
546 if (!grow_subkeys( parent )) return NULL;
548 if ((key = alloc_key( name, modif )) != NULL)
550 key->parent = parent;
551 for (i = ++parent->last_subkey; i > index; i--)
552 parent->subkeys[i] = parent->subkeys[i-1];
553 parent->subkeys[index] = key;
555 return key;
558 /* free a subkey of a given key */
559 static void free_subkey( struct key *parent, int index )
561 struct key *key;
562 int i, nb_subkeys;
564 assert( index >= 0 );
565 assert( index <= parent->last_subkey );
567 key = parent->subkeys[index];
568 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
569 parent->last_subkey--;
570 key->flags |= KEY_DELETED;
571 key->parent = NULL;
572 release_object( key );
574 /* try to shrink the array */
575 nb_subkeys = parent->nb_subkeys;
576 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
578 struct key **new_subkeys;
579 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
580 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
581 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
582 parent->subkeys = new_subkeys;
583 parent->nb_subkeys = nb_subkeys;
587 /* find the named child of a given key and return its index */
588 static struct key *find_subkey( const struct key *key, const WCHAR *name, int *index )
590 int i, min, max, res;
592 min = 0;
593 max = key->last_subkey;
594 while (min <= max)
596 i = (min + max) / 2;
597 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
599 *index = i;
600 return key->subkeys[i];
602 if (res > 0) max = i - 1;
603 else min = i + 1;
605 *index = min; /* this is where we should insert it */
606 return NULL;
609 /* open a subkey */
610 /* warning: the key name must be writeable (use copy_path) */
611 static struct key *open_key( struct key *key, WCHAR *name )
613 int index;
614 WCHAR *path;
616 if (!(path = get_path_token( name ))) return NULL;
617 while (*path)
619 if (!(key = find_subkey( key, path, &index )))
621 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
622 break;
624 path = get_path_token( NULL );
627 if (debug_level > 1) dump_operation( key, NULL, "Open" );
628 if (key) grab_object( key );
629 return key;
632 /* create a subkey */
633 /* warning: the key name must be writeable (use copy_path) */
634 static struct key *create_key( struct key *key, WCHAR *name, WCHAR *class,
635 int flags, time_t modif, int *created )
637 struct key *base;
638 int base_idx, index;
639 WCHAR *path;
641 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
643 set_error( STATUS_KEY_DELETED );
644 return NULL;
646 if (!(flags & KEY_VOLATILE) && (key->flags & KEY_VOLATILE))
648 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
649 return NULL;
651 if (!modif) modif = time(NULL);
653 if (!(path = get_path_token( name ))) return NULL;
654 *created = 0;
655 while (*path)
657 struct key *subkey;
658 if (!(subkey = find_subkey( key, path, &index ))) break;
659 key = subkey;
660 path = get_path_token( NULL );
663 /* create the remaining part */
665 if (!*path) goto done;
666 *created = 1;
667 if (flags & KEY_DIRTY) make_dirty( key );
668 base = key;
669 base_idx = index;
670 key = alloc_subkey( key, path, index, modif );
671 while (key)
673 key->flags |= flags;
674 path = get_path_token( NULL );
675 if (!*path) goto done;
676 /* we know the index is always 0 in a new key */
677 key = alloc_subkey( key, path, 0, modif );
679 if (base_idx != -1) free_subkey( base, base_idx );
680 return NULL;
682 done:
683 if (debug_level > 1) dump_operation( key, NULL, "Create" );
684 if (class) key->class = strdupW(class);
685 grab_object( key );
686 return key;
689 /* query information about a key or a subkey */
690 static void enum_key( const struct key *key, int index, int info_class,
691 struct enum_key_reply *reply )
693 int i;
694 size_t len, namelen, classlen;
695 int max_subkey = 0, max_class = 0;
696 int max_value = 0, max_data = 0;
697 WCHAR *data;
699 if (index != -1) /* -1 means use the specified key directly */
701 if ((index < 0) || (index > key->last_subkey))
703 set_error( STATUS_NO_MORE_ENTRIES );
704 return;
706 key = key->subkeys[index];
709 namelen = strlenW(key->name) * sizeof(WCHAR);
710 classlen = key->class ? strlenW(key->class) * sizeof(WCHAR) : 0;
712 switch(info_class)
714 case KeyBasicInformation:
715 classlen = 0; /* only return the name */
716 /* fall through */
717 case KeyNodeInformation:
718 reply->max_subkey = 0;
719 reply->max_class = 0;
720 reply->max_value = 0;
721 reply->max_data = 0;
722 break;
723 case KeyFullInformation:
724 for (i = 0; i <= key->last_subkey; i++)
726 struct key *subkey = key->subkeys[i];
727 len = strlenW( subkey->name );
728 if (len > max_subkey) max_subkey = len;
729 if (!subkey->class) continue;
730 len = strlenW( subkey->class );
731 if (len > max_class) max_class = len;
733 for (i = 0; i <= key->last_value; i++)
735 len = strlenW( key->values[i].name );
736 if (len > max_value) max_value = len;
737 len = key->values[i].len;
738 if (len > max_data) max_data = len;
740 reply->max_subkey = max_subkey;
741 reply->max_class = max_class;
742 reply->max_value = max_value;
743 reply->max_data = max_data;
744 namelen = 0; /* only return the class */
745 break;
746 default:
747 set_error( STATUS_INVALID_PARAMETER );
748 return;
750 reply->subkeys = key->last_subkey + 1;
751 reply->values = key->last_value + 1;
752 reply->modif = key->modif;
753 reply->total = namelen + classlen;
755 len = min( reply->total, get_reply_max_size() );
756 if (len && (data = set_reply_data_size( len )))
758 if (len > namelen)
760 reply->namelen = namelen;
761 memcpy( data, key->name, namelen );
762 memcpy( (char *)data + namelen, key->class, len - namelen );
764 else
766 reply->namelen = len;
767 memcpy( data, key->name, len );
770 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
773 /* delete a key and its values */
774 static int delete_key( struct key *key, int recurse )
776 int index;
777 struct key *parent;
779 /* must find parent and index */
780 if (key->flags & KEY_ROOT)
782 set_error( STATUS_ACCESS_DENIED );
783 return -1;
785 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
787 set_error( STATUS_KEY_DELETED );
788 return -1;
791 while (recurse && (key->last_subkey>=0))
792 if(0>delete_key(key->subkeys[key->last_subkey], 1))
793 return -1;
795 for (index = 0; index <= parent->last_subkey; index++)
796 if (parent->subkeys[index] == key) break;
797 assert( index <= parent->last_subkey );
799 /* we can only delete a key that has no subkeys (FIXME) */
800 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
802 set_error( STATUS_ACCESS_DENIED );
803 return -1;
806 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
807 free_subkey( parent, index );
808 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
809 return 0;
812 /* try to grow the array of values; return 1 if OK, 0 on error */
813 static int grow_values( struct key *key )
815 struct key_value *new_val;
816 int nb_values;
818 if (key->nb_values)
820 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
821 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
823 set_error( STATUS_NO_MEMORY );
824 return 0;
827 else
829 nb_values = MIN_VALUES;
830 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
832 key->values = new_val;
833 key->nb_values = nb_values;
834 return 1;
837 /* find the named value of a given key and return its index in the array */
838 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
840 int i, min, max, res;
842 min = 0;
843 max = key->last_value;
844 while (min <= max)
846 i = (min + max) / 2;
847 if (!(res = strcmpiW( key->values[i].name, name )))
849 *index = i;
850 return &key->values[i];
852 if (res > 0) max = i - 1;
853 else min = i + 1;
855 *index = min; /* this is where we should insert it */
856 return NULL;
859 /* insert a new value; the index must have been returned by find_value */
860 static struct key_value *insert_value( struct key *key, const WCHAR *name, int index )
862 struct key_value *value;
863 WCHAR *new_name;
864 int i;
866 if (key->last_value + 1 == key->nb_values)
868 if (!grow_values( key )) return NULL;
870 if (!(new_name = strdupW(name))) return NULL;
871 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
872 value = &key->values[index];
873 value->name = new_name;
874 value->len = 0;
875 value->data = NULL;
876 return value;
879 /* set a key value */
880 static void set_value( struct key *key, WCHAR *name, int type, const void *data, size_t len )
882 struct key_value *value;
883 void *ptr = NULL;
884 int index;
886 if ((value = find_value( key, name, &index )))
888 /* check if the new value is identical to the existing one */
889 if (value->type == type && value->len == len &&
890 value->data && !memcmp( value->data, data, len ))
892 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
893 return;
897 if (len && !(ptr = memdup( data, len ))) return;
899 if (!value)
901 if (!(value = insert_value( key, name, index )))
903 if (ptr) free( ptr );
904 return;
907 else if (value->data) free( value->data ); /* already existing, free previous data */
909 value->type = type;
910 value->len = len;
911 value->data = ptr;
912 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
913 if (debug_level > 1) dump_operation( key, value, "Set" );
916 /* get a key value */
917 static void get_value( struct key *key, const WCHAR *name, int *type, int *len )
919 struct key_value *value;
920 int index;
922 if ((value = find_value( key, name, &index )))
924 *type = value->type;
925 *len = value->len;
926 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
927 if (debug_level > 1) dump_operation( key, value, "Get" );
929 else
931 *type = -1;
932 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
936 /* enumerate a key value */
937 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
939 struct key_value *value;
941 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
942 else
944 void *data;
945 size_t namelen, maxlen;
947 value = &key->values[i];
948 reply->type = value->type;
949 namelen = strlenW( value->name ) * sizeof(WCHAR);
951 switch(info_class)
953 case KeyValueBasicInformation:
954 reply->total = namelen;
955 break;
956 case KeyValueFullInformation:
957 reply->total = namelen + value->len;
958 break;
959 case KeyValuePartialInformation:
960 reply->total = value->len;
961 namelen = 0;
962 break;
963 default:
964 set_error( STATUS_INVALID_PARAMETER );
965 return;
968 maxlen = min( reply->total, get_reply_max_size() );
969 if (maxlen && ((data = set_reply_data_size( maxlen ))))
971 if (maxlen > namelen)
973 reply->namelen = namelen;
974 memcpy( data, value->name, namelen );
975 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
977 else
979 reply->namelen = maxlen;
980 memcpy( data, value->name, maxlen );
983 if (debug_level > 1) dump_operation( key, value, "Enum" );
987 /* delete a value */
988 static void delete_value( struct key *key, const WCHAR *name )
990 struct key_value *value;
991 int i, index, nb_values;
993 if (!(value = find_value( key, name, &index )))
995 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
996 return;
998 if (debug_level > 1) dump_operation( key, value, "Delete" );
999 free( value->name );
1000 if (value->data) free( value->data );
1001 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1002 key->last_value--;
1003 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1005 /* try to shrink the array */
1006 nb_values = key->nb_values;
1007 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1009 struct key_value *new_val;
1010 nb_values -= nb_values / 3; /* shrink by 33% */
1011 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1012 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1013 key->values = new_val;
1014 key->nb_values = nb_values;
1018 static struct key *create_root_key( obj_handle_t hkey )
1020 WCHAR keyname[80];
1021 int i, dummy;
1022 struct key *key;
1023 const char *p;
1025 p = special_root_names[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST];
1026 i = 0;
1027 while (*p) keyname[i++] = *p++;
1029 if (hkey == (obj_handle_t)HKEY_CURRENT_USER) /* this one is special */
1031 /* get the current user name */
1032 p = wine_get_user_name();
1033 while (*p && i < sizeof(keyname)/sizeof(WCHAR)-1) keyname[i++] = *p++;
1035 keyname[i++] = 0;
1037 if ((key = create_key( root_key, keyname, NULL, 0, time(NULL), &dummy )))
1039 special_root_keys[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST] = key;
1040 key->flags |= KEY_ROOT;
1042 return key;
1045 /* get the registry key corresponding to an hkey handle */
1046 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1048 struct key *key;
1050 if (!hkey) return (struct key *)grab_object( root_key );
1051 if (IS_SPECIAL_ROOT_HKEY(hkey))
1053 if (!(key = special_root_keys[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST]))
1054 key = create_root_key( hkey );
1055 else
1056 grab_object( key );
1058 else
1059 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1060 return key;
1063 /* read a line from the input file */
1064 static int read_next_line( struct file_load_info *info )
1066 char *newbuf;
1067 int newlen, pos = 0;
1069 info->line++;
1070 for (;;)
1072 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1073 return (pos != 0); /* EOF */
1074 pos = strlen(info->buffer);
1075 if (info->buffer[pos-1] == '\n')
1077 /* got a full line */
1078 info->buffer[--pos] = 0;
1079 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1080 return 1;
1082 if (pos < info->len - 1) return 1; /* EOF but something was read */
1084 /* need to enlarge the buffer */
1085 newlen = info->len + info->len / 2;
1086 if (!(newbuf = realloc( info->buffer, newlen )))
1088 set_error( STATUS_NO_MEMORY );
1089 return -1;
1091 info->buffer = newbuf;
1092 info->len = newlen;
1096 /* make sure the temp buffer holds enough space */
1097 static int get_file_tmp_space( struct file_load_info *info, int size )
1099 char *tmp;
1100 if (info->tmplen >= size) return 1;
1101 if (!(tmp = realloc( info->tmp, size )))
1103 set_error( STATUS_NO_MEMORY );
1104 return 0;
1106 info->tmp = tmp;
1107 info->tmplen = size;
1108 return 1;
1111 /* report an error while loading an input file */
1112 static void file_read_error( const char *err, struct file_load_info *info )
1114 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
1117 /* parse an escaped string back into Unicode */
1118 /* return the number of chars read from the input, or -1 on output overflow */
1119 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
1121 int count = sizeof(WCHAR); /* for terminating null */
1122 const char *p = src;
1123 while (*p && *p != endchar)
1125 if (*p != '\\') *dest = (WCHAR)*p++;
1126 else
1128 p++;
1129 switch(*p)
1131 case 'a': *dest = '\a'; p++; break;
1132 case 'b': *dest = '\b'; p++; break;
1133 case 'e': *dest = '\e'; p++; break;
1134 case 'f': *dest = '\f'; p++; break;
1135 case 'n': *dest = '\n'; p++; break;
1136 case 'r': *dest = '\r'; p++; break;
1137 case 't': *dest = '\t'; p++; break;
1138 case 'v': *dest = '\v'; p++; break;
1139 case 'x': /* hex escape */
1140 p++;
1141 if (!isxdigit(*p)) *dest = 'x';
1142 else
1144 *dest = to_hex(*p++);
1145 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1146 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1147 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1149 break;
1150 case '0':
1151 case '1':
1152 case '2':
1153 case '3':
1154 case '4':
1155 case '5':
1156 case '6':
1157 case '7': /* octal escape */
1158 *dest = *p++ - '0';
1159 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1160 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1161 break;
1162 default:
1163 *dest = (WCHAR)*p++;
1164 break;
1167 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1168 dest++;
1170 *dest = 0;
1171 if (!*p) return -1; /* delimiter not found */
1172 *len = count;
1173 return p + 1 - src;
1176 /* convert a data type tag to a value type */
1177 static int get_data_type( const char *buffer, int *type, int *parse_type )
1179 struct data_type { const char *tag; int len; int type; int parse_type; };
1181 static const struct data_type data_types[] =
1182 { /* actual type */ /* type to assume for parsing */
1183 { "\"", 1, REG_SZ, REG_SZ },
1184 { "str:\"", 5, REG_SZ, REG_SZ },
1185 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1186 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1187 { "hex:", 4, REG_BINARY, REG_BINARY },
1188 { "dword:", 6, REG_DWORD, REG_DWORD },
1189 { "hex(", 4, -1, REG_BINARY },
1190 { NULL, 0, 0, 0 }
1193 const struct data_type *ptr;
1194 char *end;
1196 for (ptr = data_types; ptr->tag; ptr++)
1198 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1199 *parse_type = ptr->parse_type;
1200 if ((*type = ptr->type) != -1) return ptr->len;
1201 /* "hex(xx):" is special */
1202 *type = (int)strtoul( buffer + 4, &end, 16 );
1203 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1204 return end + 2 - buffer;
1206 return 0;
1209 /* load and create a key from the input file */
1210 static struct key *load_key( struct key *base, const char *buffer, int flags,
1211 int prefix_len, struct file_load_info *info,
1212 int default_modif )
1214 WCHAR *p, *name;
1215 int res, len, modif;
1217 len = strlen(buffer) * sizeof(WCHAR);
1218 if (!get_file_tmp_space( info, len )) return NULL;
1220 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1222 file_read_error( "Malformed key", info );
1223 return NULL;
1225 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = default_modif;
1227 p = (WCHAR *)info->tmp;
1228 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1230 if (!*p)
1232 if (prefix_len > 1)
1234 file_read_error( "Malformed key", info );
1235 return NULL;
1237 /* empty key name, return base key */
1238 return (struct key *)grab_object( base );
1240 if (!(name = copy_path( p, len - ((char *)p - info->tmp), 0 )))
1242 file_read_error( "Key is too long", info );
1243 return NULL;
1245 return create_key( base, name, NULL, flags, modif, &res );
1248 /* parse a comma-separated list of hex digits */
1249 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1251 const char *p = buffer;
1252 int count = 0;
1253 while (isxdigit(*p))
1255 int val;
1256 char buf[3];
1257 memcpy( buf, p, 2 );
1258 buf[2] = 0;
1259 sscanf( buf, "%x", &val );
1260 if (count++ >= *len) return -1; /* dest buffer overflow */
1261 *dest++ = (unsigned char )val;
1262 p += 2;
1263 if (*p == ',') p++;
1265 *len = count;
1266 return p - buffer;
1269 /* parse a value name and create the corresponding value */
1270 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1271 struct file_load_info *info )
1273 struct key_value *value;
1274 int index, maxlen;
1276 maxlen = strlen(buffer) * sizeof(WCHAR);
1277 if (!get_file_tmp_space( info, maxlen )) return NULL;
1278 if (buffer[0] == '@')
1280 info->tmp[0] = info->tmp[1] = 0;
1281 *len = 1;
1283 else
1285 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1286 (*len)++; /* for initial quote */
1288 while (isspace(buffer[*len])) (*len)++;
1289 if (buffer[*len] != '=') goto error;
1290 (*len)++;
1291 while (isspace(buffer[*len])) (*len)++;
1292 if (!(value = find_value( key, (WCHAR *)info->tmp, &index )))
1293 value = insert_value( key, (WCHAR *)info->tmp, index );
1294 return value;
1296 error:
1297 file_read_error( "Malformed value name", info );
1298 return NULL;
1301 /* load a value from the input file */
1302 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1304 DWORD dw;
1305 void *ptr, *newptr;
1306 int maxlen, len, res;
1307 int type, parse_type;
1308 struct key_value *value;
1310 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1311 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1312 buffer += len + res;
1314 switch(parse_type)
1316 case REG_SZ:
1317 len = strlen(buffer) * sizeof(WCHAR);
1318 if (!get_file_tmp_space( info, len )) return 0;
1319 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1320 ptr = info->tmp;
1321 break;
1322 case REG_DWORD:
1323 dw = strtoul( buffer, NULL, 16 );
1324 ptr = &dw;
1325 len = sizeof(dw);
1326 break;
1327 case REG_BINARY: /* hex digits */
1328 len = 0;
1329 for (;;)
1331 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1332 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1333 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1334 len += maxlen;
1335 buffer += res;
1336 while (isspace(*buffer)) buffer++;
1337 if (!*buffer) break;
1338 if (*buffer != '\\') goto error;
1339 if (read_next_line( info) != 1) goto error;
1340 buffer = info->buffer;
1341 while (isspace(*buffer)) buffer++;
1343 ptr = info->tmp;
1344 break;
1345 default:
1346 assert(0);
1347 ptr = NULL; /* keep compiler quiet */
1348 break;
1351 if (!len) newptr = NULL;
1352 else if (!(newptr = memdup( ptr, len ))) return 0;
1354 if (value->data) free( value->data );
1355 value->data = newptr;
1356 value->len = len;
1357 value->type = type;
1358 /* update the key level but not the modification time */
1359 key->level = max( key->level, current_level );
1360 make_dirty( key );
1361 return 1;
1363 error:
1364 file_read_error( "Malformed value", info );
1365 return 0;
1368 /* return the length (in path elements) of name that is part of the key name */
1369 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1370 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1372 WCHAR *p;
1373 int res;
1374 int len = strlen(name) * sizeof(WCHAR);
1375 if (!get_file_tmp_space( info, len )) return 0;
1377 if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
1379 file_read_error( "Malformed key", info );
1380 return 0;
1382 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
1383 *p = 0;
1384 for (res = 1; key != root_key; res++)
1386 if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
1387 key = key->parent;
1389 if (key == root_key) res = 0; /* no matching name */
1390 return res;
1393 /* load all the keys from the input file */
1394 static void load_keys( struct key *key, FILE *f )
1396 struct key *subkey = NULL;
1397 struct file_load_info info;
1398 char *p;
1399 int default_modif = time(NULL);
1400 int flags = (key->flags & KEY_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1401 int prefix_len = -1; /* number of key name prefixes to skip */
1403 info.file = f;
1404 info.len = 4;
1405 info.tmplen = 4;
1406 info.line = 0;
1407 if (!(info.buffer = mem_alloc( info.len ))) return;
1408 if (!(info.tmp = mem_alloc( info.tmplen )))
1410 free( info.buffer );
1411 return;
1414 if ((read_next_line( &info ) != 1) ||
1415 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1417 set_error( STATUS_NOT_REGISTRY_FILE );
1418 goto done;
1421 while (read_next_line( &info ) == 1)
1423 p = info.buffer;
1424 while (*p && isspace(*p)) p++;
1425 switch(*p)
1427 case '[': /* new key */
1428 if (subkey) release_object( subkey );
1429 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1430 if (!(subkey = load_key( key, p + 1, flags, prefix_len, &info, default_modif )))
1431 file_read_error( "Error creating key", &info );
1432 break;
1433 case '@': /* default value */
1434 case '\"': /* value */
1435 if (subkey) load_value( subkey, p, &info );
1436 else file_read_error( "Value without key", &info );
1437 break;
1438 case '#': /* comment */
1439 case ';': /* comment */
1440 case 0: /* empty line */
1441 break;
1442 default:
1443 file_read_error( "Unrecognized input", &info );
1444 break;
1448 done:
1449 if (subkey) release_object( subkey );
1450 free( info.buffer );
1451 free( info.tmp );
1454 /* load a part of the registry from a file */
1455 static void load_registry( struct key *key, obj_handle_t handle )
1457 struct file *file;
1458 int fd;
1460 if (!(file = get_file_obj( current->process, handle, GENERIC_READ ))) return;
1461 fd = dup( get_file_unix_fd( file ) );
1462 release_object( file );
1463 if (fd != -1)
1465 FILE *f = fdopen( fd, "r" );
1466 if (f)
1468 load_keys( key, f );
1469 fclose( f );
1471 else file_set_error();
1475 /* registry initialisation */
1476 void init_registry(void)
1478 static const WCHAR root_name[] = { 0 };
1479 static const WCHAR config_name[] =
1480 { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
1481 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 };
1483 char *filename;
1484 const char *config;
1485 FILE *f;
1487 /* create the root key */
1488 root_key = alloc_key( root_name, time(NULL) );
1489 assert( root_key );
1490 root_key->flags |= KEY_ROOT;
1492 /* load the config file */
1493 config = wine_get_config_dir();
1494 if (!(filename = malloc( strlen(config) + 8 ))) fatal_error( "out of memory\n" );
1495 strcpy( filename, config );
1496 strcat( filename, "/config" );
1497 if ((f = fopen( filename, "r" )))
1499 struct key *key;
1500 int dummy;
1502 /* create the config key */
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 key\n" );
1506 key->flags |= KEY_VOLATILE;
1508 load_keys( key, f );
1509 fclose( f );
1510 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1511 fatal_error( "%s is not a valid registry file\n", filename );
1512 if (get_error())
1513 fatal_error( "loading %s failed with error %x\n", filename, get_error() );
1515 release_object( key );
1517 free( filename );
1520 /* update the level of the parents of a key (only needed for the old format) */
1521 static int update_level( struct key *key )
1523 int i;
1524 int max = key->level;
1525 for (i = 0; i <= key->last_subkey; i++)
1527 int sub = update_level( key->subkeys[i] );
1528 if (sub > max) max = sub;
1530 key->level = max;
1531 return max;
1534 /* save a registry branch to a file */
1535 static void save_all_subkeys( struct key *key, FILE *f )
1537 fprintf( f, "WINE REGISTRY Version 2\n" );
1538 fprintf( f, ";; All keys relative to " );
1539 dump_path( key, NULL, f );
1540 fprintf( f, "\n" );
1541 save_subkeys( key, key, f );
1544 /* save a registry branch to a file handle */
1545 static void save_registry( struct key *key, obj_handle_t handle )
1547 struct file *file;
1548 int fd;
1550 if (key->flags & KEY_DELETED)
1552 set_error( STATUS_KEY_DELETED );
1553 return;
1555 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE ))) return;
1556 fd = dup( get_file_unix_fd( file ) );
1557 release_object( file );
1558 if (fd != -1)
1560 FILE *f = fdopen( fd, "w" );
1561 if (f)
1563 save_all_subkeys( key, f );
1564 if (fclose( f )) file_set_error();
1566 else
1568 file_set_error();
1569 close( fd );
1574 /* register a key branch for being saved on exit */
1575 static void register_branch_for_saving( struct key *key, const char *path, size_t len )
1577 if (save_branch_count >= MAX_SAVE_BRANCH_INFO)
1579 set_error( STATUS_NO_MORE_ENTRIES );
1580 return;
1582 if (!len || !(save_branch_info[save_branch_count].path = memdup( path, len ))) return;
1583 save_branch_info[save_branch_count].path[len - 1] = 0;
1584 save_branch_info[save_branch_count].key = (struct key *)grab_object( key );
1585 save_branch_count++;
1588 /* save a registry branch to a file */
1589 static int save_branch( struct key *key, const char *path )
1591 struct stat st;
1592 char *p, *real, *tmp = NULL;
1593 int fd, count = 0, ret = 0, by_symlink;
1594 FILE *f;
1596 if (!(key->flags & KEY_DIRTY))
1598 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1599 return 1;
1602 /* get the real path */
1604 by_symlink = (!lstat(path, &st) && S_ISLNK (st.st_mode));
1605 if (!(real = malloc( PATH_MAX ))) return 0;
1606 if (!realpath( path, real ))
1608 free( real );
1609 real = NULL;
1611 else path = real;
1613 /* test the file type */
1615 if ((fd = open( path, O_WRONLY )) != -1)
1617 /* if file is not a regular file or has multiple links or is accessed
1618 * via symbolic links, write directly into it; otherwise use a temp file */
1619 if (by_symlink ||
1620 (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)))
1622 ftruncate( fd, 0 );
1623 goto save;
1625 close( fd );
1628 /* create a temp file in the same directory */
1630 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1631 strcpy( tmp, path );
1632 if ((p = strrchr( tmp, '/' ))) p++;
1633 else p = tmp;
1634 for (;;)
1636 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1637 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1638 if (errno != EEXIST) goto done;
1639 close( fd );
1642 /* now save to it */
1644 save:
1645 if (!(f = fdopen( fd, "w" )))
1647 if (tmp) unlink( tmp );
1648 close( fd );
1649 goto done;
1652 if (debug_level > 1)
1654 fprintf( stderr, "%s: ", path );
1655 dump_operation( key, NULL, "saving" );
1658 save_all_subkeys( key, f );
1659 ret = !fclose(f);
1661 if (tmp)
1663 /* if successfully written, rename to final name */
1664 if (ret) ret = !rename( tmp, path );
1665 if (!ret) unlink( tmp );
1666 free( tmp );
1669 done:
1670 if (real) free( real );
1671 if (ret) make_clean( key );
1672 return ret;
1675 /* periodic saving of the registry */
1676 static void periodic_save( void *arg )
1678 int i;
1679 for (i = 0; i < save_branch_count; i++)
1680 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1681 add_timeout( &next_save_time, save_period );
1682 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1685 /* save the modified registry branches to disk */
1686 void flush_registry(void)
1688 int i;
1690 for (i = 0; i < save_branch_count; i++)
1692 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1694 fprintf( stderr, "wineserver: could not save registry branch to %s",
1695 save_branch_info[i].path );
1696 perror( " " );
1701 /* close the top-level keys; used on server exit */
1702 void close_registry(void)
1704 int i;
1706 for (i = 0; i < save_branch_count; i++) release_object( save_branch_info[i].key );
1707 release_object( root_key );
1711 /* create a registry key */
1712 DECL_HANDLER(create_key)
1714 struct key *key = NULL, *parent;
1715 unsigned int access = req->access;
1716 WCHAR *name, *class;
1718 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1719 reply->hkey = 0;
1720 if (!(name = copy_req_path( req->namelen, !req->parent ))) return;
1721 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1723 int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1725 if (req->namelen == get_req_data_size()) /* no class specified */
1727 key = create_key( parent, name, NULL, flags, req->modif, &reply->created );
1729 else
1731 const WCHAR *class_ptr = (WCHAR *)((char *)get_req_data() + req->namelen);
1733 if ((class = req_strdupW( req, class_ptr, get_req_data_size() - req->namelen )))
1735 key = create_key( parent, name, class, flags, req->modif, &reply->created );
1736 free( class );
1739 if (key)
1741 reply->hkey = alloc_handle( current->process, key, access, 0 );
1742 release_object( key );
1744 release_object( parent );
1748 /* open a registry key */
1749 DECL_HANDLER(open_key)
1751 struct key *key, *parent;
1752 unsigned int access = req->access;
1754 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1755 reply->hkey = 0;
1756 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1758 WCHAR *name = copy_path( get_req_data(), get_req_data_size(), !req->parent );
1759 if (name && (key = open_key( parent, name )))
1761 reply->hkey = alloc_handle( current->process, key, access, 0 );
1762 release_object( key );
1764 release_object( parent );
1768 /* delete a registry key */
1769 DECL_HANDLER(delete_key)
1771 struct key *key;
1773 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1775 delete_key( key, 0);
1776 release_object( key );
1780 /* enumerate registry subkeys */
1781 DECL_HANDLER(enum_key)
1783 struct key *key;
1785 if ((key = get_hkey_obj( req->hkey,
1786 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1788 enum_key( key, req->index, req->info_class, reply );
1789 release_object( key );
1793 /* set a value of a registry key */
1794 DECL_HANDLER(set_key_value)
1796 struct key *key;
1797 WCHAR *name;
1799 if (!(name = copy_req_path( req->namelen, 0 ))) return;
1800 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1802 size_t datalen = get_req_data_size() - req->namelen;
1803 const char *data = (char *)get_req_data() + req->namelen;
1805 set_value( key, name, req->type, data, datalen );
1806 release_object( key );
1810 /* retrieve the value of a registry key */
1811 DECL_HANDLER(get_key_value)
1813 struct key *key;
1814 WCHAR *name;
1816 reply->total = 0;
1817 if (!(name = copy_path( get_req_data(), get_req_data_size(), 0 ))) return;
1818 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1820 get_value( key, name, &reply->type, &reply->total );
1821 release_object( key );
1825 /* enumerate the value of a registry key */
1826 DECL_HANDLER(enum_key_value)
1828 struct key *key;
1830 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1832 enum_value( key, req->index, req->info_class, reply );
1833 release_object( key );
1837 /* delete a value of a registry key */
1838 DECL_HANDLER(delete_key_value)
1840 WCHAR *name;
1841 struct key *key;
1843 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1845 if ((name = req_strdupW( req, get_req_data(), get_req_data_size() )))
1847 delete_value( key, name );
1848 free( name );
1850 release_object( key );
1854 /* load a registry branch from a file */
1855 DECL_HANDLER(load_registry)
1857 struct key *key;
1859 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1861 /* FIXME: use subkey name */
1862 load_registry( key, req->file );
1863 release_object( key );
1867 DECL_HANDLER(unload_registry)
1869 struct key *key;
1871 if ((key = get_hkey_obj( req->hkey, 0 )))
1873 delete_key( key, 1 ); /* FIXME */
1874 release_object( key );
1878 /* save a registry branch to a file */
1879 DECL_HANDLER(save_registry)
1881 struct key *key;
1883 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1885 save_registry( key, req->file );
1886 release_object( key );
1890 /* set the current and saving level for the registry */
1891 DECL_HANDLER(set_registry_levels)
1893 current_level = req->current;
1894 saving_level = req->saving;
1896 /* set periodic save timer */
1898 if (save_timeout_user)
1900 remove_timeout_user( save_timeout_user );
1901 save_timeout_user = NULL;
1903 if ((save_period = req->period))
1905 if (save_period < 10000) save_period = 10000; /* limit rate */
1906 gettimeofday( &next_save_time, 0 );
1907 add_timeout( &next_save_time, save_period );
1908 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1912 /* save a registry branch at server exit */
1913 DECL_HANDLER(save_registry_atexit)
1915 struct key *key;
1917 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1919 register_branch_for_saving( key, get_req_data(), get_req_data_size() );
1920 release_object( key );
1924 /* add a registry key change notification */
1925 DECL_HANDLER(set_registry_notification)
1927 struct key *key;
1928 struct event *event;
1929 struct notify *notify;
1931 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
1932 if( key )
1934 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
1935 if( event )
1937 notify = find_notify( key, req->hkey );
1938 if( notify )
1940 release_object( notify->event );
1941 grab_object( event );
1942 notify->event = event;
1944 else
1946 notify = (struct notify *) malloc (sizeof(*notify));
1947 if( notify )
1949 grab_object( event );
1950 notify->event = event;
1951 notify->subtree = req->subtree;
1952 notify->filter = req->filter;
1953 notify->hkey = req->hkey;
1955 /* add to linked list */
1956 notify->prev = NULL;
1957 notify->next = key->first_notify;
1958 if ( notify->next )
1959 notify->next->prev = notify;
1960 else
1961 key->last_notify = notify;
1962 key->first_notify = notify;
1964 else
1965 set_error( STATUS_NO_MEMORY );
1967 release_object( event );
1969 release_object( key );