Initial wineboot implementation; at this stage only wininit.ini
[wine/multimedia.git] / server / registry.c
blob31dfe97c4cfa457e93728732f979955b07699e22
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 <string.h>
37 #include <stdlib.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include "object.h"
41 #include "handle.h"
42 #include "request.h"
43 #include "unicode.h"
45 #include "winbase.h"
46 #include "winreg.h"
47 #include "winternl.h"
48 #include "wine/library.h"
50 struct notify
52 struct event *event; /* event to set when changing this key */
53 int subtree; /* true if subtree notification */
54 unsigned int filter; /* which events to notify on */
55 obj_handle_t hkey; /* hkey associated with this notification */
56 struct notify *next; /* list of notifications */
57 struct notify *prev; /* list of notifications */
60 /* a registry key */
61 struct key
63 struct object obj; /* object header */
64 WCHAR *name; /* key name */
65 WCHAR *class; /* key class */
66 struct key *parent; /* parent key */
67 int last_subkey; /* last in use subkey */
68 int nb_subkeys; /* count of allocated subkeys */
69 struct key **subkeys; /* subkeys array */
70 int last_value; /* last in use value */
71 int nb_values; /* count of allocated values in array */
72 struct key_value *values; /* values array */
73 short flags; /* flags */
74 short level; /* saving level */
75 time_t modif; /* last modification time */
76 struct notify *first_notify; /* list of notifications */
77 struct notify *last_notify; /* list of notifications */
80 /* key flags */
81 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
82 #define KEY_DELETED 0x0002 /* key has been deleted */
83 #define KEY_DIRTY 0x0004 /* key has been modified */
84 #define KEY_ROOT 0x0008 /* key is a root key */
86 /* a key value */
87 struct key_value
89 WCHAR *name; /* value name */
90 int type; /* value type */
91 size_t len; /* value data length in bytes */
92 void *data; /* pointer to value data */
95 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
96 #define MIN_VALUES 8 /* min. number of allocated values per key */
99 /* the special root keys */
100 #define HKEY_SPECIAL_ROOT_FIRST ((unsigned int)HKEY_CLASSES_ROOT)
101 #define HKEY_SPECIAL_ROOT_LAST ((unsigned int)HKEY_DYN_DATA)
102 #define NB_SPECIAL_ROOT_KEYS (HKEY_SPECIAL_ROOT_LAST - HKEY_SPECIAL_ROOT_FIRST + 1)
103 #define IS_SPECIAL_ROOT_HKEY(h) (((unsigned int)(h) >= HKEY_SPECIAL_ROOT_FIRST) && \
104 ((unsigned int)(h) <= HKEY_SPECIAL_ROOT_LAST))
106 static struct key *special_root_keys[NB_SPECIAL_ROOT_KEYS];
108 /* the real root key */
109 static struct key *root_key;
111 /* the special root key names */
112 static const char * const special_root_names[NB_SPECIAL_ROOT_KEYS] =
114 "Machine\\Software\\Classes", /* HKEY_CLASSES_ROOT */
115 "User\\", /* we append the user name dynamically */ /* HKEY_CURRENT_USER */
116 "Machine", /* HKEY_LOCAL_MACHINE */
117 "User", /* HKEY_USERS */
118 "PerfData", /* HKEY_PERFORMANCE_DATA */
119 "Machine\\System\\CurrentControlSet\\HardwareProfiles\\Current", /* HKEY_CURRENT_CONFIG */
120 "DynData" /* HKEY_DYN_DATA */
124 /* keys saving level */
125 /* current_level is the level that is put into all newly created or modified keys */
126 /* saving_level is the minimum level that a key needs in order to get saved */
127 static int current_level;
128 static int saving_level;
130 static struct timeval next_save_time; /* absolute time of next periodic save */
131 static int save_period; /* delay between periodic saves (ms) */
132 static struct timeout_user *save_timeout_user; /* saving timer */
134 /* information about where to save a registry branch */
135 struct save_branch_info
137 struct key *key;
138 char *path;
141 #define MAX_SAVE_BRANCH_INFO 8
142 static int save_branch_count;
143 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
146 /* information about a file being loaded */
147 struct file_load_info
149 FILE *file; /* input file */
150 char *buffer; /* line buffer */
151 int len; /* buffer length */
152 int line; /* current input line */
153 char *tmp; /* temp buffer to use while parsing input */
154 int tmplen; /* length of temp buffer */
158 static void key_dump( struct object *obj, int verbose );
159 static void key_destroy( struct object *obj );
161 static const struct object_ops key_ops =
163 sizeof(struct key), /* size */
164 key_dump, /* dump */
165 no_add_queue, /* add_queue */
166 NULL, /* remove_queue */
167 NULL, /* signaled */
168 NULL, /* satisfied */
169 NULL, /* get_poll_events */
170 NULL, /* poll_event */
171 no_get_fd, /* get_fd */
172 no_flush, /* flush */
173 no_get_file_info, /* get_file_info */
174 NULL, /* queue_async */
175 key_destroy /* destroy */
180 * The registry text file format v2 used by this code is similar to the one
181 * used by REGEDIT import/export functionality, with the following differences:
182 * - strings and key names can contain \x escapes for Unicode
183 * - key names use escapes too in order to support Unicode
184 * - the modification time optionally follows the key name
185 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
188 static inline char to_hex( char ch )
190 if (isdigit(ch)) return ch - '0';
191 return tolower(ch) - 'a' + 10;
194 /* dump the full path of a key */
195 static void dump_path( const struct key *key, const struct key *base, FILE *f )
197 if (key->parent && key->parent != base)
199 dump_path( key->parent, base, f );
200 fprintf( f, "\\\\" );
202 dump_strW( key->name, strlenW(key->name), f, "[]" );
205 /* dump a value to a text file */
206 static void dump_value( const struct key_value *value, FILE *f )
208 int i, count;
210 if (value->name[0])
212 fputc( '\"', f );
213 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
214 count += fprintf( f, "\"=" );
216 else count = fprintf( f, "@=" );
218 switch(value->type)
220 case REG_SZ:
221 case REG_EXPAND_SZ:
222 case REG_MULTI_SZ:
223 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
224 fputc( '\"', f );
225 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
226 fputc( '\"', f );
227 break;
228 case REG_DWORD:
229 if (value->len == sizeof(DWORD))
231 DWORD dw;
232 memcpy( &dw, value->data, sizeof(DWORD) );
233 fprintf( f, "dword:%08lx", dw );
234 break;
236 /* else fall through */
237 default:
238 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
239 else count += fprintf( f, "hex(%x):", value->type );
240 for (i = 0; i < value->len; i++)
242 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
243 if (i < value->len-1)
245 fputc( ',', f );
246 if (++count > 76)
248 fprintf( f, "\\\n " );
249 count = 2;
253 break;
255 fputc( '\n', f );
258 /* save a registry and all its subkeys to a text file */
259 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
261 int i;
263 if (key->flags & KEY_VOLATILE) return;
264 /* save key if it has the proper level, and has either some values or no subkeys */
265 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
266 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
268 fprintf( f, "\n[" );
269 if (key != base) dump_path( key, base, f );
270 fprintf( f, "] %ld\n", key->modif );
271 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
273 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
276 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
278 fprintf( stderr, "%s key ", op );
279 if (key) dump_path( key, NULL, stderr );
280 else fprintf( stderr, "ERROR" );
281 if (value)
283 fprintf( stderr, " value ");
284 dump_value( value, stderr );
286 else fprintf( stderr, "\n" );
289 static void key_dump( struct object *obj, int verbose )
291 struct key *key = (struct key *)obj;
292 assert( obj->ops == &key_ops );
293 fprintf( stderr, "Key flags=%x ", key->flags );
294 dump_path( key, NULL, stderr );
295 fprintf( stderr, "\n" );
298 /* notify waiter and maybe delete the notification */
299 static void do_notification( struct key *key, struct notify *notify, int del )
301 if( notify->event )
303 set_event( notify->event );
304 release_object( notify->event );
305 notify->event = NULL;
308 if ( !del )
309 return;
310 if( notify->next )
311 notify->next->prev = notify->prev;
312 else
313 key->last_notify = notify->prev;
314 if( notify->prev )
315 notify->prev->next = notify->next;
316 else
317 key->first_notify = notify->next;
318 free( notify );
321 static struct notify *find_notify( struct key *key, obj_handle_t hkey)
323 struct notify *n;
325 for( n=key->first_notify; n; n = n->next)
326 if( n->hkey == hkey )
327 break;
328 return n;
331 /* close the notification associated with a handle */
332 void registry_close_handle( struct object *obj, obj_handle_t hkey )
334 struct key * key = (struct key *) obj;
335 struct notify *notify;
337 if( obj->ops != &key_ops )
338 return;
339 notify = find_notify( key, hkey );
340 if( !notify )
341 return;
342 do_notification( key, notify, 1 );
345 static void key_destroy( struct object *obj )
347 int i;
348 struct key *key = (struct key *)obj;
349 assert( obj->ops == &key_ops );
351 if (key->name) free( key->name );
352 if (key->class) free( key->class );
353 for (i = 0; i <= key->last_value; i++)
355 free( key->values[i].name );
356 if (key->values[i].data) free( key->values[i].data );
358 for (i = 0; i <= key->last_subkey; i++)
360 key->subkeys[i]->parent = NULL;
361 release_object( key->subkeys[i] );
363 /* unconditionally notify everything waiting on this key */
364 while ( key->first_notify )
365 do_notification( key, key->first_notify, 1 );
368 /* duplicate a key path */
369 /* returns a pointer to a static buffer, so only useable once per request */
370 static WCHAR *copy_path( const WCHAR *path, size_t len, int skip_root )
372 static WCHAR buffer[MAX_PATH+1];
373 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\',0 };
375 if (len > sizeof(buffer)-sizeof(buffer[0]))
377 set_error( STATUS_BUFFER_OVERFLOW );
378 return NULL;
380 memcpy( buffer, path, len );
381 buffer[len / sizeof(WCHAR)] = 0;
382 if (skip_root && !strncmpiW( buffer, root_name, 10 )) return buffer + 10;
383 return buffer;
386 /* copy a path from the request buffer */
387 static WCHAR *copy_req_path( size_t len, int skip_root )
389 const WCHAR *name_ptr = get_req_data();
390 if (len > get_req_data_size())
392 fatal_protocol_error( current, "copy_req_path: invalid length %d/%d\n",
393 len, get_req_data_size() );
394 return NULL;
396 return copy_path( name_ptr, len, skip_root );
399 /* return the next token in a given path */
400 /* returns a pointer to a static buffer, so only useable once per request */
401 static WCHAR *get_path_token( WCHAR *initpath )
403 static WCHAR *path;
404 WCHAR *ret;
406 if (initpath)
408 /* path cannot start with a backslash */
409 if (*initpath == '\\')
411 set_error( STATUS_OBJECT_PATH_INVALID );
412 return NULL;
414 path = initpath;
416 else while (*path == '\\') path++;
418 ret = path;
419 while (*path && *path != '\\') path++;
420 if (*path) *path++ = 0;
421 return ret;
424 /* duplicate a Unicode string from the request buffer */
425 static WCHAR *req_strdupW( const void *req, const WCHAR *str, size_t len )
427 WCHAR *name;
428 if ((name = mem_alloc( len + sizeof(WCHAR) )) != NULL)
430 memcpy( name, str, len );
431 name[len / sizeof(WCHAR)] = 0;
433 return name;
436 /* allocate a key object */
437 static struct key *alloc_key( const WCHAR *name, time_t modif )
439 struct key *key;
440 if ((key = (struct key *)alloc_object( &key_ops, -1 )))
442 key->class = NULL;
443 key->flags = 0;
444 key->last_subkey = -1;
445 key->nb_subkeys = 0;
446 key->subkeys = NULL;
447 key->nb_values = 0;
448 key->last_value = -1;
449 key->values = NULL;
450 key->level = current_level;
451 key->modif = modif;
452 key->parent = NULL;
453 key->first_notify = NULL;
454 key->last_notify = NULL;
455 if (!(key->name = strdupW( name )))
457 release_object( key );
458 key = NULL;
461 return key;
464 /* mark a key and all its parents as dirty (modified) */
465 static void make_dirty( struct key *key )
467 while (key)
469 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
470 key->flags |= KEY_DIRTY;
471 key = key->parent;
475 /* mark a key and all its subkeys as clean (not modified) */
476 static void make_clean( struct key *key )
478 int i;
480 if (key->flags & KEY_VOLATILE) return;
481 if (!(key->flags & KEY_DIRTY)) return;
482 key->flags &= ~KEY_DIRTY;
483 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
486 /* go through all the notifications and send them if necessary */
487 void check_notify( struct key *key, unsigned int change, int not_subtree )
489 struct notify *n = key->first_notify;
490 while (n)
492 struct notify *next = n->next;
493 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
494 do_notification( key, n, 0 );
495 n = next;
499 /* update key modification time */
500 static void touch_key( struct key *key, unsigned int change )
502 struct key *k;
504 key->modif = time(NULL);
505 key->level = max( key->level, current_level );
506 make_dirty( key );
508 /* do notifications */
509 check_notify( key, change, 1 );
510 for ( k = key->parent; k; k = k->parent )
511 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
514 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
515 static int grow_subkeys( struct key *key )
517 struct key **new_subkeys;
518 int nb_subkeys;
520 if (key->nb_subkeys)
522 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
523 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
525 set_error( STATUS_NO_MEMORY );
526 return 0;
529 else
531 nb_subkeys = MIN_VALUES;
532 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
534 key->subkeys = new_subkeys;
535 key->nb_subkeys = nb_subkeys;
536 return 1;
539 /* allocate a subkey for a given key, and return its index */
540 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
542 struct key *key;
543 int i;
545 if (parent->last_subkey + 1 == parent->nb_subkeys)
547 /* need to grow the array */
548 if (!grow_subkeys( parent )) return NULL;
550 if ((key = alloc_key( name, modif )) != NULL)
552 key->parent = parent;
553 for (i = ++parent->last_subkey; i > index; i--)
554 parent->subkeys[i] = parent->subkeys[i-1];
555 parent->subkeys[index] = key;
557 return key;
560 /* free a subkey of a given key */
561 static void free_subkey( struct key *parent, int index )
563 struct key *key;
564 int i, nb_subkeys;
566 assert( index >= 0 );
567 assert( index <= parent->last_subkey );
569 key = parent->subkeys[index];
570 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
571 parent->last_subkey--;
572 key->flags |= KEY_DELETED;
573 key->parent = NULL;
574 release_object( key );
576 /* try to shrink the array */
577 nb_subkeys = parent->nb_subkeys;
578 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
580 struct key **new_subkeys;
581 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
582 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
583 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
584 parent->subkeys = new_subkeys;
585 parent->nb_subkeys = nb_subkeys;
589 /* find the named child of a given key and return its index */
590 static struct key *find_subkey( const struct key *key, const WCHAR *name, int *index )
592 int i, min, max, res;
594 min = 0;
595 max = key->last_subkey;
596 while (min <= max)
598 i = (min + max) / 2;
599 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
601 *index = i;
602 return key->subkeys[i];
604 if (res > 0) max = i - 1;
605 else min = i + 1;
607 *index = min; /* this is where we should insert it */
608 return NULL;
611 /* open a subkey */
612 /* warning: the key name must be writeable (use copy_path) */
613 static struct key *open_key( struct key *key, WCHAR *name )
615 int index;
616 WCHAR *path;
618 if (!(path = get_path_token( name ))) return NULL;
619 while (*path)
621 if (!(key = find_subkey( key, path, &index )))
623 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
624 break;
626 path = get_path_token( NULL );
629 if (debug_level > 1) dump_operation( key, NULL, "Open" );
630 if (key) grab_object( key );
631 return key;
634 /* create a subkey */
635 /* warning: the key name must be writeable (use copy_path) */
636 static struct key *create_key( struct key *key, WCHAR *name, WCHAR *class,
637 int flags, time_t modif, int *created )
639 struct key *base;
640 int base_idx, index;
641 WCHAR *path;
643 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
645 set_error( STATUS_KEY_DELETED );
646 return NULL;
648 if (!(flags & KEY_VOLATILE) && (key->flags & KEY_VOLATILE))
650 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
651 return NULL;
653 if (!modif) modif = time(NULL);
655 if (!(path = get_path_token( name ))) return NULL;
656 *created = 0;
657 while (*path)
659 struct key *subkey;
660 if (!(subkey = find_subkey( key, path, &index ))) break;
661 key = subkey;
662 path = get_path_token( NULL );
665 /* create the remaining part */
667 if (!*path) goto done;
668 *created = 1;
669 touch_key( key, REG_NOTIFY_CHANGE_NAME ); /* FIXME: is this right? */
670 if (flags & KEY_DIRTY) make_dirty( key );
671 base = key;
672 base_idx = index;
673 key = alloc_subkey( key, path, index, modif );
674 while (key)
676 key->flags |= flags;
677 path = get_path_token( NULL );
678 if (!*path) goto done;
679 /* we know the index is always 0 in a new key */
680 key = alloc_subkey( key, path, 0, modif );
682 if (base_idx != -1) free_subkey( base, base_idx );
683 return NULL;
685 done:
686 if (debug_level > 1) dump_operation( key, NULL, "Create" );
687 if (class) key->class = strdupW(class);
688 grab_object( key );
689 return key;
692 /* query information about a key or a subkey */
693 static void enum_key( const struct key *key, int index, int info_class,
694 struct enum_key_reply *reply )
696 int i;
697 size_t len, namelen, classlen;
698 int max_subkey = 0, max_class = 0;
699 int max_value = 0, max_data = 0;
700 WCHAR *data;
702 if (index != -1) /* -1 means use the specified key directly */
704 if ((index < 0) || (index > key->last_subkey))
706 set_error( STATUS_NO_MORE_ENTRIES );
707 return;
709 key = key->subkeys[index];
712 namelen = strlenW(key->name) * sizeof(WCHAR);
713 classlen = key->class ? strlenW(key->class) * sizeof(WCHAR) : 0;
715 switch(info_class)
717 case KeyBasicInformation:
718 classlen = 0; /* only return the name */
719 /* fall through */
720 case KeyNodeInformation:
721 reply->max_subkey = 0;
722 reply->max_class = 0;
723 reply->max_value = 0;
724 reply->max_data = 0;
725 break;
726 case KeyFullInformation:
727 for (i = 0; i <= key->last_subkey; i++)
729 struct key *subkey = key->subkeys[i];
730 len = strlenW( subkey->name );
731 if (len > max_subkey) max_subkey = len;
732 if (!subkey->class) continue;
733 len = strlenW( subkey->class );
734 if (len > max_class) max_class = len;
736 for (i = 0; i <= key->last_value; i++)
738 len = strlenW( key->values[i].name );
739 if (len > max_value) max_value = len;
740 len = key->values[i].len;
741 if (len > max_data) max_data = len;
743 reply->max_subkey = max_subkey;
744 reply->max_class = max_class;
745 reply->max_value = max_value;
746 reply->max_data = max_data;
747 namelen = 0; /* only return the class */
748 break;
749 default:
750 set_error( STATUS_INVALID_PARAMETER );
751 return;
753 reply->subkeys = key->last_subkey + 1;
754 reply->values = key->last_value + 1;
755 reply->modif = key->modif;
756 reply->total = namelen + classlen;
758 len = min( reply->total, get_reply_max_size() );
759 if (len && (data = set_reply_data_size( len )))
761 if (len > namelen)
763 reply->namelen = namelen;
764 memcpy( data, key->name, namelen );
765 memcpy( (char *)data + namelen, key->class, len - namelen );
767 else
769 reply->namelen = len;
770 memcpy( data, key->name, len );
773 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
776 /* delete a key and its values */
777 static void delete_key( struct key *key )
779 int index;
780 struct key *parent;
782 /* must find parent and index */
783 if (key->flags & KEY_ROOT)
785 set_error( STATUS_ACCESS_DENIED );
786 return;
788 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
790 set_error( STATUS_KEY_DELETED );
791 return;
793 for (index = 0; index <= parent->last_subkey; index++)
794 if (parent->subkeys[index] == key) break;
795 assert( index <= parent->last_subkey );
797 /* we can only delete a key that has no subkeys (FIXME) */
798 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
800 set_error( STATUS_ACCESS_DENIED );
801 return;
803 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
804 free_subkey( parent, index );
805 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
808 /* try to grow the array of values; return 1 if OK, 0 on error */
809 static int grow_values( struct key *key )
811 struct key_value *new_val;
812 int nb_values;
814 if (key->nb_values)
816 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
817 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
819 set_error( STATUS_NO_MEMORY );
820 return 0;
823 else
825 nb_values = MIN_VALUES;
826 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
828 key->values = new_val;
829 key->nb_values = nb_values;
830 return 1;
833 /* find the named value of a given key and return its index in the array */
834 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
836 int i, min, max, res;
838 min = 0;
839 max = key->last_value;
840 while (min <= max)
842 i = (min + max) / 2;
843 if (!(res = strcmpiW( key->values[i].name, name )))
845 *index = i;
846 return &key->values[i];
848 if (res > 0) max = i - 1;
849 else min = i + 1;
851 *index = min; /* this is where we should insert it */
852 return NULL;
855 /* insert a new value; the index must have been returned by find_value */
856 static struct key_value *insert_value( struct key *key, const WCHAR *name, int index )
858 struct key_value *value;
859 WCHAR *new_name;
860 int i;
862 if (key->last_value + 1 == key->nb_values)
864 if (!grow_values( key )) return NULL;
866 if (!(new_name = strdupW(name))) return NULL;
867 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
868 value = &key->values[index];
869 value->name = new_name;
870 value->len = 0;
871 value->data = NULL;
872 return value;
875 /* set a key value */
876 static void set_value( struct key *key, WCHAR *name, int type, const void *data, size_t len )
878 struct key_value *value;
879 void *ptr = NULL;
880 int index;
882 if ((value = find_value( key, name, &index )))
884 /* check if the new value is identical to the existing one */
885 if (value->type == type && value->len == len &&
886 value->data && !memcmp( value->data, data, len ))
888 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
889 return;
893 if (len && !(ptr = memdup( data, len ))) return;
895 if (!value)
897 if (!(value = insert_value( key, name, index )))
899 if (ptr) free( ptr );
900 return;
903 else if (value->data) free( value->data ); /* already existing, free previous data */
905 value->type = type;
906 value->len = len;
907 value->data = ptr;
908 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
909 if (debug_level > 1) dump_operation( key, value, "Set" );
912 /* get a key value */
913 static void get_value( struct key *key, const WCHAR *name, int *type, int *len )
915 struct key_value *value;
916 int index;
918 if ((value = find_value( key, name, &index )))
920 *type = value->type;
921 *len = value->len;
922 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
923 if (debug_level > 1) dump_operation( key, value, "Get" );
925 else
927 *type = -1;
928 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
932 /* enumerate a key value */
933 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
935 struct key_value *value;
937 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
938 else
940 void *data;
941 size_t namelen, maxlen;
943 value = &key->values[i];
944 reply->type = value->type;
945 namelen = strlenW( value->name ) * sizeof(WCHAR);
947 switch(info_class)
949 case KeyValueBasicInformation:
950 reply->total = namelen;
951 break;
952 case KeyValueFullInformation:
953 reply->total = namelen + value->len;
954 break;
955 case KeyValuePartialInformation:
956 reply->total = value->len;
957 namelen = 0;
958 break;
959 default:
960 set_error( STATUS_INVALID_PARAMETER );
961 return;
964 maxlen = min( reply->total, get_reply_max_size() );
965 if (maxlen && ((data = set_reply_data_size( maxlen ))))
967 if (maxlen > namelen)
969 reply->namelen = namelen;
970 memcpy( data, value->name, namelen );
971 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
973 else
975 reply->namelen = maxlen;
976 memcpy( data, value->name, maxlen );
979 if (debug_level > 1) dump_operation( key, value, "Enum" );
983 /* delete a value */
984 static void delete_value( struct key *key, const WCHAR *name )
986 struct key_value *value;
987 int i, index, nb_values;
989 if (!(value = find_value( key, name, &index )))
991 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
992 return;
994 if (debug_level > 1) dump_operation( key, value, "Delete" );
995 free( value->name );
996 if (value->data) free( value->data );
997 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
998 key->last_value--;
999 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1001 /* try to shrink the array */
1002 nb_values = key->nb_values;
1003 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1005 struct key_value *new_val;
1006 nb_values -= nb_values / 3; /* shrink by 33% */
1007 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1008 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1009 key->values = new_val;
1010 key->nb_values = nb_values;
1014 static struct key *create_root_key( obj_handle_t hkey )
1016 WCHAR keyname[80];
1017 int i, dummy;
1018 struct key *key;
1019 const char *p;
1021 p = special_root_names[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST];
1022 i = 0;
1023 while (*p) keyname[i++] = *p++;
1025 if (hkey == (obj_handle_t)HKEY_CURRENT_USER) /* this one is special */
1027 /* get the current user name */
1028 p = wine_get_user_name();
1029 while (*p && i < sizeof(keyname)/sizeof(WCHAR)-1) keyname[i++] = *p++;
1031 keyname[i++] = 0;
1033 if ((key = create_key( root_key, keyname, NULL, 0, time(NULL), &dummy )))
1035 special_root_keys[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST] = key;
1036 key->flags |= KEY_ROOT;
1038 return key;
1041 /* get the registry key corresponding to an hkey handle */
1042 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1044 struct key *key;
1046 if (!hkey) return (struct key *)grab_object( root_key );
1047 if (IS_SPECIAL_ROOT_HKEY(hkey))
1049 if (!(key = special_root_keys[(unsigned int)hkey - HKEY_SPECIAL_ROOT_FIRST]))
1050 key = create_root_key( hkey );
1051 else
1052 grab_object( key );
1054 else
1055 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1056 return key;
1059 /* read a line from the input file */
1060 static int read_next_line( struct file_load_info *info )
1062 char *newbuf;
1063 int newlen, pos = 0;
1065 info->line++;
1066 for (;;)
1068 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1069 return (pos != 0); /* EOF */
1070 pos = strlen(info->buffer);
1071 if (info->buffer[pos-1] == '\n')
1073 /* got a full line */
1074 info->buffer[--pos] = 0;
1075 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1076 return 1;
1078 if (pos < info->len - 1) return 1; /* EOF but something was read */
1080 /* need to enlarge the buffer */
1081 newlen = info->len + info->len / 2;
1082 if (!(newbuf = realloc( info->buffer, newlen )))
1084 set_error( STATUS_NO_MEMORY );
1085 return -1;
1087 info->buffer = newbuf;
1088 info->len = newlen;
1092 /* make sure the temp buffer holds enough space */
1093 static int get_file_tmp_space( struct file_load_info *info, int size )
1095 char *tmp;
1096 if (info->tmplen >= size) return 1;
1097 if (!(tmp = realloc( info->tmp, size )))
1099 set_error( STATUS_NO_MEMORY );
1100 return 0;
1102 info->tmp = tmp;
1103 info->tmplen = size;
1104 return 1;
1107 /* report an error while loading an input file */
1108 static void file_read_error( const char *err, struct file_load_info *info )
1110 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
1113 /* parse an escaped string back into Unicode */
1114 /* return the number of chars read from the input, or -1 on output overflow */
1115 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
1117 int count = sizeof(WCHAR); /* for terminating null */
1118 const char *p = src;
1119 while (*p && *p != endchar)
1121 if (*p != '\\') *dest = (WCHAR)*p++;
1122 else
1124 p++;
1125 switch(*p)
1127 case 'a': *dest = '\a'; p++; break;
1128 case 'b': *dest = '\b'; p++; break;
1129 case 'e': *dest = '\e'; p++; break;
1130 case 'f': *dest = '\f'; p++; break;
1131 case 'n': *dest = '\n'; p++; break;
1132 case 'r': *dest = '\r'; p++; break;
1133 case 't': *dest = '\t'; p++; break;
1134 case 'v': *dest = '\v'; p++; break;
1135 case 'x': /* hex escape */
1136 p++;
1137 if (!isxdigit(*p)) *dest = 'x';
1138 else
1140 *dest = to_hex(*p++);
1141 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1142 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1143 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1145 break;
1146 case '0':
1147 case '1':
1148 case '2':
1149 case '3':
1150 case '4':
1151 case '5':
1152 case '6':
1153 case '7': /* octal escape */
1154 *dest = *p++ - '0';
1155 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1156 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1157 break;
1158 default:
1159 *dest = (WCHAR)*p++;
1160 break;
1163 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1164 dest++;
1166 *dest = 0;
1167 if (!*p) return -1; /* delimiter not found */
1168 *len = count;
1169 return p + 1 - src;
1172 /* convert a data type tag to a value type */
1173 static int get_data_type( const char *buffer, int *type, int *parse_type )
1175 struct data_type { const char *tag; int len; int type; int parse_type; };
1177 static const struct data_type data_types[] =
1178 { /* actual type */ /* type to assume for parsing */
1179 { "\"", 1, REG_SZ, REG_SZ },
1180 { "str:\"", 5, REG_SZ, REG_SZ },
1181 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1182 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1183 { "hex:", 4, REG_BINARY, REG_BINARY },
1184 { "dword:", 6, REG_DWORD, REG_DWORD },
1185 { "hex(", 4, -1, REG_BINARY },
1186 { NULL, 0, 0, 0 }
1189 const struct data_type *ptr;
1190 char *end;
1192 for (ptr = data_types; ptr->tag; ptr++)
1194 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1195 *parse_type = ptr->parse_type;
1196 if ((*type = ptr->type) != -1) return ptr->len;
1197 /* "hex(xx):" is special */
1198 *type = (int)strtoul( buffer + 4, &end, 16 );
1199 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1200 return end + 2 - buffer;
1202 return 0;
1205 /* load and create a key from the input file */
1206 static struct key *load_key( struct key *base, const char *buffer, int flags,
1207 int prefix_len, struct file_load_info *info )
1209 WCHAR *p, *name;
1210 int res, len, modif;
1212 len = strlen(buffer) * sizeof(WCHAR);
1213 if (!get_file_tmp_space( info, len )) return NULL;
1215 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1217 file_read_error( "Malformed key", info );
1218 return NULL;
1220 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = time(NULL);
1222 p = (WCHAR *)info->tmp;
1223 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1225 if (!*p)
1227 if (prefix_len > 1)
1229 file_read_error( "Malformed key", info );
1230 return NULL;
1232 /* empty key name, return base key */
1233 return (struct key *)grab_object( base );
1235 if (!(name = copy_path( p, len - ((char *)p - info->tmp), 0 )))
1237 file_read_error( "Key is too long", info );
1238 return NULL;
1240 return create_key( base, name, NULL, flags, modif, &res );
1243 /* parse a comma-separated list of hex digits */
1244 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1246 const char *p = buffer;
1247 int count = 0;
1248 while (isxdigit(*p))
1250 int val;
1251 char buf[3];
1252 memcpy( buf, p, 2 );
1253 buf[2] = 0;
1254 sscanf( buf, "%x", &val );
1255 if (count++ >= *len) return -1; /* dest buffer overflow */
1256 *dest++ = (unsigned char )val;
1257 p += 2;
1258 if (*p == ',') p++;
1260 *len = count;
1261 return p - buffer;
1264 /* parse a value name and create the corresponding value */
1265 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1266 struct file_load_info *info )
1268 struct key_value *value;
1269 int index, maxlen;
1271 maxlen = strlen(buffer) * sizeof(WCHAR);
1272 if (!get_file_tmp_space( info, maxlen )) return NULL;
1273 if (buffer[0] == '@')
1275 info->tmp[0] = info->tmp[1] = 0;
1276 *len = 1;
1278 else
1280 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1281 (*len)++; /* for initial quote */
1283 while (isspace(buffer[*len])) (*len)++;
1284 if (buffer[*len] != '=') goto error;
1285 (*len)++;
1286 while (isspace(buffer[*len])) (*len)++;
1287 if (!(value = find_value( key, (WCHAR *)info->tmp, &index )))
1288 value = insert_value( key, (WCHAR *)info->tmp, index );
1289 return value;
1291 error:
1292 file_read_error( "Malformed value name", info );
1293 return NULL;
1296 /* load a value from the input file */
1297 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1299 DWORD dw;
1300 void *ptr, *newptr;
1301 int maxlen, len, res;
1302 int type, parse_type;
1303 struct key_value *value;
1305 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1306 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1307 buffer += len + res;
1309 switch(parse_type)
1311 case REG_SZ:
1312 len = strlen(buffer) * sizeof(WCHAR);
1313 if (!get_file_tmp_space( info, len )) return 0;
1314 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1315 ptr = info->tmp;
1316 break;
1317 case REG_DWORD:
1318 dw = strtoul( buffer, NULL, 16 );
1319 ptr = &dw;
1320 len = sizeof(dw);
1321 break;
1322 case REG_BINARY: /* hex digits */
1323 len = 0;
1324 for (;;)
1326 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1327 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1328 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1329 len += maxlen;
1330 buffer += res;
1331 while (isspace(*buffer)) buffer++;
1332 if (!*buffer) break;
1333 if (*buffer != '\\') goto error;
1334 if (read_next_line( info) != 1) goto error;
1335 buffer = info->buffer;
1336 while (isspace(*buffer)) buffer++;
1338 ptr = info->tmp;
1339 break;
1340 default:
1341 assert(0);
1342 ptr = NULL; /* keep compiler quiet */
1343 break;
1346 if (!len) newptr = NULL;
1347 else if (!(newptr = memdup( ptr, len ))) return 0;
1349 if (value->data) free( value->data );
1350 value->data = newptr;
1351 value->len = len;
1352 value->type = type;
1353 /* update the key level but not the modification time */
1354 key->level = max( key->level, current_level );
1355 make_dirty( key );
1356 return 1;
1358 error:
1359 file_read_error( "Malformed value", info );
1360 return 0;
1363 /* return the length (in path elements) of name that is part of the key name */
1364 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1365 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1367 WCHAR *p;
1368 int res;
1369 int len = strlen(name) * sizeof(WCHAR);
1370 if (!get_file_tmp_space( info, len )) return 0;
1372 if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
1374 file_read_error( "Malformed key", info );
1375 return 0;
1377 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
1378 *p = 0;
1379 for (res = 1; key != root_key; res++)
1381 if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
1382 key = key->parent;
1384 if (key == root_key) res = 0; /* no matching name */
1385 return res;
1388 /* load all the keys from the input file */
1389 static void load_keys( struct key *key, FILE *f )
1391 struct key *subkey = NULL;
1392 struct file_load_info info;
1393 char *p;
1394 int flags = (key->flags & KEY_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1395 int prefix_len = -1; /* number of key name prefixes to skip */
1397 info.file = f;
1398 info.len = 4;
1399 info.tmplen = 4;
1400 info.line = 0;
1401 if (!(info.buffer = mem_alloc( info.len ))) return;
1402 if (!(info.tmp = mem_alloc( info.tmplen )))
1404 free( info.buffer );
1405 return;
1408 if ((read_next_line( &info ) != 1) ||
1409 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1411 set_error( STATUS_NOT_REGISTRY_FILE );
1412 goto done;
1415 while (read_next_line( &info ) == 1)
1417 p = info.buffer;
1418 while (*p && isspace(*p)) p++;
1419 switch(*p)
1421 case '[': /* new key */
1422 if (subkey) release_object( subkey );
1423 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1424 if (!(subkey = load_key( key, p + 1, flags, prefix_len, &info )))
1425 file_read_error( "Error creating key", &info );
1426 break;
1427 case '@': /* default value */
1428 case '\"': /* value */
1429 if (subkey) load_value( subkey, p, &info );
1430 else file_read_error( "Value without key", &info );
1431 break;
1432 case '#': /* comment */
1433 case ';': /* comment */
1434 case 0: /* empty line */
1435 break;
1436 default:
1437 file_read_error( "Unrecognized input", &info );
1438 break;
1442 done:
1443 if (subkey) release_object( subkey );
1444 free( info.buffer );
1445 free( info.tmp );
1448 /* load a part of the registry from a file */
1449 static void load_registry( struct key *key, obj_handle_t handle )
1451 struct object *obj;
1452 int fd;
1454 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return;
1455 fd = dup(obj->ops->get_fd( obj ));
1456 release_object( obj );
1457 if (fd != -1)
1459 FILE *f = fdopen( fd, "r" );
1460 if (f)
1462 load_keys( key, f );
1463 fclose( f );
1465 else file_set_error();
1469 /* registry initialisation */
1470 void init_registry(void)
1472 static const WCHAR root_name[] = { 0 };
1473 static const WCHAR config_name[] =
1474 { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
1475 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 };
1477 char *filename;
1478 const char *config;
1479 FILE *f;
1481 /* create the root key */
1482 root_key = alloc_key( root_name, time(NULL) );
1483 assert( root_key );
1484 root_key->flags |= KEY_ROOT;
1486 /* load the config file */
1487 config = wine_get_config_dir();
1488 if (!(filename = malloc( strlen(config) + 8 ))) fatal_error( "out of memory\n" );
1489 strcpy( filename, config );
1490 strcat( filename, "/config" );
1491 if ((f = fopen( filename, "r" )))
1493 struct key *key;
1494 int dummy;
1496 /* create the config key */
1497 if (!(key = create_key( root_key, copy_path( config_name, sizeof(config_name), 0 ),
1498 NULL, 0, time(NULL), &dummy )))
1499 fatal_error( "could not create config key\n" );
1500 key->flags |= KEY_VOLATILE;
1502 load_keys( key, f );
1503 fclose( f );
1504 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1505 fatal_error( "%s is not a valid registry file\n", filename );
1506 if (get_error())
1507 fatal_error( "loading %s failed with error %x\n", filename, get_error() );
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 object *obj;
1542 int fd;
1544 if (key->flags & KEY_DELETED)
1546 set_error( STATUS_KEY_DELETED );
1547 return;
1549 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return;
1550 fd = dup(obj->ops->get_fd( obj ));
1551 release_object( obj );
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 /* register a key branch for being saved on exit */
1569 static void register_branch_for_saving( struct key *key, const char *path, size_t len )
1571 if (save_branch_count >= MAX_SAVE_BRANCH_INFO)
1573 set_error( STATUS_NO_MORE_ENTRIES );
1574 return;
1576 if (!len || !(save_branch_info[save_branch_count].path = memdup( path, len ))) return;
1577 save_branch_info[save_branch_count].path[len - 1] = 0;
1578 save_branch_info[save_branch_count].key = (struct key *)grab_object( key );
1579 save_branch_count++;
1582 /* save a registry branch to a file */
1583 static int save_branch( struct key *key, const char *path )
1585 struct stat st;
1586 char *p, *real, *tmp = NULL;
1587 int fd, count = 0, ret = 0, by_symlink;
1588 FILE *f;
1590 if (!(key->flags & KEY_DIRTY))
1592 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1593 return 1;
1596 /* get the real path */
1598 by_symlink = (!lstat(path, &st) && S_ISLNK (st.st_mode));
1599 if (!(real = malloc( PATH_MAX ))) return 0;
1600 if (!realpath( path, real ))
1602 free( real );
1603 real = NULL;
1605 else path = real;
1607 /* test the file type */
1609 if ((fd = open( path, O_WRONLY )) != -1)
1611 /* if file is not a regular file or has multiple links or is accessed
1612 * via symbolic links, write directly into it; otherwise use a temp file */
1613 if (by_symlink ||
1614 (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)))
1616 ftruncate( fd, 0 );
1617 goto save;
1619 close( fd );
1622 /* create a temp file in the same directory */
1624 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1625 strcpy( tmp, path );
1626 if ((p = strrchr( tmp, '/' ))) p++;
1627 else p = tmp;
1628 for (;;)
1630 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1631 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1632 if (errno != EEXIST) goto done;
1633 close( fd );
1636 /* now save to it */
1638 save:
1639 if (!(f = fdopen( fd, "w" )))
1641 if (tmp) unlink( tmp );
1642 close( fd );
1643 goto done;
1646 if (debug_level > 1)
1648 fprintf( stderr, "%s: ", path );
1649 dump_operation( key, NULL, "saving" );
1652 save_all_subkeys( key, f );
1653 ret = !fclose(f);
1655 if (tmp)
1657 /* if successfully written, rename to final name */
1658 if (ret) ret = !rename( tmp, path );
1659 if (!ret) unlink( tmp );
1660 free( tmp );
1663 done:
1664 if (real) free( real );
1665 if (ret) make_clean( key );
1666 return ret;
1669 /* periodic saving of the registry */
1670 static void periodic_save( void *arg )
1672 int i;
1673 for (i = 0; i < save_branch_count; i++)
1674 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1675 add_timeout( &next_save_time, save_period );
1676 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1679 /* save the modified registry branches to disk */
1680 void flush_registry(void)
1682 int i;
1684 for (i = 0; i < save_branch_count; i++)
1686 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1688 fprintf( stderr, "wineserver: could not save registry branch to %s",
1689 save_branch_info[i].path );
1690 perror( " " );
1695 /* close the top-level keys; used on server exit */
1696 void close_registry(void)
1698 int i;
1700 for (i = 0; i < save_branch_count; i++) release_object( save_branch_info[i].key );
1701 release_object( root_key );
1705 /* create a registry key */
1706 DECL_HANDLER(create_key)
1708 struct key *key = NULL, *parent;
1709 unsigned int access = req->access;
1710 WCHAR *name, *class;
1712 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1713 reply->hkey = 0;
1714 if (!(name = copy_req_path( req->namelen, !req->parent ))) return;
1715 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1717 int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1719 if (req->namelen == get_req_data_size()) /* no class specified */
1721 key = create_key( parent, name, NULL, flags, req->modif, &reply->created );
1723 else
1725 const WCHAR *class_ptr = (WCHAR *)((char *)get_req_data() + req->namelen);
1727 if ((class = req_strdupW( req, class_ptr, get_req_data_size() - req->namelen )))
1729 key = create_key( parent, name, class, flags, req->modif, &reply->created );
1730 free( class );
1733 if (key)
1735 reply->hkey = alloc_handle( current->process, key, access, 0 );
1736 release_object( key );
1738 release_object( parent );
1742 /* open a registry key */
1743 DECL_HANDLER(open_key)
1745 struct key *key, *parent;
1746 unsigned int access = req->access;
1748 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1749 reply->hkey = 0;
1750 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1752 WCHAR *name = copy_path( get_req_data(), get_req_data_size(), !req->parent );
1753 if (name && (key = open_key( parent, name )))
1755 reply->hkey = alloc_handle( current->process, key, access, 0 );
1756 release_object( key );
1758 release_object( parent );
1762 /* delete a registry key */
1763 DECL_HANDLER(delete_key)
1765 struct key *key;
1767 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1769 delete_key( key );
1770 release_object( key );
1774 /* enumerate registry subkeys */
1775 DECL_HANDLER(enum_key)
1777 struct key *key;
1779 if ((key = get_hkey_obj( req->hkey,
1780 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1782 enum_key( key, req->index, req->info_class, reply );
1783 release_object( key );
1787 /* set a value of a registry key */
1788 DECL_HANDLER(set_key_value)
1790 struct key *key;
1791 WCHAR *name;
1793 if (!(name = copy_req_path( req->namelen, 0 ))) return;
1794 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1796 size_t datalen = get_req_data_size() - req->namelen;
1797 const char *data = (char *)get_req_data() + req->namelen;
1799 set_value( key, name, req->type, data, datalen );
1800 release_object( key );
1804 /* retrieve the value of a registry key */
1805 DECL_HANDLER(get_key_value)
1807 struct key *key;
1808 WCHAR *name;
1810 reply->total = 0;
1811 if (!(name = copy_path( get_req_data(), get_req_data_size(), 0 ))) return;
1812 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1814 get_value( key, name, &reply->type, &reply->total );
1815 release_object( key );
1819 /* enumerate the value of a registry key */
1820 DECL_HANDLER(enum_key_value)
1822 struct key *key;
1824 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1826 enum_value( key, req->index, req->info_class, reply );
1827 release_object( key );
1831 /* delete a value of a registry key */
1832 DECL_HANDLER(delete_key_value)
1834 WCHAR *name;
1835 struct key *key;
1837 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1839 if ((name = req_strdupW( req, get_req_data(), get_req_data_size() )))
1841 delete_value( key, name );
1842 free( name );
1844 release_object( key );
1848 /* load a registry branch from a file */
1849 DECL_HANDLER(load_registry)
1851 struct key *key;
1853 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1855 /* FIXME: use subkey name */
1856 load_registry( key, req->file );
1857 release_object( key );
1861 /* save a registry branch to a file */
1862 DECL_HANDLER(save_registry)
1864 struct key *key;
1866 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1868 save_registry( key, req->file );
1869 release_object( key );
1873 /* set the current and saving level for the registry */
1874 DECL_HANDLER(set_registry_levels)
1876 current_level = req->current;
1877 saving_level = req->saving;
1879 /* set periodic save timer */
1881 if (save_timeout_user)
1883 remove_timeout_user( save_timeout_user );
1884 save_timeout_user = NULL;
1886 if ((save_period = req->period))
1888 if (save_period < 10000) save_period = 10000; /* limit rate */
1889 gettimeofday( &next_save_time, 0 );
1890 add_timeout( &next_save_time, save_period );
1891 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1895 /* save a registry branch at server exit */
1896 DECL_HANDLER(save_registry_atexit)
1898 struct key *key;
1900 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1902 register_branch_for_saving( key, get_req_data(), get_req_data_size() );
1903 release_object( key );
1907 /* add a registry key change notification */
1908 DECL_HANDLER(set_registry_notification)
1910 struct key *key;
1911 struct event *event;
1912 struct notify *notify;
1914 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
1915 if( key )
1917 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
1918 if( event )
1920 notify = find_notify( key, req->hkey );
1921 if( notify )
1923 release_object( notify->event );
1924 grab_object( event );
1925 notify->event = event;
1927 else
1929 notify = (struct notify *) malloc (sizeof *notify);
1930 if( notify )
1932 grab_object( event );
1933 notify->event = event;
1934 notify->subtree = req->subtree;
1935 notify->filter = req->filter;
1936 notify->hkey = req->hkey;
1938 /* add to linked list */
1939 notify->prev = NULL;
1940 notify->next = key->first_notify;
1941 if ( notify->next )
1942 notify->next->prev = notify;
1943 else
1944 key->last_notify = notify;
1945 key->first_notify = notify;
1947 else
1948 set_error( STATUS_NO_MEMORY );
1950 release_object( event );
1952 release_object( key );