2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
8 * - behavior with deleted keys
9 * - values larger than request buffer
30 #include "winnt.h" /* registry definitions */
36 struct object obj
; /* object header */
37 WCHAR
*name
; /* key name */
38 WCHAR
*class; /* key class */
39 struct key
*parent
; /* parent key */
40 int last_subkey
; /* last in use subkey */
41 int nb_subkeys
; /* count of allocated subkeys */
42 struct key
**subkeys
; /* subkeys array */
43 int last_value
; /* last in use value */
44 int nb_values
; /* count of allocated values in array */
45 struct key_value
*values
; /* values array */
46 short flags
; /* flags */
47 short level
; /* saving level */
48 time_t modif
; /* last modification time */
52 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
53 #define KEY_DELETED 0x0002 /* key has been deleted */
54 #define KEY_ROOT 0x0004 /* key is a root key */
59 WCHAR
*name
; /* value name */
60 int type
; /* value type */
61 size_t len
; /* value data length in bytes */
62 void *data
; /* pointer to value data */
65 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
66 #define MIN_VALUES 8 /* min. number of allocated values per key */
70 #define HKEY_ROOT_FIRST HKEY_CLASSES_ROOT
71 #define HKEY_ROOT_LAST HKEY_DYN_DATA
72 #define NB_ROOT_KEYS (HKEY_ROOT_LAST - HKEY_ROOT_FIRST + 1)
73 #define IS_ROOT_HKEY(h) (((h) >= HKEY_ROOT_FIRST) && ((h) <= HKEY_ROOT_LAST))
74 static struct key
*root_keys
[NB_ROOT_KEYS
];
76 static const char * const root_key_names
[NB_ROOT_KEYS
] =
82 "HKEY_PERFORMANCE_DATA",
83 "HKEY_CURRENT_CONFIG",
88 /* keys saving level */
89 /* current_level is the level that is put into all newly created or modified keys */
90 /* saving_level is the minimum level that a key needs in order to get saved */
91 static int current_level
;
92 static int saving_level
;
94 static int saving_version
= 1; /* file format version */
96 static struct timeval next_save_time
; /* absolute time of next periodic save */
97 static int save_period
; /* delay between periodic saves (ms) */
98 static struct timeout_user
*save_timeout_user
; /* saving timer */
100 /* information about where to save a registry branch */
101 struct save_branch_info
107 #define MAX_SAVE_BRANCH_INFO 8
108 static int save_branch_count
;
109 static struct save_branch_info save_branch_info
[MAX_SAVE_BRANCH_INFO
];
112 /* information about a file being loaded */
113 struct file_load_info
115 FILE *file
; /* input file */
116 char *buffer
; /* line buffer */
117 int len
; /* buffer length */
118 int line
; /* current input line */
119 char *tmp
; /* temp buffer to use while parsing input */
120 int tmplen
; /* length of temp buffer */
124 static void key_dump( struct object
*obj
, int verbose
);
125 static void key_destroy( struct object
*obj
);
127 static const struct object_ops key_ops
=
129 sizeof(struct key
), /* size */
131 no_add_queue
, /* add_queue */
132 NULL
, /* remove_queue */
134 NULL
, /* satisfied */
135 NULL
, /* get_poll_events */
136 NULL
, /* poll_event */
137 no_read_fd
, /* get_read_fd */
138 no_write_fd
, /* get_write_fd */
139 no_flush
, /* flush */
140 no_get_file_info
, /* get_file_info */
141 key_destroy
/* destroy */
146 * The registry text file format v2 used by this code is similar to the one
147 * used by REGEDIT import/export functionality, with the following differences:
148 * - strings and key names can contain \x escapes for Unicode
149 * - key names use escapes too in order to support Unicode
150 * - the modification time optionally follows the key name
151 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
154 static inline char to_hex( char ch
)
156 if (isdigit(ch
)) return ch
- '0';
157 return tolower(ch
) - 'a' + 10;
160 /* dump the full path of a key */
161 static void dump_path( struct key
*key
, struct key
*base
, FILE *f
)
163 if (key
->parent
&& key
!= base
)
165 dump_path( key
->parent
, base
, f
);
166 fprintf( f
, "\\\\" );
169 if (key
->name
) dump_strW( key
->name
, strlenW(key
->name
), f
, "[]" );
173 for (i
= 0; i
< NB_ROOT_KEYS
; i
++)
174 if (root_keys
[i
] == key
) fprintf( f
, "%s", root_key_names
[i
] );
178 /* dump a value to a text file */
179 static void dump_value( struct key_value
*value
, FILE *f
)
186 count
= 1 + dump_strW( value
->name
, strlenW(value
->name
), f
, "\"\"" );
187 count
+= fprintf( f
, "\"=" );
189 else count
= fprintf( f
, "@=" );
196 if (value
->type
!= REG_SZ
) fprintf( f
, "str(%d):", value
->type
);
198 if (value
->data
) dump_strW( (WCHAR
*)value
->data
, value
->len
/ sizeof(WCHAR
), f
, "\"\"" );
202 if (value
->len
== sizeof(DWORD
))
205 memcpy( &dw
, value
->data
, sizeof(DWORD
) );
206 fprintf( f
, "dword:%08lx", dw
);
209 /* else fall through */
211 if (value
->type
== REG_BINARY
) count
+= fprintf( f
, "hex:" );
212 else count
+= fprintf( f
, "hex(%x):", value
->type
);
213 for (i
= 0; i
< value
->len
; i
++)
215 count
+= fprintf( f
, "%02x", *((unsigned char *)value
->data
+ i
) );
216 if (i
< value
->len
-1)
221 fprintf( f
, "\\\n " );
231 /* save a registry and all its subkeys to a text file */
232 static void save_subkeys( struct key
*key
, struct key
*base
, FILE *f
)
236 if (key
->flags
& KEY_VOLATILE
) return;
237 /* save key if it has the proper level, and has either some values or no subkeys */
238 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
239 if ((key
->level
>= saving_level
) && ((key
->last_value
>= 0) || (key
->last_subkey
== -1)))
242 dump_path( key
, base
, f
);
243 fprintf( f
, "] %ld\n", key
->modif
);
244 for (i
= 0; i
<= key
->last_value
; i
++) dump_value( &key
->values
[i
], f
);
246 for (i
= 0; i
<= key
->last_subkey
; i
++) save_subkeys( key
->subkeys
[i
], base
, f
);
249 static void dump_operation( struct key
*key
, struct key_value
*value
, const char *op
)
251 fprintf( stderr
, "%s key ", op
);
252 if (key
) dump_path( key
, NULL
, stderr
);
253 else fprintf( stderr
, "ERROR" );
256 fprintf( stderr
, " value ");
257 dump_value( value
, stderr
);
259 else fprintf( stderr
, "\n" );
262 static void key_dump( struct object
*obj
, int verbose
)
264 struct key
*key
= (struct key
*)obj
;
265 assert( obj
->ops
== &key_ops
);
266 fprintf( stderr
, "Key flags=%x ", key
->flags
);
267 dump_path( key
, NULL
, stderr
);
268 fprintf( stderr
, "\n" );
271 static void key_destroy( struct object
*obj
)
274 struct key
*key
= (struct key
*)obj
;
275 assert( obj
->ops
== &key_ops
);
278 if (key
->class) free( key
->class );
279 for (i
= 0; i
<= key
->last_value
; i
++)
281 free( key
->values
[i
].name
);
282 if (key
->values
[i
].data
) free( key
->values
[i
].data
);
284 for (i
= 0; i
<= key
->last_subkey
; i
++)
286 key
->subkeys
[i
]->parent
= NULL
;
287 release_object( key
->subkeys
[i
] );
291 /* duplicate a key path from the request buffer */
292 /* returns a pointer to a static buffer, so only useable once per request */
293 static WCHAR
*copy_path( const path_t path
)
295 static WCHAR buffer
[MAX_PATH
+1];
298 while (p
< buffer
+ sizeof(buffer
) - 1) if (!(*p
++ = *path
++)) break;
303 /* return the next token in a given path */
304 /* returns a pointer to a static buffer, so only useable once per request */
305 static WCHAR
*get_path_token( const WCHAR
*initpath
, size_t maxlen
)
307 static const WCHAR
*path
;
308 static const WCHAR
*end
;
309 static WCHAR buffer
[MAX_PATH
+1];
315 end
= path
+ maxlen
/ sizeof(WCHAR
);
317 while ((path
< end
) && (*path
== '\\')) path
++;
318 while ((path
< end
) && (p
< buffer
+ sizeof(buffer
) - 1))
321 if (!ch
|| (ch
== '\\')) break;
329 /* duplicate a Unicode string from the request buffer */
330 static WCHAR
*req_strdupW( const void *req
, const WCHAR
*str
)
333 size_t len
= get_req_strlenW( req
, str
);
334 if ((name
= mem_alloc( (len
+ 1) * sizeof(WCHAR
) )) != NULL
)
336 memcpy( name
, str
, len
* sizeof(WCHAR
) );
342 /* allocate a key object */
343 static struct key
*alloc_key( const WCHAR
*name
, time_t modif
)
346 if ((key
= (struct key
*)alloc_object( &key_ops
, -1 )))
351 key
->last_subkey
= -1;
355 key
->last_value
= -1;
357 key
->level
= current_level
;
360 if (name
&& !(key
->name
= strdupW( name
)))
362 release_object( key
);
369 /* update key modification time */
370 static void touch_key( struct key
*key
)
372 key
->modif
= time(NULL
);
373 key
->level
= max( key
->level
, current_level
);
376 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
377 static int grow_subkeys( struct key
*key
)
379 struct key
**new_subkeys
;
384 nb_subkeys
= key
->nb_subkeys
+ (key
->nb_subkeys
/ 2); /* grow by 50% */
385 if (!(new_subkeys
= realloc( key
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) )))
387 set_error( STATUS_NO_MEMORY
);
393 nb_subkeys
= MIN_VALUES
;
394 if (!(new_subkeys
= mem_alloc( nb_subkeys
* sizeof(*new_subkeys
) ))) return 0;
396 key
->subkeys
= new_subkeys
;
397 key
->nb_subkeys
= nb_subkeys
;
401 /* allocate a subkey for a given key, and return its index */
402 static struct key
*alloc_subkey( struct key
*parent
, const WCHAR
*name
, int index
, time_t modif
)
407 if (parent
->last_subkey
+ 1 == parent
->nb_subkeys
)
409 /* need to grow the array */
410 if (!grow_subkeys( parent
)) return NULL
;
412 if ((key
= alloc_key( name
, modif
)) != NULL
)
414 key
->parent
= parent
;
415 for (i
= ++parent
->last_subkey
; i
> index
; i
--)
416 parent
->subkeys
[i
] = parent
->subkeys
[i
-1];
417 parent
->subkeys
[index
] = key
;
422 /* free a subkey of a given key */
423 static void free_subkey( struct key
*parent
, int index
)
428 assert( index
>= 0 );
429 assert( index
<= parent
->last_subkey
);
431 key
= parent
->subkeys
[index
];
432 for (i
= index
; i
< parent
->last_subkey
; i
++) parent
->subkeys
[i
] = parent
->subkeys
[i
+ 1];
433 parent
->last_subkey
--;
434 key
->flags
|= KEY_DELETED
;
436 release_object( key
);
438 /* try to shrink the array */
439 nb_subkeys
= key
->nb_subkeys
;
440 if (nb_subkeys
> MIN_SUBKEYS
&& key
->last_subkey
< nb_subkeys
/ 2)
442 struct key
**new_subkeys
;
443 nb_subkeys
-= nb_subkeys
/ 3; /* shrink by 33% */
444 if (nb_subkeys
< MIN_SUBKEYS
) nb_subkeys
= MIN_SUBKEYS
;
445 if (!(new_subkeys
= realloc( key
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) ))) return;
446 key
->subkeys
= new_subkeys
;
447 key
->nb_subkeys
= nb_subkeys
;
451 /* find the named child of a given key and return its index */
452 static struct key
*find_subkey( struct key
*key
, const WCHAR
*name
, int *index
)
454 int i
, min
, max
, res
;
457 max
= key
->last_subkey
;
461 if (!(res
= strcmpiW( key
->subkeys
[i
]->name
, name
)))
464 return key
->subkeys
[i
];
466 if (res
> 0) max
= i
- 1;
469 *index
= min
; /* this is where we should insert it */
474 static struct key
*open_key( struct key
*key
, const WCHAR
*name
, size_t maxlen
)
479 path
= get_path_token( name
, maxlen
);
482 if (!(key
= find_subkey( key
, path
, &index
)))
484 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
487 path
= get_path_token( NULL
, 0 );
490 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
491 if (key
) grab_object( key
);
495 /* create a subkey */
496 static struct key
*create_key( struct key
*key
, const WCHAR
*name
, size_t maxlen
, WCHAR
*class,
497 unsigned int options
, time_t modif
, int *created
)
500 int base_idx
, index
, flags
= 0;
503 if (key
->flags
& KEY_DELETED
) /* we cannot create a subkey under a deleted key */
505 set_error( STATUS_KEY_DELETED
);
508 if (options
& REG_OPTION_VOLATILE
) flags
|= KEY_VOLATILE
;
509 else if (key
->flags
& KEY_VOLATILE
)
511 set_error( STATUS_CHILD_MUST_BE_VOLATILE
);
515 path
= get_path_token( name
, maxlen
);
520 if (!(subkey
= find_subkey( key
, path
, &index
))) break;
522 path
= get_path_token( NULL
, 0 );
525 /* create the remaining part */
527 if (!*path
) goto done
;
531 key
= alloc_subkey( key
, path
, index
, modif
);
535 path
= get_path_token( NULL
, 0 );
536 if (!*path
) goto done
;
537 /* we know the index is always 0 in a new key */
538 key
= alloc_subkey( key
, path
, 0, modif
);
540 if (base_idx
!= -1) free_subkey( base
, base_idx
);
544 if (debug_level
> 1) dump_operation( key
, NULL
, "Create" );
545 if (class) key
->class = strdupW(class);
550 /* find a subkey of a given key by its index */
551 static void enum_key( struct key
*parent
, int index
, WCHAR
*name
, WCHAR
*class, time_t *modif
)
555 if ((index
< 0) || (index
> parent
->last_subkey
)) set_error( STATUS_NO_MORE_ENTRIES
);
558 key
= parent
->subkeys
[index
];
560 strcpyW( name
, key
->name
);
561 if (key
->class) strcpyW( class, key
->class ); /* FIXME: length */
563 if (debug_level
> 1) dump_operation( key
, NULL
, "Enum" );
567 /* query information about a key */
568 static void query_key( struct key
*key
, struct query_key_info_request
*req
)
571 int max_subkey
= 0, max_class
= 0;
572 int max_value
= 0, max_data
= 0;
574 for (i
= 0; i
<= key
->last_subkey
; i
++)
576 struct key
*subkey
= key
->subkeys
[i
];
577 len
= strlenW( subkey
->name
);
578 if (len
> max_subkey
) max_subkey
= len
;
579 if (!subkey
->class) continue;
580 len
= strlenW( subkey
->class );
581 if (len
> max_class
) max_class
= len
;
583 for (i
= 0; i
<= key
->last_value
; i
++)
585 len
= strlenW( key
->values
[i
].name
);
586 if (len
> max_value
) max_value
= len
;
587 len
= key
->values
[i
].len
;
588 if (len
> max_data
) max_data
= len
;
590 req
->subkeys
= key
->last_subkey
+ 1;
591 req
->max_subkey
= max_subkey
;
592 req
->max_class
= max_class
;
593 req
->values
= key
->last_value
+ 1;
594 req
->max_value
= max_value
;
595 req
->max_data
= max_data
;
596 req
->modif
= key
->modif
;
597 strcpyW( req
->name
, key
->name
);
598 if (key
->class) strcpyW( req
->class, key
->class ); /* FIXME: length */
599 else req
->class[0] = 0;
600 if (debug_level
> 1) dump_operation( key
, NULL
, "Query" );
603 /* delete a key and its values */
604 static void delete_key( struct key
*key
, const WCHAR
*name
, size_t maxlen
)
610 path
= get_path_token( name
, maxlen
);
613 /* deleting this key, must find parent and index */
614 if (key
->flags
& KEY_ROOT
)
616 set_error( STATUS_ACCESS_DENIED
);
619 if (!(parent
= key
->parent
) || (key
->flags
& KEY_DELETED
))
621 set_error( STATUS_KEY_DELETED
);
624 for (index
= 0; index
<= parent
->last_subkey
; index
++)
625 if (parent
->subkeys
[index
] == key
) break;
626 assert( index
<= parent
->last_subkey
);
631 if (!(key
= find_subkey( parent
, path
, &index
)))
633 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
636 path
= get_path_token( NULL
, 0 );
639 /* we can only delete a key that has no subkeys (FIXME) */
640 if ((key
->flags
& KEY_ROOT
) || (key
->last_subkey
>= 0))
642 set_error( STATUS_ACCESS_DENIED
);
645 if (debug_level
> 1) dump_operation( key
, NULL
, "Delete" );
646 free_subkey( parent
, index
);
650 /* try to grow the array of values; return 1 if OK, 0 on error */
651 static int grow_values( struct key
*key
)
653 struct key_value
*new_val
;
658 nb_values
= key
->nb_values
+ (key
->nb_values
/ 2); /* grow by 50% */
659 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) )))
661 set_error( STATUS_NO_MEMORY
);
667 nb_values
= MIN_VALUES
;
668 if (!(new_val
= mem_alloc( nb_values
* sizeof(*new_val
) ))) return 0;
670 key
->values
= new_val
;
671 key
->nb_values
= nb_values
;
675 /* find the named value of a given key and return its index in the array */
676 static struct key_value
*find_value( const struct key
*key
, const WCHAR
*name
, int *index
)
678 int i
, min
, max
, res
;
681 max
= key
->last_value
;
685 if (!(res
= strcmpiW( key
->values
[i
].name
, name
)))
688 return &key
->values
[i
];
690 if (res
> 0) max
= i
- 1;
693 *index
= min
; /* this is where we should insert it */
697 /* insert a new value or return a pointer to an existing one */
698 static struct key_value
*insert_value( struct key
*key
, const WCHAR
*name
)
700 struct key_value
*value
;
704 if (!(value
= find_value( key
, name
, &index
)))
706 /* not found, add it */
707 if (key
->last_value
+ 1 == key
->nb_values
)
709 if (!grow_values( key
)) return NULL
;
711 if (!(new_name
= strdupW(name
))) return NULL
;
712 for (i
= ++key
->last_value
; i
> index
; i
--) key
->values
[i
] = key
->values
[i
- 1];
713 value
= &key
->values
[index
];
714 value
->name
= new_name
;
721 /* set a key value */
722 static void set_value( struct key
*key
, WCHAR
*name
, int type
, int datalen
, void *data
)
724 struct key_value
*value
;
727 /* first copy the data */
730 if (!(ptr
= mem_alloc( datalen
))) return;
731 memcpy( ptr
, data
, datalen
);
734 if (!(value
= insert_value( key
, name
)))
736 if (ptr
) free( ptr
);
739 if (value
->data
) free( value
->data
); /* already existing, free previous data */
741 value
->len
= datalen
;
744 if (debug_level
> 1) dump_operation( key
, value
, "Set" );
747 /* get a key value */
748 static void get_value( struct key
*key
, WCHAR
*name
, int *type
, int *len
, void *data
)
750 struct key_value
*value
;
753 if ((value
= find_value( key
, name
, &index
)))
757 if (value
->data
) memcpy( data
, value
->data
, value
->len
);
758 if (debug_level
> 1) dump_operation( key
, value
, "Get" );
763 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
767 /* enumerate a key value */
768 static void enum_value( struct key
*key
, int i
, WCHAR
*name
, int *type
, int *len
, void *data
)
770 struct key_value
*value
;
772 if (i
< 0 || i
> key
->last_value
) set_error( STATUS_NO_MORE_ENTRIES
);
775 value
= &key
->values
[i
];
776 strcpyW( name
, value
->name
);
779 if (value
->data
) memcpy( data
, value
->data
, value
->len
);
780 if (debug_level
> 1) dump_operation( key
, value
, "Enum" );
785 static void delete_value( struct key
*key
, const WCHAR
*name
)
787 struct key_value
*value
;
788 int i
, index
, nb_values
;
790 if (!(value
= find_value( key
, name
, &index
)))
792 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
795 if (debug_level
> 1) dump_operation( key
, value
, "Delete" );
797 if (value
->data
) free( value
->data
);
798 for (i
= index
; i
< key
->last_value
; i
++) key
->values
[i
] = key
->values
[i
+ 1];
802 /* try to shrink the array */
803 nb_values
= key
->nb_values
;
804 if (nb_values
> MIN_VALUES
&& key
->last_value
< nb_values
/ 2)
806 struct key_value
*new_val
;
807 nb_values
-= nb_values
/ 3; /* shrink by 33% */
808 if (nb_values
< MIN_VALUES
) nb_values
= MIN_VALUES
;
809 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) ))) return;
810 key
->values
= new_val
;
811 key
->nb_values
= nb_values
;
815 static struct key
*get_hkey_obj( int hkey
, unsigned int access
);
817 static struct key
*create_root_key( int hkey
)
824 /* the two real root-keys */
825 case HKEY_LOCAL_MACHINE
:
827 static const WCHAR name
[] = { 'M','A','C','H','I','N','E',0 };
828 key
= alloc_key( name
, time(NULL
) );
833 static const WCHAR name
[] = { 'U','S','E','R',0 };
834 key
= alloc_key( name
, time(NULL
) );
837 /* special subkeys */
838 case HKEY_CLASSES_ROOT
:
840 static const WCHAR name
[] =
841 { 'S','O','F','T','W','A','R','E','\\','C','l','a','s','s','e','s',0 };
843 struct key
*root
= get_hkey_obj( HKEY_LOCAL_MACHINE
, 0 );
845 key
= create_key( root
, name
, sizeof(name
), NULL
, 0, time(NULL
), &dummy
);
846 release_object( root
);
849 case HKEY_CURRENT_CONFIG
:
851 static const WCHAR name
[] = {
852 'S','Y','S','T','E','M','\\',
853 'C','U','R','R','E','N','T','C','O','N','T','R','O','L','S','E','T','\\',
854 'H','A','R','D','W','A','R','E','P','R','O','F','I','L','E','S','\\',
855 'C','U','R','R','E','N','T',0};
856 struct key
*root
= get_hkey_obj( HKEY_LOCAL_MACHINE
, 0 );
858 key
= create_key( root
, name
, sizeof(name
), NULL
, 0, time(NULL
), &dummy
);
859 release_object( root
);
862 case HKEY_CURRENT_USER
:
864 /* get the current user name */
869 struct passwd
*pwd
= getpwuid( getuid() );
871 if (pwd
) p
= pwd
->pw_name
;
874 sprintf( buffer
, "%ld", (long) getuid() );
878 if ((name
= mem_alloc( (len
+1) * sizeof(WCHAR
) )))
880 struct key
*root
= get_hkey_obj( HKEY_USERS
, 0 );
882 for (i
= 0; i
<= len
; i
++) name
[i
] = p
[i
];
883 key
= create_key( root
, name
, (len
+1) * sizeof(WCHAR
),
884 NULL
, 0, time(NULL
), &dummy
);
885 release_object( root
);
891 /* dynamically generated keys */
892 case HKEY_PERFORMANCE_DATA
:
894 key
= alloc_key( NULL
, time(NULL
) );
902 root_keys
[hkey
- HKEY_ROOT_FIRST
] = key
;
903 key
->flags
|= KEY_ROOT
;
908 /* get the registry key corresponding to an hkey handle */
909 static struct key
*get_hkey_obj( int hkey
, unsigned int access
)
913 if (IS_ROOT_HKEY(hkey
))
915 if (!(key
= root_keys
[hkey
- HKEY_ROOT_FIRST
])) key
= create_root_key( hkey
);
919 key
= (struct key
*)get_handle_obj( current
->process
, hkey
, access
, &key_ops
);
923 /* read a line from the input file */
924 static int read_next_line( struct file_load_info
*info
)
932 if (!fgets( info
->buffer
+ pos
, info
->len
- pos
, info
->file
))
933 return (pos
!= 0); /* EOF */
934 pos
= strlen(info
->buffer
);
935 if (info
->buffer
[pos
-1] == '\n')
937 /* got a full line */
938 info
->buffer
[--pos
] = 0;
939 if (pos
> 0 && info
->buffer
[pos
-1] == '\r') info
->buffer
[pos
-1] = 0;
942 if (pos
< info
->len
- 1) return 1; /* EOF but something was read */
944 /* need to enlarge the buffer */
945 newlen
= info
->len
+ info
->len
/ 2;
946 if (!(newbuf
= realloc( info
->buffer
, newlen
)))
948 set_error( STATUS_NO_MEMORY
);
951 info
->buffer
= newbuf
;
956 /* make sure the temp buffer holds enough space */
957 static int get_file_tmp_space( struct file_load_info
*info
, int size
)
960 if (info
->tmplen
>= size
) return 1;
961 if (!(tmp
= realloc( info
->tmp
, size
)))
963 set_error( STATUS_NO_MEMORY
);
971 /* report an error while loading an input file */
972 static void file_read_error( const char *err
, struct file_load_info
*info
)
974 fprintf( stderr
, "Line %d: %s '%s'\n", info
->line
, err
, info
->buffer
);
977 /* parse an escaped string back into Unicode */
978 /* return the number of chars read from the input, or -1 on output overflow */
979 static int parse_strW( WCHAR
*dest
, int *len
, const char *src
, char endchar
)
981 int count
= sizeof(WCHAR
); /* for terminating null */
983 while (*p
&& *p
!= endchar
)
985 if (*p
!= '\\') *dest
= (WCHAR
)*p
++;
991 case 'a': *dest
= '\a'; p
++; break;
992 case 'b': *dest
= '\b'; p
++; break;
993 case 'e': *dest
= '\e'; p
++; break;
994 case 'f': *dest
= '\f'; p
++; break;
995 case 'n': *dest
= '\n'; p
++; break;
996 case 'r': *dest
= '\r'; p
++; break;
997 case 't': *dest
= '\t'; p
++; break;
998 case 'v': *dest
= '\v'; p
++; break;
999 case 'x': /* hex escape */
1001 if (!isxdigit(*p
)) *dest
= 'x';
1004 *dest
= to_hex(*p
++);
1005 if (isxdigit(*p
)) *dest
= (*dest
* 16) + to_hex(*p
++);
1006 if (isxdigit(*p
)) *dest
= (*dest
* 16) + to_hex(*p
++);
1007 if (isxdigit(*p
)) *dest
= (*dest
* 16) + to_hex(*p
++);
1017 case '7': /* octal escape */
1019 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
1020 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
1023 *dest
= (WCHAR
)*p
++;
1027 if ((count
+= sizeof(WCHAR
)) > *len
) return -1; /* dest buffer overflow */
1031 if (!*p
) return -1; /* delimiter not found */
1036 /* convert a data type tag to a value type */
1037 static int get_data_type( const char *buffer
, int *type
, int *parse_type
)
1039 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
1041 static const struct data_type data_types
[] =
1042 { /* actual type */ /* type to assume for parsing */
1043 { "\"", 1, REG_SZ
, REG_SZ
},
1044 { "str:\"", 5, REG_SZ
, REG_SZ
},
1045 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
1046 { "str(7):\"", 8, REG_MULTI_SZ
, REG_SZ
},
1047 { "hex:", 4, REG_BINARY
, REG_BINARY
},
1048 { "dword:", 6, REG_DWORD
, REG_DWORD
},
1049 { "hex(", 4, -1, REG_BINARY
},
1053 const struct data_type
*ptr
;
1056 for (ptr
= data_types
; ptr
->tag
; ptr
++)
1058 if (memcmp( ptr
->tag
, buffer
, ptr
->len
)) continue;
1059 *parse_type
= ptr
->parse_type
;
1060 if ((*type
= ptr
->type
) != -1) return ptr
->len
;
1061 /* "hex(xx):" is special */
1062 *type
= (int)strtoul( buffer
+ 4, &end
, 16 );
1063 if ((end
<= buffer
) || memcmp( end
, "):", 2 )) return 0;
1064 return end
+ 2 - buffer
;
1069 /* load and create a key from the input file */
1070 static struct key
*load_key( struct key
*base
, const char *buffer
, struct file_load_info
*info
)
1073 int res
, len
, modif
;
1075 len
= strlen(buffer
) * sizeof(WCHAR
);
1076 if (!get_file_tmp_space( info
, len
)) return NULL
;
1078 if ((res
= parse_strW( (WCHAR
*)info
->tmp
, &len
, buffer
, ']' )) == -1)
1080 file_read_error( "Malformed key", info
);
1083 if (!sscanf( buffer
+ res
, " %d", &modif
)) modif
= time(NULL
);
1085 for (p
= (WCHAR
*)info
->tmp
; *p
; p
++) if (*p
== '\\') { p
++; break; }
1086 return create_key( base
, p
, len
- ((char *)p
- info
->tmp
), NULL
, 0, modif
, &res
);
1089 /* parse a comma-separated list of hex digits */
1090 static int parse_hex( unsigned char *dest
, int *len
, const char *buffer
)
1092 const char *p
= buffer
;
1094 while (isxdigit(*p
))
1098 memcpy( buf
, p
, 2 );
1100 sscanf( buf
, "%x", &val
);
1101 if (count
++ >= *len
) return -1; /* dest buffer overflow */
1102 *dest
++ = (unsigned char )val
;
1110 /* parse a value name and create the corresponding value */
1111 static struct key_value
*parse_value_name( struct key
*key
, const char *buffer
, int *len
,
1112 struct file_load_info
*info
)
1114 int maxlen
= strlen(buffer
) * sizeof(WCHAR
);
1115 if (!get_file_tmp_space( info
, maxlen
)) return NULL
;
1116 if (buffer
[0] == '@')
1118 info
->tmp
[0] = info
->tmp
[1] = 0;
1123 if ((*len
= parse_strW( (WCHAR
*)info
->tmp
, &maxlen
, buffer
+ 1, '\"' )) == -1) goto error
;
1124 (*len
)++; /* for initial quote */
1126 if (buffer
[*len
] != '=') goto error
;
1128 return insert_value( key
, (WCHAR
*)info
->tmp
);
1131 file_read_error( "Malformed value name", info
);
1135 /* load a value from the input file */
1136 static int load_value( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1140 int maxlen
, len
, res
;
1141 int type
, parse_type
;
1142 struct key_value
*value
;
1144 if (!(value
= parse_value_name( key
, buffer
, &len
, info
))) return 0;
1145 if (!(res
= get_data_type( buffer
+ len
, &type
, &parse_type
))) goto error
;
1146 buffer
+= len
+ res
;
1151 len
= strlen(buffer
) * sizeof(WCHAR
);
1152 if (!get_file_tmp_space( info
, len
)) return 0;
1153 if ((res
= parse_strW( (WCHAR
*)info
->tmp
, &len
, buffer
, '\"' )) == -1) goto error
;
1157 dw
= strtoul( buffer
, NULL
, 16 );
1161 case REG_BINARY
: /* hex digits */
1165 maxlen
= 1 + strlen(buffer
)/3; /* 3 chars for one hex byte */
1166 if (!get_file_tmp_space( info
, len
+ maxlen
)) return 0;
1167 if ((res
= parse_hex( info
->tmp
+ len
, &maxlen
, buffer
)) == -1) goto error
;
1170 while (isspace(*buffer
)) buffer
++;
1171 if (!*buffer
) break;
1172 if (*buffer
!= '\\') goto error
;
1173 if (read_next_line( info
) != 1) goto error
;
1174 buffer
= info
->buffer
;
1175 while (isspace(*buffer
)) buffer
++;
1181 ptr
= NULL
; /* keep compiler quiet */
1185 if (!len
) newptr
= NULL
;
1186 else if (!(newptr
= memdup( ptr
, len
))) return 0;
1188 if (value
->data
) free( value
->data
);
1189 value
->data
= newptr
;
1192 /* update the key level but not the modification time */
1193 key
->level
= max( key
->level
, current_level
);
1197 file_read_error( "Malformed value", info
);
1201 /* load all the keys from the input file */
1202 static void load_keys( struct key
*key
, FILE *f
)
1204 struct key
*subkey
= NULL
;
1205 struct file_load_info info
;
1212 if (!(info
.buffer
= mem_alloc( info
.len
))) return;
1213 if (!(info
.tmp
= mem_alloc( info
.tmplen
)))
1215 free( info
.buffer
);
1219 if ((read_next_line( &info
) != 1) ||
1220 strcmp( info
.buffer
, "WINE REGISTRY Version 2" ))
1222 set_error( STATUS_NOT_REGISTRY_FILE
);
1226 while (read_next_line( &info
) == 1)
1228 for (p
= info
.buffer
; *p
&& isspace(*p
); p
++);
1231 case '[': /* new key */
1232 if (subkey
) release_object( subkey
);
1233 subkey
= load_key( key
, p
+ 1, &info
);
1235 case '@': /* default value */
1236 case '\"': /* value */
1237 if (subkey
) load_value( subkey
, p
, &info
);
1238 else file_read_error( "Value without key", &info
);
1240 case '#': /* comment */
1241 case ';': /* comment */
1242 case 0: /* empty line */
1245 file_read_error( "Unrecognized input", &info
);
1251 if (subkey
) release_object( subkey
);
1252 free( info
.buffer
);
1256 /* load a part of the registry from a file */
1257 static void load_registry( struct key
*key
, int handle
)
1262 if (!(obj
= get_handle_obj( current
->process
, handle
, GENERIC_READ
, NULL
))) return;
1263 fd
= obj
->ops
->get_read_fd( obj
);
1264 release_object( obj
);
1267 FILE *f
= fdopen( fd
, "r" );
1270 load_keys( key
, f
);
1273 else file_set_error();
1277 /* update the level of the parents of a key (only needed for the old format) */
1278 static int update_level( struct key
*key
)
1281 int max
= key
->level
;
1282 for (i
= 0; i
<= key
->last_subkey
; i
++)
1284 int sub
= update_level( key
->subkeys
[i
] );
1285 if (sub
> max
) max
= sub
;
1291 /* dump a string to a registry save file in the old v1 format */
1292 static void save_string_v1( LPCWSTR str
, FILE *f
, int len
)
1295 while ((len
== -1) ? *str
: (*str
&& len
--))
1297 if ((*str
> 0x7f) || (*str
== '\n') || (*str
== '='))
1298 fprintf( f
, "\\u%04x", *str
);
1301 if (*str
== '\\') fputc( '\\', f
);
1302 fputc( (char)*str
, f
);
1308 /* save a registry and all its subkeys to a text file in the old v1 format */
1309 static void save_subkeys_v1( struct key
*key
, int nesting
, FILE *f
)
1313 if (key
->flags
& KEY_VOLATILE
) return;
1314 if (key
->level
< saving_level
) return;
1315 for (i
= 0; i
<= key
->last_value
; i
++)
1317 struct key_value
*value
= &key
->values
[i
];
1318 for (j
= nesting
; j
> 0; j
--) fputc( '\t', f
);
1319 save_string_v1( value
->name
, f
, -1 );
1320 fprintf( f
, "=%d,%d,", value
->type
, 0 );
1321 if (value
->type
== REG_SZ
|| value
->type
== REG_EXPAND_SZ
)
1322 save_string_v1( (LPWSTR
)value
->data
, f
, value
->len
/ 2 );
1324 for (j
= 0; j
< value
->len
; j
++)
1325 fprintf( f
, "%02x", *((unsigned char *)value
->data
+ j
) );
1328 for (i
= 0; i
<= key
->last_subkey
; i
++)
1330 for (j
= nesting
; j
> 0; j
--) fputc( '\t', f
);
1331 save_string_v1( key
->subkeys
[i
]->name
, f
, -1 );
1333 save_subkeys_v1( key
->subkeys
[i
], nesting
+ 1, f
);
1337 /* save a registry branch to a file handle */
1338 static void save_registry( struct key
*key
, int handle
)
1343 if (key
->flags
& KEY_DELETED
)
1345 set_error( STATUS_KEY_DELETED
);
1348 if (!(obj
= get_handle_obj( current
->process
, handle
, GENERIC_WRITE
, NULL
))) return;
1349 fd
= obj
->ops
->get_write_fd( obj
);
1350 release_object( obj
);
1353 FILE *f
= fdopen( fd
, "w" );
1356 fprintf( f
, "WINE REGISTRY Version %d\n", saving_version
);
1357 if (saving_version
== 2) save_subkeys( key
, key
, f
);
1360 update_level( key
);
1361 save_subkeys_v1( key
, 0, f
);
1363 if (fclose( f
)) file_set_error();
1373 /* register a key branch for being saved on exit */
1374 static void register_branch_for_saving( struct key
*key
, const char *path
, size_t len
)
1376 if (save_branch_count
>= MAX_SAVE_BRANCH_INFO
)
1378 set_error( STATUS_NO_MORE_ENTRIES
);
1381 if (!(save_branch_info
[save_branch_count
].path
= memdup( path
, len
+1 ))) return;
1382 save_branch_info
[save_branch_count
].path
[len
] = 0;
1383 save_branch_info
[save_branch_count
].key
= (struct key
*)grab_object( key
);
1384 save_branch_count
++;
1387 /* save a registry branch to a file */
1388 static int save_branch( struct key
*key
, const char *path
)
1390 char *p
, *real
, *tmp
= NULL
;
1391 int fd
, count
= 0, ret
= 0;
1394 /* get the real path */
1396 if (!(real
= malloc( PATH_MAX
))) return 0;
1397 if (!realpath( path
, real
))
1404 /* test the file type */
1406 if ((fd
= open( path
, O_WRONLY
)) != -1)
1409 /* if file is not a regular file or has multiple links,
1410 write directly into it; otherwise use a temp file */
1411 if (!fstat( fd
, &st
) && (!S_ISREG(st
.st_mode
) || st
.st_nlink
> 1))
1419 /* create a temp file in the same directory */
1421 if (!(tmp
= malloc( strlen(path
) + 20 ))) goto done
;
1422 strcpy( tmp
, path
);
1423 if ((p
= strrchr( tmp
, '/' ))) p
++;
1427 sprintf( p
, "reg%x%04x.tmp", getpid(), count
++ );
1428 if ((fd
= open( tmp
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666 )) != -1) break;
1429 if (errno
!= EEXIST
) goto done
;
1433 /* now save to it */
1436 if (!(f
= fdopen( fd
, "w" )))
1438 if (tmp
) unlink( tmp
);
1443 if (debug_level
> 1)
1445 fprintf( stderr
, "%s: ", path
);
1446 dump_operation( key
, NULL
, "saving" );
1449 fprintf( f
, "WINE REGISTRY Version %d\n", saving_version
);
1450 if (saving_version
== 2) save_subkeys( key
, key
, f
);
1453 update_level( key
);
1454 save_subkeys_v1( key
, 0, f
);
1460 /* if successfully written, rename to final name */
1461 if (ret
) ret
= !rename( tmp
, path
);
1462 if (!ret
) unlink( tmp
);
1467 if (real
) free( real
);
1471 /* periodic saving of the registry */
1472 static void periodic_save( void *arg
)
1475 for (i
= 0; i
< save_branch_count
; i
++)
1476 save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
);
1477 add_timeout( &next_save_time
, save_period
);
1478 save_timeout_user
= add_timeout_user( &next_save_time
, periodic_save
, 0 );
1481 /* save the registry and close the top-level keys; used on server exit */
1482 void close_registry(void)
1486 for (i
= 0; i
< save_branch_count
; i
++)
1488 if (!save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
))
1490 fprintf( stderr
, "wineserver: could not save registry branch to %s",
1491 save_branch_info
[i
].path
);
1494 release_object( save_branch_info
[i
].key
);
1496 for (i
= 0; i
< NB_ROOT_KEYS
; i
++)
1498 if (root_keys
[i
]) release_object( root_keys
[i
] );
1503 /* create a registry key */
1504 DECL_HANDLER(create_key
)
1506 struct key
*key
, *parent
;
1508 unsigned int access
= req
->access
;
1510 if (access
& MAXIMUM_ALLOWED
) access
= KEY_ALL_ACCESS
; /* FIXME: needs general solution */
1512 if ((parent
= get_hkey_obj( req
->parent
, 0 /*FIXME*/ )))
1514 if ((class = req_strdupW( req
, req
->class )))
1516 if ((key
= create_key( parent
, req
->name
, sizeof(req
->name
), class, req
->options
,
1517 req
->modif
, &req
->created
)))
1519 req
->hkey
= alloc_handle( current
->process
, key
, access
, 0 );
1520 release_object( key
);
1524 release_object( parent
);
1528 /* open a registry key */
1529 DECL_HANDLER(open_key
)
1531 struct key
*key
, *parent
;
1532 unsigned int access
= req
->access
;
1534 if (access
& MAXIMUM_ALLOWED
) access
= KEY_ALL_ACCESS
; /* FIXME: needs general solution */
1536 if ((parent
= get_hkey_obj( req
->parent
, 0 /*FIXME*/ )))
1538 if ((key
= open_key( parent
, req
->name
, sizeof(req
->name
) )))
1540 req
->hkey
= alloc_handle( current
->process
, key
, access
, 0 );
1541 release_object( key
);
1543 release_object( parent
);
1547 /* delete a registry key */
1548 DECL_HANDLER(delete_key
)
1552 if ((key
= get_hkey_obj( req
->hkey
, 0 /*FIXME*/ )))
1554 delete_key( key
, req
->name
, sizeof(req
->name
) );
1555 release_object( key
);
1559 /* close a registry key */
1560 DECL_HANDLER(close_key
)
1562 int hkey
= req
->hkey
;
1563 /* ignore attempts to close a root key */
1564 if (!IS_ROOT_HKEY(hkey
)) close_handle( current
->process
, hkey
);
1567 /* enumerate registry subkeys */
1568 DECL_HANDLER(enum_key
)
1572 req
->name
[0] = req
->class[0] = 0;
1573 if ((key
= get_hkey_obj( req
->hkey
, KEY_ENUMERATE_SUB_KEYS
)))
1575 enum_key( key
, req
->index
, req
->name
, req
->class, &req
->modif
);
1576 release_object( key
);
1580 /* query information about a registry key */
1581 DECL_HANDLER(query_key_info
)
1585 req
->name
[0] = req
->class[0] = 0;
1586 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
1588 query_key( key
, req
);
1589 release_object( key
);
1593 /* set a value of a registry key */
1594 DECL_HANDLER(set_key_value
)
1597 int max
= get_req_size( req
, req
->data
, sizeof(req
->data
[0]) );
1598 int datalen
= req
->len
;
1601 set_error( STATUS_NO_MEMORY
); /* FIXME */
1604 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
1606 set_value( key
, copy_path( req
->name
), req
->type
, datalen
, req
->data
);
1607 release_object( key
);
1611 /* retrieve the value of a registry key */
1612 DECL_HANDLER(get_key_value
)
1617 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
1619 get_value( key
, copy_path( req
->name
), &req
->type
, &req
->len
, req
->data
);
1620 release_object( key
);
1624 /* enumerate the value of a registry key */
1625 DECL_HANDLER(enum_key_value
)
1631 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
1633 enum_value( key
, req
->index
, req
->name
, &req
->type
, &req
->len
, req
->data
);
1634 release_object( key
);
1638 /* delete a value of a registry key */
1639 DECL_HANDLER(delete_key_value
)
1644 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
1646 if ((name
= req_strdupW( req
, req
->name
)))
1648 delete_value( key
, name
);
1651 release_object( key
);
1655 /* load a registry branch from a file */
1656 DECL_HANDLER(load_registry
)
1660 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
| KEY_CREATE_SUB_KEY
)))
1662 /* FIXME: use subkey name */
1663 load_registry( key
, req
->file
);
1664 release_object( key
);
1668 /* save a registry branch to a file */
1669 DECL_HANDLER(save_registry
)
1673 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
| KEY_ENUMERATE_SUB_KEYS
)))
1675 save_registry( key
, req
->file
);
1676 release_object( key
);
1680 /* set the current and saving level for the registry */
1681 DECL_HANDLER(set_registry_levels
)
1683 current_level
= req
->current
;
1684 saving_level
= req
->saving
;
1685 saving_version
= req
->version
;
1687 /* set periodic save timer */
1689 if (save_timeout_user
)
1691 remove_timeout_user( save_timeout_user
);
1692 save_timeout_user
= NULL
;
1694 if ((save_period
= req
->period
))
1696 if (save_period
< 10000) save_period
= 10000; /* limit rate */
1697 gettimeofday( &next_save_time
, 0 );
1698 add_timeout( &next_save_time
, save_period
);
1699 save_timeout_user
= add_timeout_user( &next_save_time
, periodic_save
, 0 );
1703 /* save a registry branch at server exit */
1704 DECL_HANDLER(save_registry_atexit
)
1708 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
| KEY_ENUMERATE_SUB_KEYS
)))
1710 register_branch_for_saving( key
, req
->file
, get_req_strlen( req
, req
->file
) );
1711 release_object( key
);