shell32: tests for RunFileDlg
[wine/kumbayo.git] / server / registry.c
blobd691e04a65bd3aa2513ee0a1519ca9937684913f
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* To do:
22 * - symbolic links
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "object.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "request.h"
46 #include "unicode.h"
47 #include "security.h"
49 #include "winternl.h"
50 #include "wine/library.h"
52 struct notify
54 struct list entry; /* entry in list of notifications */
55 struct event *event; /* event to set when changing this key */
56 int subtree; /* true if subtree notification */
57 unsigned int filter; /* which events to notify on */
58 obj_handle_t hkey; /* hkey associated with this notification */
59 struct process *process; /* process in which the hkey is valid */
62 /* a registry key */
63 struct key
65 struct object obj; /* object header */
66 WCHAR *name; /* key name */
67 WCHAR *class; /* key class */
68 unsigned short namelen; /* length of key name */
69 unsigned short classlen; /* length of class name */
70 struct key *parent; /* parent key */
71 int last_subkey; /* last in use subkey */
72 int nb_subkeys; /* count of allocated subkeys */
73 struct key **subkeys; /* subkeys array */
74 int last_value; /* last in use value */
75 int nb_values; /* count of allocated values in array */
76 struct key_value *values; /* values array */
77 unsigned int flags; /* flags */
78 time_t modif; /* last modification time */
79 struct list notify_list; /* list of notifications */
82 /* key flags */
83 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
84 #define KEY_DELETED 0x0002 /* key has been deleted */
85 #define KEY_DIRTY 0x0004 /* key has been modified */
87 /* a key value */
88 struct key_value
90 WCHAR *name; /* value name */
91 unsigned short namelen; /* length of value name */
92 unsigned short type; /* value type */
93 data_size_t len; /* value data length in bytes */
94 void *data; /* pointer to value data */
97 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
98 #define MIN_VALUES 8 /* min. number of allocated values per key */
100 #define MAX_NAME_LEN MAX_PATH /* max. length of a key name */
101 #define MAX_VALUE_LEN MAX_PATH /* max. length of a value name */
103 /* the root of the registry tree */
104 static struct key *root_key;
106 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
107 static struct timeout_user *save_timeout_user; /* saving timer */
109 static void set_periodic_save_timer(void);
111 /* information about where to save a registry branch */
112 struct save_branch_info
114 struct key *key;
115 char *path;
118 #define MAX_SAVE_BRANCH_INFO 3
119 static int save_branch_count;
120 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
123 /* information about a file being loaded */
124 struct file_load_info
126 const char *filename; /* input file name */
127 FILE *file; /* input file */
128 char *buffer; /* line buffer */
129 int len; /* buffer length */
130 int line; /* current input line */
131 WCHAR *tmp; /* temp buffer to use while parsing input */
132 size_t tmplen; /* length of temp buffer */
136 static void key_dump( struct object *obj, int verbose );
137 static unsigned int key_map_access( struct object *obj, unsigned int access );
138 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
139 static void key_destroy( struct object *obj );
141 static const struct object_ops key_ops =
143 sizeof(struct key), /* size */
144 key_dump, /* dump */
145 no_get_type, /* get_type */
146 no_add_queue, /* add_queue */
147 NULL, /* remove_queue */
148 NULL, /* signaled */
149 NULL, /* satisfied */
150 no_signal, /* signal */
151 no_get_fd, /* get_fd */
152 key_map_access, /* map_access */
153 default_get_sd, /* get_sd */
154 default_set_sd, /* set_sd */
155 no_lookup_name, /* lookup_name */
156 no_open_file, /* open_file */
157 key_close_handle, /* close_handle */
158 key_destroy /* destroy */
163 * The registry text file format v2 used by this code is similar to the one
164 * used by REGEDIT import/export functionality, with the following differences:
165 * - strings and key names can contain \x escapes for Unicode
166 * - key names use escapes too in order to support Unicode
167 * - the modification time optionally follows the key name
168 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
171 static inline char to_hex( char ch )
173 if (isdigit(ch)) return ch - '0';
174 return tolower(ch) - 'a' + 10;
177 /* dump the full path of a key */
178 static void dump_path( const struct key *key, const struct key *base, FILE *f )
180 if (key->parent && key->parent != base)
182 dump_path( key->parent, base, f );
183 fprintf( f, "\\\\" );
185 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
188 /* dump a value to a text file */
189 static void dump_value( const struct key_value *value, FILE *f )
191 unsigned int i;
192 int count;
194 if (value->namelen)
196 fputc( '\"', f );
197 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
198 count += fprintf( f, "\"=" );
200 else count = fprintf( f, "@=" );
202 switch(value->type)
204 case REG_SZ:
205 case REG_EXPAND_SZ:
206 case REG_MULTI_SZ:
207 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
208 fputc( '\"', f );
209 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
210 fputc( '\"', f );
211 break;
212 case REG_DWORD:
213 if (value->len == sizeof(DWORD))
215 DWORD dw;
216 memcpy( &dw, value->data, sizeof(DWORD) );
217 fprintf( f, "dword:%08x", dw );
218 break;
220 /* else fall through */
221 default:
222 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
223 else count += fprintf( f, "hex(%x):", value->type );
224 for (i = 0; i < value->len; i++)
226 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
227 if (i < value->len-1)
229 fputc( ',', f );
230 if (++count > 76)
232 fprintf( f, "\\\n " );
233 count = 2;
237 break;
239 fputc( '\n', f );
242 /* save a registry and all its subkeys to a text file */
243 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
245 int i;
247 if (key->flags & KEY_VOLATILE) return;
248 /* save key if it has either some values or no subkeys */
249 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
250 if ((key->last_value >= 0) || (key->last_subkey == -1))
252 fprintf( f, "\n[" );
253 if (key != base) dump_path( key, base, f );
254 fprintf( f, "] %ld\n", (long)key->modif );
255 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
257 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
260 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
262 fprintf( stderr, "%s key ", op );
263 if (key) dump_path( key, NULL, stderr );
264 else fprintf( stderr, "ERROR" );
265 if (value)
267 fprintf( stderr, " value ");
268 dump_value( value, stderr );
270 else fprintf( stderr, "\n" );
273 static void key_dump( struct object *obj, int verbose )
275 struct key *key = (struct key *)obj;
276 assert( obj->ops == &key_ops );
277 fprintf( stderr, "Key flags=%x ", key->flags );
278 dump_path( key, NULL, stderr );
279 fprintf( stderr, "\n" );
282 /* notify waiter and maybe delete the notification */
283 static void do_notification( struct key *key, struct notify *notify, int del )
285 if (notify->event)
287 set_event( notify->event );
288 release_object( notify->event );
289 notify->event = NULL;
291 if (del)
293 list_remove( &notify->entry );
294 free( notify );
298 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
300 struct notify *notify;
302 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
304 if (notify->process == process && notify->hkey == hkey) return notify;
306 return NULL;
309 static unsigned int key_map_access( struct object *obj, unsigned int access )
311 if (access & GENERIC_READ) access |= KEY_READ;
312 if (access & GENERIC_WRITE) access |= KEY_WRITE;
313 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
314 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
315 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
318 /* close the notification associated with a handle */
319 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
321 struct key * key = (struct key *) obj;
322 struct notify *notify = find_notify( key, process, handle );
323 if (notify) do_notification( key, notify, 1 );
324 return 1; /* ok to close */
327 static void key_destroy( struct object *obj )
329 int i;
330 struct list *ptr;
331 struct key *key = (struct key *)obj;
332 assert( obj->ops == &key_ops );
334 free( key->name );
335 free( key->class );
336 for (i = 0; i <= key->last_value; i++)
338 free( key->values[i].name );
339 free( key->values[i].data );
341 free( key->values );
342 for (i = 0; i <= key->last_subkey; i++)
344 key->subkeys[i]->parent = NULL;
345 release_object( key->subkeys[i] );
347 free( key->subkeys );
348 /* unconditionally notify everything waiting on this key */
349 while ((ptr = list_head( &key->notify_list )))
351 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
352 do_notification( key, notify, 1 );
356 /* get the request vararg as registry path */
357 static inline void get_req_path( struct unicode_str *str, int skip_root )
359 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
361 str->str = get_req_data();
362 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
364 if (skip_root && str->len >= sizeof(root_name) &&
365 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
367 str->str += sizeof(root_name)/sizeof(WCHAR);
368 str->len -= sizeof(root_name);
372 /* return the next token in a given path */
373 /* token->str must point inside the path, or be NULL for the first call */
374 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
376 data_size_t i = 0, len = path->len / sizeof(WCHAR);
378 if (!token->str) /* first time */
380 /* path cannot start with a backslash */
381 if (len && path->str[0] == '\\')
383 set_error( STATUS_OBJECT_PATH_INVALID );
384 return NULL;
387 else
389 i = token->str - path->str;
390 i += token->len / sizeof(WCHAR);
391 while (i < len && path->str[i] == '\\') i++;
393 token->str = path->str + i;
394 while (i < len && path->str[i] != '\\') i++;
395 token->len = (path->str + i - token->str) * sizeof(WCHAR);
396 return token;
399 /* allocate a key object */
400 static struct key *alloc_key( const struct unicode_str *name, time_t modif )
402 struct key *key;
403 if ((key = alloc_object( &key_ops )))
405 key->name = NULL;
406 key->class = NULL;
407 key->namelen = name->len;
408 key->classlen = 0;
409 key->flags = 0;
410 key->last_subkey = -1;
411 key->nb_subkeys = 0;
412 key->subkeys = NULL;
413 key->nb_values = 0;
414 key->last_value = -1;
415 key->values = NULL;
416 key->modif = modif;
417 key->parent = NULL;
418 list_init( &key->notify_list );
419 if (name->len && !(key->name = memdup( name->str, name->len )))
421 release_object( key );
422 key = NULL;
425 return key;
428 /* mark a key and all its parents as dirty (modified) */
429 static void make_dirty( struct key *key )
431 while (key)
433 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
434 key->flags |= KEY_DIRTY;
435 key = key->parent;
439 /* mark a key and all its subkeys as clean (not modified) */
440 static void make_clean( struct key *key )
442 int i;
444 if (key->flags & KEY_VOLATILE) return;
445 if (!(key->flags & KEY_DIRTY)) return;
446 key->flags &= ~KEY_DIRTY;
447 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
450 /* go through all the notifications and send them if necessary */
451 static void check_notify( struct key *key, unsigned int change, int not_subtree )
453 struct list *ptr, *next;
455 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
457 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
458 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
459 do_notification( key, n, 0 );
463 /* update key modification time */
464 static void touch_key( struct key *key, unsigned int change )
466 struct key *k;
468 key->modif = time(NULL);
469 make_dirty( key );
471 /* do notifications */
472 check_notify( key, change, 1 );
473 for ( k = key->parent; k; k = k->parent )
474 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
477 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
478 static int grow_subkeys( struct key *key )
480 struct key **new_subkeys;
481 int nb_subkeys;
483 if (key->nb_subkeys)
485 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
486 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
488 set_error( STATUS_NO_MEMORY );
489 return 0;
492 else
494 nb_subkeys = MIN_VALUES;
495 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
497 key->subkeys = new_subkeys;
498 key->nb_subkeys = nb_subkeys;
499 return 1;
502 /* allocate a subkey for a given key, and return its index */
503 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
504 int index, time_t modif )
506 struct key *key;
507 int i;
509 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
511 set_error( STATUS_NAME_TOO_LONG );
512 return NULL;
514 if (parent->last_subkey + 1 == parent->nb_subkeys)
516 /* need to grow the array */
517 if (!grow_subkeys( parent )) return NULL;
519 if ((key = alloc_key( name, modif )) != NULL)
521 key->parent = parent;
522 for (i = ++parent->last_subkey; i > index; i--)
523 parent->subkeys[i] = parent->subkeys[i-1];
524 parent->subkeys[index] = key;
526 return key;
529 /* free a subkey of a given key */
530 static void free_subkey( struct key *parent, int index )
532 struct key *key;
533 int i, nb_subkeys;
535 assert( index >= 0 );
536 assert( index <= parent->last_subkey );
538 key = parent->subkeys[index];
539 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
540 parent->last_subkey--;
541 key->flags |= KEY_DELETED;
542 key->parent = NULL;
543 release_object( key );
545 /* try to shrink the array */
546 nb_subkeys = parent->nb_subkeys;
547 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
549 struct key **new_subkeys;
550 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
551 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
552 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
553 parent->subkeys = new_subkeys;
554 parent->nb_subkeys = nb_subkeys;
558 /* find the named child of a given key and return its index */
559 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
561 int i, min, max, res;
562 data_size_t len;
564 min = 0;
565 max = key->last_subkey;
566 while (min <= max)
568 i = (min + max) / 2;
569 len = min( key->subkeys[i]->namelen, name->len );
570 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
571 if (!res) res = key->subkeys[i]->namelen - name->len;
572 if (!res)
574 *index = i;
575 return key->subkeys[i];
577 if (res > 0) max = i - 1;
578 else min = i + 1;
580 *index = min; /* this is where we should insert it */
581 return NULL;
584 /* open a subkey */
585 static struct key *open_key( struct key *key, const struct unicode_str *name )
587 int index;
588 struct unicode_str token;
590 token.str = NULL;
591 if (!get_path_token( name, &token )) return NULL;
592 while (token.len)
594 if (!(key = find_subkey( key, &token, &index )))
596 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
597 break;
599 get_path_token( name, &token );
602 if (debug_level > 1) dump_operation( key, NULL, "Open" );
603 if (key) grab_object( key );
604 return key;
607 /* create a subkey */
608 static struct key *create_key( struct key *key, const struct unicode_str *name,
609 const struct unicode_str *class, int flags, time_t modif, int *created )
611 struct key *base;
612 int index;
613 struct unicode_str token;
615 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
617 set_error( STATUS_KEY_DELETED );
618 return NULL;
620 if (!(flags & KEY_VOLATILE) && (key->flags & KEY_VOLATILE))
622 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
623 return NULL;
625 if (!modif) modif = time(NULL);
627 token.str = NULL;
628 if (!get_path_token( name, &token )) return NULL;
629 *created = 0;
630 while (token.len)
632 struct key *subkey;
633 if (!(subkey = find_subkey( key, &token, &index ))) break;
634 key = subkey;
635 get_path_token( name, &token );
638 /* create the remaining part */
640 if (!token.len) goto done;
641 *created = 1;
642 if (flags & KEY_DIRTY) make_dirty( key );
643 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
644 base = key;
645 for (;;)
647 key->flags |= flags;
648 get_path_token( name, &token );
649 if (!token.len) break;
650 /* we know the index is always 0 in a new key */
651 if (!(key = alloc_subkey( key, &token, 0, modif )))
653 free_subkey( base, index );
654 return NULL;
658 done:
659 if (debug_level > 1) dump_operation( key, NULL, "Create" );
660 if (class && class->len)
662 key->classlen = class->len;
663 free(key->class);
664 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
666 grab_object( key );
667 return key;
670 /* query information about a key or a subkey */
671 static void enum_key( const struct key *key, int index, int info_class,
672 struct enum_key_reply *reply )
674 int i;
675 data_size_t len, namelen, classlen;
676 data_size_t max_subkey = 0, max_class = 0;
677 data_size_t max_value = 0, max_data = 0;
678 char *data;
680 if (index != -1) /* -1 means use the specified key directly */
682 if ((index < 0) || (index > key->last_subkey))
684 set_error( STATUS_NO_MORE_ENTRIES );
685 return;
687 key = key->subkeys[index];
690 namelen = key->namelen;
691 classlen = key->classlen;
693 switch(info_class)
695 case KeyBasicInformation:
696 classlen = 0; /* only return the name */
697 /* fall through */
698 case KeyNodeInformation:
699 reply->max_subkey = 0;
700 reply->max_class = 0;
701 reply->max_value = 0;
702 reply->max_data = 0;
703 break;
704 case KeyFullInformation:
705 for (i = 0; i <= key->last_subkey; i++)
707 struct key *subkey = key->subkeys[i];
708 len = subkey->namelen / sizeof(WCHAR);
709 if (len > max_subkey) max_subkey = len;
710 len = subkey->classlen / sizeof(WCHAR);
711 if (len > max_class) max_class = len;
713 for (i = 0; i <= key->last_value; i++)
715 len = key->values[i].namelen / sizeof(WCHAR);
716 if (len > max_value) max_value = len;
717 len = key->values[i].len;
718 if (len > max_data) max_data = len;
720 reply->max_subkey = max_subkey;
721 reply->max_class = max_class;
722 reply->max_value = max_value;
723 reply->max_data = max_data;
724 namelen = 0; /* only return the class */
725 break;
726 default:
727 set_error( STATUS_INVALID_PARAMETER );
728 return;
730 reply->subkeys = key->last_subkey + 1;
731 reply->values = key->last_value + 1;
732 reply->modif = key->modif;
733 reply->total = namelen + classlen;
735 len = min( reply->total, get_reply_max_size() );
736 if (len && (data = set_reply_data_size( len )))
738 if (len > namelen)
740 reply->namelen = namelen;
741 memcpy( data, key->name, namelen );
742 memcpy( data + namelen, key->class, len - namelen );
744 else
746 reply->namelen = len;
747 memcpy( data, key->name, len );
750 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
753 /* delete a key and its values */
754 static int delete_key( struct key *key, int recurse )
756 int index;
757 struct key *parent;
759 /* must find parent and index */
760 if (key == root_key)
762 set_error( STATUS_ACCESS_DENIED );
763 return -1;
765 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
767 set_error( STATUS_KEY_DELETED );
768 return -1;
771 while (recurse && (key->last_subkey>=0))
772 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
773 return -1;
775 for (index = 0; index <= parent->last_subkey; index++)
776 if (parent->subkeys[index] == key) break;
777 assert( index <= parent->last_subkey );
779 /* we can only delete a key that has no subkeys */
780 if (key->last_subkey >= 0)
782 set_error( STATUS_ACCESS_DENIED );
783 return -1;
786 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
787 free_subkey( parent, index );
788 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
789 return 0;
792 /* try to grow the array of values; return 1 if OK, 0 on error */
793 static int grow_values( struct key *key )
795 struct key_value *new_val;
796 int nb_values;
798 if (key->nb_values)
800 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
801 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
803 set_error( STATUS_NO_MEMORY );
804 return 0;
807 else
809 nb_values = MIN_VALUES;
810 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
812 key->values = new_val;
813 key->nb_values = nb_values;
814 return 1;
817 /* find the named value of a given key and return its index in the array */
818 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
820 int i, min, max, res;
821 data_size_t len;
823 min = 0;
824 max = key->last_value;
825 while (min <= max)
827 i = (min + max) / 2;
828 len = min( key->values[i].namelen, name->len );
829 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
830 if (!res) res = key->values[i].namelen - name->len;
831 if (!res)
833 *index = i;
834 return &key->values[i];
836 if (res > 0) max = i - 1;
837 else min = i + 1;
839 *index = min; /* this is where we should insert it */
840 return NULL;
843 /* insert a new value; the index must have been returned by find_value */
844 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
846 struct key_value *value;
847 WCHAR *new_name = NULL;
848 int i;
850 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
852 set_error( STATUS_NAME_TOO_LONG );
853 return NULL;
855 if (key->last_value + 1 == key->nb_values)
857 if (!grow_values( key )) return NULL;
859 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
860 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
861 value = &key->values[index];
862 value->name = new_name;
863 value->namelen = name->len;
864 value->len = 0;
865 value->data = NULL;
866 return value;
869 /* set a key value */
870 static void set_value( struct key *key, const struct unicode_str *name,
871 int type, const void *data, data_size_t len )
873 struct key_value *value;
874 void *ptr = NULL;
875 int index;
877 if ((value = find_value( key, name, &index )))
879 /* check if the new value is identical to the existing one */
880 if (value->type == type && value->len == len &&
881 value->data && !memcmp( value->data, data, len ))
883 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
884 return;
888 if (len && !(ptr = memdup( data, len ))) return;
890 if (!value)
892 if (!(value = insert_value( key, name, index )))
894 free( ptr );
895 return;
898 else free( value->data ); /* already existing, free previous data */
900 value->type = type;
901 value->len = len;
902 value->data = ptr;
903 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
904 if (debug_level > 1) dump_operation( key, value, "Set" );
907 /* get a key value */
908 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
910 struct key_value *value;
911 int index;
913 if ((value = find_value( key, name, &index )))
915 *type = value->type;
916 *len = value->len;
917 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
918 if (debug_level > 1) dump_operation( key, value, "Get" );
920 else
922 *type = -1;
923 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
927 /* enumerate a key value */
928 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
930 struct key_value *value;
932 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
933 else
935 void *data;
936 data_size_t namelen, maxlen;
938 value = &key->values[i];
939 reply->type = value->type;
940 namelen = value->namelen;
942 switch(info_class)
944 case KeyValueBasicInformation:
945 reply->total = namelen;
946 break;
947 case KeyValueFullInformation:
948 reply->total = namelen + value->len;
949 break;
950 case KeyValuePartialInformation:
951 reply->total = value->len;
952 namelen = 0;
953 break;
954 default:
955 set_error( STATUS_INVALID_PARAMETER );
956 return;
959 maxlen = min( reply->total, get_reply_max_size() );
960 if (maxlen && ((data = set_reply_data_size( maxlen ))))
962 if (maxlen > namelen)
964 reply->namelen = namelen;
965 memcpy( data, value->name, namelen );
966 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
968 else
970 reply->namelen = maxlen;
971 memcpy( data, value->name, maxlen );
974 if (debug_level > 1) dump_operation( key, value, "Enum" );
978 /* delete a value */
979 static void delete_value( struct key *key, const struct unicode_str *name )
981 struct key_value *value;
982 int i, index, nb_values;
984 if (!(value = find_value( key, name, &index )))
986 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
987 return;
989 if (debug_level > 1) dump_operation( key, value, "Delete" );
990 free( value->name );
991 free( value->data );
992 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
993 key->last_value--;
994 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
996 /* try to shrink the array */
997 nb_values = key->nb_values;
998 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1000 struct key_value *new_val;
1001 nb_values -= nb_values / 3; /* shrink by 33% */
1002 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1003 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1004 key->values = new_val;
1005 key->nb_values = nb_values;
1009 /* get the registry key corresponding to an hkey handle */
1010 static inline struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1012 return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1015 /* get the registry key corresponding to a parent key handle */
1016 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1018 if (!hkey) return (struct key *)grab_object( root_key );
1019 return (struct key *)get_handle_obj( current->process, hkey, 0, &key_ops );
1022 /* read a line from the input file */
1023 static int read_next_line( struct file_load_info *info )
1025 char *newbuf;
1026 int newlen, pos = 0;
1028 info->line++;
1029 for (;;)
1031 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1032 return (pos != 0); /* EOF */
1033 pos = strlen(info->buffer);
1034 if (info->buffer[pos-1] == '\n')
1036 /* got a full line */
1037 info->buffer[--pos] = 0;
1038 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1039 return 1;
1041 if (pos < info->len - 1) return 1; /* EOF but something was read */
1043 /* need to enlarge the buffer */
1044 newlen = info->len + info->len / 2;
1045 if (!(newbuf = realloc( info->buffer, newlen )))
1047 set_error( STATUS_NO_MEMORY );
1048 return -1;
1050 info->buffer = newbuf;
1051 info->len = newlen;
1055 /* make sure the temp buffer holds enough space */
1056 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1058 WCHAR *tmp;
1059 if (info->tmplen >= size) return 1;
1060 if (!(tmp = realloc( info->tmp, size )))
1062 set_error( STATUS_NO_MEMORY );
1063 return 0;
1065 info->tmp = tmp;
1066 info->tmplen = size;
1067 return 1;
1070 /* report an error while loading an input file */
1071 static void file_read_error( const char *err, struct file_load_info *info )
1073 if (info->filename)
1074 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1075 else
1076 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1079 /* parse an escaped string back into Unicode */
1080 /* return the number of chars read from the input, or -1 on output overflow */
1081 static int parse_strW( WCHAR *dest, data_size_t *len, const char *src, char endchar )
1083 data_size_t count = sizeof(WCHAR); /* for terminating null */
1084 const char *p = src;
1085 while (*p && *p != endchar)
1087 if (*p != '\\') *dest = (WCHAR)*p++;
1088 else
1090 p++;
1091 switch(*p)
1093 case 'a': *dest = '\a'; p++; break;
1094 case 'b': *dest = '\b'; p++; break;
1095 case 'e': *dest = '\e'; p++; break;
1096 case 'f': *dest = '\f'; p++; break;
1097 case 'n': *dest = '\n'; p++; break;
1098 case 'r': *dest = '\r'; p++; break;
1099 case 't': *dest = '\t'; p++; break;
1100 case 'v': *dest = '\v'; p++; break;
1101 case 'x': /* hex escape */
1102 p++;
1103 if (!isxdigit(*p)) *dest = 'x';
1104 else
1106 *dest = to_hex(*p++);
1107 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1108 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1109 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1111 break;
1112 case '0':
1113 case '1':
1114 case '2':
1115 case '3':
1116 case '4':
1117 case '5':
1118 case '6':
1119 case '7': /* octal escape */
1120 *dest = *p++ - '0';
1121 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1122 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1123 break;
1124 default:
1125 *dest = (WCHAR)*p++;
1126 break;
1129 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1130 dest++;
1132 *dest = 0;
1133 if (!*p) return -1; /* delimiter not found */
1134 *len = count;
1135 return p + 1 - src;
1138 /* convert a data type tag to a value type */
1139 static int get_data_type( const char *buffer, int *type, int *parse_type )
1141 struct data_type { const char *tag; int len; int type; int parse_type; };
1143 static const struct data_type data_types[] =
1144 { /* actual type */ /* type to assume for parsing */
1145 { "\"", 1, REG_SZ, REG_SZ },
1146 { "str:\"", 5, REG_SZ, REG_SZ },
1147 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1148 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1149 { "hex:", 4, REG_BINARY, REG_BINARY },
1150 { "dword:", 6, REG_DWORD, REG_DWORD },
1151 { "hex(", 4, -1, REG_BINARY },
1152 { NULL, 0, 0, 0 }
1155 const struct data_type *ptr;
1156 char *end;
1158 for (ptr = data_types; ptr->tag; ptr++)
1160 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1161 *parse_type = ptr->parse_type;
1162 if ((*type = ptr->type) != -1) return ptr->len;
1163 /* "hex(xx):" is special */
1164 *type = (int)strtoul( buffer + 4, &end, 16 );
1165 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1166 return end + 2 - buffer;
1168 return 0;
1171 /* load and create a key from the input file */
1172 static struct key *load_key( struct key *base, const char *buffer, int flags,
1173 int prefix_len, struct file_load_info *info,
1174 int default_modif )
1176 WCHAR *p;
1177 struct unicode_str name;
1178 int res, modif;
1179 data_size_t len = strlen(buffer) * sizeof(WCHAR);
1181 if (!get_file_tmp_space( info, len )) return NULL;
1183 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1185 file_read_error( "Malformed key", info );
1186 return NULL;
1188 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = default_modif;
1190 p = info->tmp;
1191 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1193 if (!*p)
1195 if (prefix_len > 1)
1197 file_read_error( "Malformed key", info );
1198 return NULL;
1200 /* empty key name, return base key */
1201 return (struct key *)grab_object( base );
1203 name.str = p;
1204 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1205 return create_key( base, &name, NULL, flags, modif, &res );
1208 /* parse a comma-separated list of hex digits */
1209 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1211 const char *p = buffer;
1212 data_size_t count = 0;
1213 while (isxdigit(*p))
1215 int val;
1216 char buf[3];
1217 memcpy( buf, p, 2 );
1218 buf[2] = 0;
1219 sscanf( buf, "%x", &val );
1220 if (count++ >= *len) return -1; /* dest buffer overflow */
1221 *dest++ = (unsigned char )val;
1222 p += 2;
1223 if (*p == ',') p++;
1225 *len = count;
1226 return p - buffer;
1229 /* parse a value name and create the corresponding value */
1230 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1231 struct file_load_info *info )
1233 struct key_value *value;
1234 struct unicode_str name;
1235 int index;
1236 data_size_t maxlen = strlen(buffer) * sizeof(WCHAR);
1238 if (!get_file_tmp_space( info, maxlen )) return NULL;
1239 name.str = info->tmp;
1240 if (buffer[0] == '@')
1242 name.len = 0;
1243 *len = 1;
1245 else
1247 int r = parse_strW( info->tmp, &maxlen, buffer + 1, '\"' );
1248 if (r == -1) goto error;
1249 *len = r + 1; /* for initial quote */
1250 name.len = maxlen - sizeof(WCHAR);
1252 while (isspace(buffer[*len])) (*len)++;
1253 if (buffer[*len] != '=') goto error;
1254 (*len)++;
1255 while (isspace(buffer[*len])) (*len)++;
1256 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1257 return value;
1259 error:
1260 file_read_error( "Malformed value name", info );
1261 return NULL;
1264 /* load a value from the input file */
1265 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1267 DWORD dw;
1268 void *ptr, *newptr;
1269 int res, type, parse_type;
1270 data_size_t maxlen, len;
1271 struct key_value *value;
1273 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1274 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1275 buffer += len + res;
1277 switch(parse_type)
1279 case REG_SZ:
1280 len = strlen(buffer) * sizeof(WCHAR);
1281 if (!get_file_tmp_space( info, len )) return 0;
1282 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1283 ptr = info->tmp;
1284 break;
1285 case REG_DWORD:
1286 dw = strtoul( buffer, NULL, 16 );
1287 ptr = &dw;
1288 len = sizeof(dw);
1289 break;
1290 case REG_BINARY: /* hex digits */
1291 len = 0;
1292 for (;;)
1294 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1295 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1296 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1297 len += maxlen;
1298 buffer += res;
1299 while (isspace(*buffer)) buffer++;
1300 if (!*buffer) break;
1301 if (*buffer != '\\') goto error;
1302 if (read_next_line( info) != 1) goto error;
1303 buffer = info->buffer;
1304 while (isspace(*buffer)) buffer++;
1306 ptr = info->tmp;
1307 break;
1308 default:
1309 assert(0);
1310 ptr = NULL; /* keep compiler quiet */
1311 break;
1314 if (!len) newptr = NULL;
1315 else if (!(newptr = memdup( ptr, len ))) return 0;
1317 free( value->data );
1318 value->data = newptr;
1319 value->len = len;
1320 value->type = type;
1321 make_dirty( key );
1322 return 1;
1324 error:
1325 file_read_error( "Malformed value", info );
1326 return 0;
1329 /* return the length (in path elements) of name that is part of the key name */
1330 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1331 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1333 WCHAR *p;
1334 int res;
1335 data_size_t len = strlen(name) * sizeof(WCHAR);
1337 if (!get_file_tmp_space( info, len )) return 0;
1339 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1341 file_read_error( "Malformed key", info );
1342 return 0;
1344 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1345 len = (p - info->tmp) * sizeof(WCHAR);
1346 for (res = 1; key != root_key; res++)
1348 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1349 key = key->parent;
1351 if (key == root_key) res = 0; /* no matching name */
1352 return res;
1355 /* load all the keys from the input file */
1356 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1357 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1359 struct key *subkey = NULL;
1360 struct file_load_info info;
1361 char *p;
1362 int default_modif = time(NULL);
1363 int flags = (key->flags & KEY_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1365 info.filename = filename;
1366 info.file = f;
1367 info.len = 4;
1368 info.tmplen = 4;
1369 info.line = 0;
1370 if (!(info.buffer = mem_alloc( info.len ))) return;
1371 if (!(info.tmp = mem_alloc( info.tmplen )))
1373 free( info.buffer );
1374 return;
1377 if ((read_next_line( &info ) != 1) ||
1378 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1380 set_error( STATUS_NOT_REGISTRY_FILE );
1381 goto done;
1384 while (read_next_line( &info ) == 1)
1386 p = info.buffer;
1387 while (*p && isspace(*p)) p++;
1388 switch(*p)
1390 case '[': /* new key */
1391 if (subkey) release_object( subkey );
1392 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1393 if (!(subkey = load_key( key, p + 1, flags, prefix_len, &info, default_modif )))
1394 file_read_error( "Error creating key", &info );
1395 break;
1396 case '@': /* default value */
1397 case '\"': /* value */
1398 if (subkey) load_value( subkey, p, &info );
1399 else file_read_error( "Value without key", &info );
1400 break;
1401 case '#': /* comment */
1402 case ';': /* comment */
1403 case 0: /* empty line */
1404 break;
1405 default:
1406 file_read_error( "Unrecognized input", &info );
1407 break;
1411 done:
1412 if (subkey) release_object( subkey );
1413 free( info.buffer );
1414 free( info.tmp );
1417 /* load a part of the registry from a file */
1418 static void load_registry( struct key *key, obj_handle_t handle )
1420 struct file *file;
1421 int fd;
1423 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1424 fd = dup( get_file_unix_fd( file ) );
1425 release_object( file );
1426 if (fd != -1)
1428 FILE *f = fdopen( fd, "r" );
1429 if (f)
1431 load_keys( key, NULL, f, -1 );
1432 fclose( f );
1434 else file_set_error();
1438 /* load one of the initial registry files */
1439 static void load_init_registry_from_file( const char *filename, struct key *key )
1441 FILE *f;
1443 if ((f = fopen( filename, "r" )))
1445 load_keys( key, filename, f, 0 );
1446 fclose( f );
1447 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1449 fprintf( stderr, "%s is not a valid registry file\n", filename );
1450 return;
1454 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1456 if ((save_branch_info[save_branch_count].path = strdup( filename )))
1458 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1459 make_object_static( &key->obj );
1463 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1465 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1466 static const WCHAR formatW[] = {'-','%','u',0};
1467 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1468 WCHAR *p = buffer;
1469 unsigned int i;
1471 strcpyW( p, prefixW );
1472 p += strlenW( prefixW );
1473 p += sprintfW( p, formatW, sid->Revision );
1474 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1475 sid->IdentifierAuthority.Value[4] ),
1476 MAKEWORD( sid->IdentifierAuthority.Value[3],
1477 sid->IdentifierAuthority.Value[2] )));
1478 for (i = 0; i < sid->SubAuthorityCount; i++)
1479 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1481 path->len = (p - buffer) * sizeof(WCHAR);
1482 path->str = p = memdup( buffer, path->len );
1483 return p;
1486 /* registry initialisation */
1487 void init_registry(void)
1489 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1490 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1491 static const struct unicode_str root_name = { NULL, 0 };
1492 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1493 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1495 WCHAR *current_user_path;
1496 struct unicode_str current_user_str;
1498 const char *config = wine_get_config_dir();
1499 char *p, *filename;
1500 struct key *key;
1501 int dummy;
1503 /* create the root key */
1504 root_key = alloc_key( &root_name, time(NULL) );
1505 assert( root_key );
1506 make_object_static( &root_key->obj );
1508 if (!(filename = malloc( strlen(config) + 16 ))) fatal_error( "out of memory\n" );
1509 strcpy( filename, config );
1510 p = filename + strlen(filename);
1512 /* load system.reg into Registry\Machine */
1514 if (!(key = create_key( root_key, &HKLM_name, NULL, 0, time(NULL), &dummy )))
1515 fatal_error( "could not create Machine registry key\n" );
1517 strcpy( p, "/system.reg" );
1518 load_init_registry_from_file( filename, key );
1519 release_object( key );
1521 /* load userdef.reg into Registry\User\.Default */
1523 if (!(key = create_key( root_key, &HKU_name, NULL, 0, time(NULL), &dummy )))
1524 fatal_error( "could not create User\\.Default registry key\n" );
1526 strcpy( p, "/userdef.reg" );
1527 load_init_registry_from_file( filename, key );
1528 release_object( key );
1530 /* load user.reg into HKEY_CURRENT_USER */
1532 /* FIXME: match default user in token.c. should get from process token instead */
1533 current_user_path = format_user_registry_path( security_interactive_sid, &current_user_str );
1534 if (!current_user_path ||
1535 !(key = create_key( root_key, &current_user_str, NULL, 0, time(NULL), &dummy )))
1536 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1537 free( current_user_path );
1538 strcpy( p, "/user.reg" );
1539 load_init_registry_from_file( filename, key );
1540 release_object( key );
1542 free( filename );
1544 /* start the periodic save timer */
1545 set_periodic_save_timer();
1548 /* save a registry branch to a file */
1549 static void save_all_subkeys( struct key *key, FILE *f )
1551 fprintf( f, "WINE REGISTRY Version 2\n" );
1552 fprintf( f, ";; All keys relative to " );
1553 dump_path( key, NULL, f );
1554 fprintf( f, "\n" );
1555 save_subkeys( key, key, f );
1558 /* save a registry branch to a file handle */
1559 static void save_registry( struct key *key, obj_handle_t handle )
1561 struct file *file;
1562 int fd;
1564 if (key->flags & KEY_DELETED)
1566 set_error( STATUS_KEY_DELETED );
1567 return;
1569 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1570 fd = dup( get_file_unix_fd( file ) );
1571 release_object( file );
1572 if (fd != -1)
1574 FILE *f = fdopen( fd, "w" );
1575 if (f)
1577 save_all_subkeys( key, f );
1578 if (fclose( f )) file_set_error();
1580 else
1582 file_set_error();
1583 close( fd );
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 );
1668 done:
1669 free( tmp );
1670 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;
1680 save_timeout_user = NULL;
1681 for (i = 0; i < save_branch_count; i++)
1682 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1683 set_periodic_save_timer();
1686 /* start the periodic save timer */
1687 static void set_periodic_save_timer(void)
1689 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1690 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
1693 /* save the modified registry branches to disk */
1694 void flush_registry(void)
1696 int i;
1698 for (i = 0; i < save_branch_count; i++)
1700 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1702 fprintf( stderr, "wineserver: could not save registry branch to %s",
1703 save_branch_info[i].path );
1704 perror( " " );
1710 /* create a registry key */
1711 DECL_HANDLER(create_key)
1713 struct key *key = NULL, *parent;
1714 struct unicode_str name, class;
1715 unsigned int access = req->access;
1717 reply->hkey = 0;
1719 if (req->namelen > get_req_data_size())
1721 set_error( STATUS_INVALID_PARAMETER );
1722 return;
1724 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1725 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1726 get_req_path( &name, !req->parent );
1727 if (name.str > class.str)
1729 set_error( STATUS_INVALID_PARAMETER );
1730 return;
1732 name.len = (class.str - name.str) * sizeof(WCHAR);
1734 /* NOTE: no access rights are required from the parent handle to create a key */
1735 if ((parent = get_parent_hkey_obj( req->parent )))
1737 int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1739 if ((key = create_key( parent, &name, &class, flags, req->modif, &reply->created )))
1741 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1742 release_object( key );
1744 release_object( parent );
1748 /* open a registry key */
1749 DECL_HANDLER(open_key)
1751 struct key *key, *parent;
1752 struct unicode_str name;
1753 unsigned int access = req->access;
1755 reply->hkey = 0;
1756 /* NOTE: no access rights are required to open the parent key, only the child key */
1757 if ((parent = get_parent_hkey_obj( req->parent )))
1759 get_req_path( &name, !req->parent );
1760 if ((key = open_key( parent, &name )))
1762 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1763 release_object( key );
1765 release_object( parent );
1769 /* delete a registry key */
1770 DECL_HANDLER(delete_key)
1772 struct key *key;
1774 if ((key = get_hkey_obj( req->hkey, DELETE )))
1776 delete_key( key, 0);
1777 release_object( key );
1781 /* flush a registry key */
1782 DECL_HANDLER(flush_key)
1784 struct key *key = get_hkey_obj( req->hkey, 0 );
1785 if (key)
1787 /* we don't need to do anything here with the current implementation */
1788 release_object( key );
1792 /* enumerate registry subkeys */
1793 DECL_HANDLER(enum_key)
1795 struct key *key;
1797 if ((key = get_hkey_obj( req->hkey,
1798 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1800 enum_key( key, req->index, req->info_class, reply );
1801 release_object( key );
1805 /* set a value of a registry key */
1806 DECL_HANDLER(set_key_value)
1808 struct key *key;
1809 struct unicode_str name;
1811 if (req->namelen > get_req_data_size())
1813 set_error( STATUS_INVALID_PARAMETER );
1814 return;
1816 name.str = get_req_data();
1817 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
1819 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1821 data_size_t datalen = get_req_data_size() - req->namelen;
1822 const char *data = (const char *)get_req_data() + req->namelen;
1824 set_value( key, &name, req->type, data, datalen );
1825 release_object( key );
1829 /* retrieve the value of a registry key */
1830 DECL_HANDLER(get_key_value)
1832 struct key *key;
1833 struct unicode_str name;
1835 reply->total = 0;
1836 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1838 get_req_unicode_str( &name );
1839 get_value( key, &name, &reply->type, &reply->total );
1840 release_object( key );
1844 /* enumerate the value of a registry key */
1845 DECL_HANDLER(enum_key_value)
1847 struct key *key;
1849 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1851 enum_value( key, req->index, req->info_class, reply );
1852 release_object( key );
1856 /* delete a value of a registry key */
1857 DECL_HANDLER(delete_key_value)
1859 struct key *key;
1860 struct unicode_str name;
1862 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1864 get_req_unicode_str( &name );
1865 delete_value( key, &name );
1866 release_object( key );
1870 /* load a registry branch from a file */
1871 DECL_HANDLER(load_registry)
1873 struct key *key, *parent;
1874 struct token *token = thread_get_impersonation_token( current );
1875 struct unicode_str name;
1877 const LUID_AND_ATTRIBUTES privs[] =
1879 { SeBackupPrivilege, 0 },
1880 { SeRestorePrivilege, 0 },
1883 if (!token || !token_check_privileges( token, TRUE, privs,
1884 sizeof(privs)/sizeof(privs[0]), NULL ))
1886 set_error( STATUS_PRIVILEGE_NOT_HELD );
1887 return;
1890 if ((parent = get_parent_hkey_obj( req->hkey )))
1892 int dummy;
1893 get_req_path( &name, !req->hkey );
1894 if ((key = create_key( parent, &name, NULL, KEY_DIRTY, time(NULL), &dummy )))
1896 load_registry( key, req->file );
1897 release_object( key );
1899 release_object( parent );
1903 DECL_HANDLER(unload_registry)
1905 struct key *key;
1906 struct token *token = thread_get_impersonation_token( current );
1908 const LUID_AND_ATTRIBUTES privs[] =
1910 { SeBackupPrivilege, 0 },
1911 { SeRestorePrivilege, 0 },
1914 if (!token || !token_check_privileges( token, TRUE, privs,
1915 sizeof(privs)/sizeof(privs[0]), NULL ))
1917 set_error( STATUS_PRIVILEGE_NOT_HELD );
1918 return;
1921 if ((key = get_hkey_obj( req->hkey, 0 )))
1923 delete_key( key, 1 ); /* FIXME */
1924 release_object( key );
1928 /* save a registry branch to a file */
1929 DECL_HANDLER(save_registry)
1931 struct key *key;
1933 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
1935 set_error( STATUS_PRIVILEGE_NOT_HELD );
1936 return;
1939 if ((key = get_hkey_obj( req->hkey, 0 )))
1941 save_registry( key, req->file );
1942 release_object( key );
1946 /* add a registry key change notification */
1947 DECL_HANDLER(set_registry_notification)
1949 struct key *key;
1950 struct event *event;
1951 struct notify *notify;
1953 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
1954 if (key)
1956 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
1957 if (event)
1959 notify = find_notify( key, current->process, req->hkey );
1960 if (notify)
1962 if (notify->event)
1963 release_object( notify->event );
1964 grab_object( event );
1965 notify->event = event;
1967 else
1969 notify = mem_alloc( sizeof(*notify) );
1970 if (notify)
1972 grab_object( event );
1973 notify->event = event;
1974 notify->subtree = req->subtree;
1975 notify->filter = req->filter;
1976 notify->hkey = req->hkey;
1977 notify->process = current->process;
1978 list_add_head( &key->notify_list, &notify->entry );
1981 release_object( event );
1983 release_object( key );