Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side registry management |
| 3 | * |
| 4 | * Copyright (C) 1999 Alexandre Julliard |
| 5 | */ |
| 6 | |
| 7 | /* To do: |
| 8 | * - behavior with deleted keys |
| 9 | * - values larger than request buffer |
| 10 | * - symbolic links |
| 11 | */ |
| 12 | |
| 13 | #include <assert.h> |
Alexandre Julliard | 7e495e1 | 2000-07-25 21:01:59 +0000 | [diff] [blame] | 14 | #include <ctype.h> |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 17 | #include <limits.h> |
| 18 | #include <stdio.h> |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 19 | #include <string.h> |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 21 | #include <sys/stat.h> |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Juergen Schmied | 788d8d5 | 2000-01-27 05:37:54 +0000 | [diff] [blame] | 23 | #include <pwd.h> |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 24 | #include "object.h" |
| 25 | #include "handle.h" |
| 26 | #include "request.h" |
| 27 | #include "unicode.h" |
| 28 | |
| 29 | #include "winbase.h" |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 30 | #include "winreg.h" |
Andreas Mohr | 32a5b63 | 2000-03-26 14:41:10 +0000 | [diff] [blame] | 31 | #include "winnt.h" /* registry definitions */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | /* a registry key */ |
| 35 | struct key |
| 36 | { |
| 37 | struct object obj; /* object header */ |
| 38 | WCHAR *name; /* key name */ |
| 39 | WCHAR *class; /* key class */ |
| 40 | struct key *parent; /* parent key */ |
| 41 | int last_subkey; /* last in use subkey */ |
| 42 | int nb_subkeys; /* count of allocated subkeys */ |
| 43 | struct key **subkeys; /* subkeys array */ |
| 44 | int last_value; /* last in use value */ |
| 45 | int nb_values; /* count of allocated values in array */ |
| 46 | struct key_value *values; /* values array */ |
| 47 | short flags; /* flags */ |
| 48 | short level; /* saving level */ |
| 49 | time_t modif; /* last modification time */ |
| 50 | }; |
| 51 | |
| 52 | /* key flags */ |
| 53 | #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */ |
| 54 | #define KEY_DELETED 0x0002 /* key has been deleted */ |
| 55 | #define KEY_ROOT 0x0004 /* key is a root key */ |
| 56 | |
| 57 | /* a key value */ |
| 58 | struct key_value |
| 59 | { |
| 60 | WCHAR *name; /* value name */ |
| 61 | int type; /* value type */ |
| 62 | size_t len; /* value data length in bytes */ |
| 63 | void *data; /* pointer to value data */ |
| 64 | }; |
| 65 | |
| 66 | #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */ |
| 67 | #define MIN_VALUES 8 /* min. number of allocated values per key */ |
| 68 | |
| 69 | |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 70 | /* the special root keys */ |
| 71 | #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT |
| 72 | #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA |
| 73 | #define NB_SPECIAL_ROOT_KEYS (HKEY_SPECIAL_ROOT_LAST - HKEY_SPECIAL_ROOT_FIRST + 1) |
| 74 | #define IS_SPECIAL_ROOT_HKEY(h) (((h) >= HKEY_SPECIAL_ROOT_FIRST) && ((h) <= HKEY_SPECIAL_ROOT_LAST)) |
| 75 | static struct key *special_root_keys[NB_SPECIAL_ROOT_KEYS]; |
| 76 | |
| 77 | /* the real root key */ |
| 78 | static struct key *root_key; |
| 79 | |
| 80 | /* the special root key names */ |
| 81 | static const char * const special_root_names[NB_SPECIAL_ROOT_KEYS] = |
| 82 | { |
| 83 | "Machine\\Software\\Classes", /* HKEY_CLASSES_ROOT */ |
| 84 | "User\\", /* we append the user name dynamically */ /* HKEY_CURRENT_USER */ |
| 85 | "Machine", /* HKEY_LOCAL_MACHINE */ |
| 86 | "User", /* HKEY_USERS */ |
| 87 | "PerfData", /* HKEY_PERFORMANCE_DATA */ |
| 88 | "Machine\\System\\CurrentControlSet\\HardwardProfiles\\Current", /* HKEY_CURRENT_CONFIG */ |
| 89 | "DynData" /* HKEY_DYN_DATA */ |
| 90 | }; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 91 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 92 | |
| 93 | /* keys saving level */ |
| 94 | /* current_level is the level that is put into all newly created or modified keys */ |
| 95 | /* saving_level is the minimum level that a key needs in order to get saved */ |
| 96 | static int current_level; |
| 97 | static int saving_level; |
| 98 | |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 99 | static struct timeval next_save_time; /* absolute time of next periodic save */ |
| 100 | static int save_period; /* delay between periodic saves (ms) */ |
| 101 | static struct timeout_user *save_timeout_user; /* saving timer */ |
| 102 | |
| 103 | /* information about where to save a registry branch */ |
| 104 | struct save_branch_info |
| 105 | { |
| 106 | struct key *key; |
| 107 | char *path; |
| 108 | }; |
| 109 | |
| 110 | #define MAX_SAVE_BRANCH_INFO 8 |
| 111 | static int save_branch_count; |
| 112 | static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO]; |
| 113 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 114 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 115 | /* information about a file being loaded */ |
| 116 | struct file_load_info |
| 117 | { |
| 118 | FILE *file; /* input file */ |
| 119 | char *buffer; /* line buffer */ |
| 120 | int len; /* buffer length */ |
| 121 | int line; /* current input line */ |
| 122 | char *tmp; /* temp buffer to use while parsing input */ |
| 123 | int tmplen; /* length of temp buffer */ |
| 124 | }; |
| 125 | |
| 126 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 127 | static void key_dump( struct object *obj, int verbose ); |
| 128 | static void key_destroy( struct object *obj ); |
| 129 | |
| 130 | static const struct object_ops key_ops = |
| 131 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 132 | sizeof(struct key), /* size */ |
| 133 | key_dump, /* dump */ |
| 134 | no_add_queue, /* add_queue */ |
| 135 | NULL, /* remove_queue */ |
| 136 | NULL, /* signaled */ |
| 137 | NULL, /* satisfied */ |
| 138 | NULL, /* get_poll_events */ |
| 139 | NULL, /* poll_event */ |
| 140 | no_read_fd, /* get_read_fd */ |
| 141 | no_write_fd, /* get_write_fd */ |
| 142 | no_flush, /* flush */ |
| 143 | no_get_file_info, /* get_file_info */ |
| 144 | key_destroy /* destroy */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | |
| 148 | /* |
| 149 | * The registry text file format v2 used by this code is similar to the one |
| 150 | * used by REGEDIT import/export functionality, with the following differences: |
| 151 | * - strings and key names can contain \x escapes for Unicode |
| 152 | * - key names use escapes too in order to support Unicode |
| 153 | * - the modification time optionally follows the key name |
| 154 | * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex |
| 155 | */ |
| 156 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 157 | static inline char to_hex( char ch ) |
| 158 | { |
| 159 | if (isdigit(ch)) return ch - '0'; |
| 160 | return tolower(ch) - 'a' + 10; |
| 161 | } |
| 162 | |
| 163 | /* dump the full path of a key */ |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 164 | static void dump_path( struct key *key, struct key *base, FILE *f ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 165 | { |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 166 | if (key->parent && key->parent != base) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 167 | { |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 168 | dump_path( key->parent, base, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 169 | fprintf( f, "\\\\" ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 170 | } |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 171 | dump_strW( key->name, strlenW(key->name), f, "[]" ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* dump a value to a text file */ |
| 175 | static void dump_value( struct key_value *value, FILE *f ) |
| 176 | { |
| 177 | int i, count; |
| 178 | |
| 179 | if (value->name[0]) |
| 180 | { |
| 181 | fputc( '\"', f ); |
| 182 | count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" ); |
| 183 | count += fprintf( f, "\"=" ); |
| 184 | } |
| 185 | else count = fprintf( f, "@=" ); |
| 186 | |
| 187 | switch(value->type) |
| 188 | { |
| 189 | case REG_SZ: |
| 190 | case REG_EXPAND_SZ: |
| 191 | case REG_MULTI_SZ: |
| 192 | if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type ); |
| 193 | fputc( '\"', f ); |
| 194 | if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" ); |
| 195 | fputc( '\"', f ); |
| 196 | break; |
| 197 | case REG_DWORD: |
| 198 | if (value->len == sizeof(DWORD)) |
| 199 | { |
| 200 | DWORD dw; |
| 201 | memcpy( &dw, value->data, sizeof(DWORD) ); |
| 202 | fprintf( f, "dword:%08lx", dw ); |
| 203 | break; |
| 204 | } |
| 205 | /* else fall through */ |
| 206 | default: |
| 207 | if (value->type == REG_BINARY) count += fprintf( f, "hex:" ); |
| 208 | else count += fprintf( f, "hex(%x):", value->type ); |
| 209 | for (i = 0; i < value->len; i++) |
| 210 | { |
| 211 | count += fprintf( f, "%02x", *((unsigned char *)value->data + i) ); |
| 212 | if (i < value->len-1) |
| 213 | { |
| 214 | fputc( ',', f ); |
| 215 | if (++count > 76) |
| 216 | { |
| 217 | fprintf( f, "\\\n " ); |
| 218 | count = 2; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | break; |
| 223 | } |
| 224 | fputc( '\n', f ); |
| 225 | } |
| 226 | |
| 227 | /* save a registry and all its subkeys to a text file */ |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 228 | static void save_subkeys( struct key *key, struct key *base, FILE *f ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 229 | { |
| 230 | int i; |
| 231 | |
| 232 | if (key->flags & KEY_VOLATILE) return; |
| 233 | /* save key if it has the proper level, and has either some values or no subkeys */ |
| 234 | /* keys with no values but subkeys are saved implicitly by saving the subkeys */ |
| 235 | if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1))) |
| 236 | { |
| 237 | fprintf( f, "\n[" ); |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 238 | if (key != base) dump_path( key, base, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 239 | fprintf( f, "] %ld\n", key->modif ); |
| 240 | for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f ); |
| 241 | } |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 242 | for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static void dump_operation( struct key *key, struct key_value *value, const char *op ) |
| 246 | { |
| 247 | fprintf( stderr, "%s key ", op ); |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 248 | if (key) dump_path( key, NULL, stderr ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 249 | else fprintf( stderr, "ERROR" ); |
| 250 | if (value) |
| 251 | { |
| 252 | fprintf( stderr, " value "); |
| 253 | dump_value( value, stderr ); |
| 254 | } |
| 255 | else fprintf( stderr, "\n" ); |
| 256 | } |
| 257 | |
| 258 | static void key_dump( struct object *obj, int verbose ) |
| 259 | { |
| 260 | struct key *key = (struct key *)obj; |
| 261 | assert( obj->ops == &key_ops ); |
| 262 | fprintf( stderr, "Key flags=%x ", key->flags ); |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 263 | dump_path( key, NULL, stderr ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 264 | fprintf( stderr, "\n" ); |
| 265 | } |
| 266 | |
| 267 | static void key_destroy( struct object *obj ) |
| 268 | { |
| 269 | int i; |
| 270 | struct key *key = (struct key *)obj; |
| 271 | assert( obj->ops == &key_ops ); |
| 272 | |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 273 | if (key->name) free( key->name ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 274 | if (key->class) free( key->class ); |
| 275 | for (i = 0; i <= key->last_value; i++) |
| 276 | { |
| 277 | free( key->values[i].name ); |
| 278 | if (key->values[i].data) free( key->values[i].data ); |
| 279 | } |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 280 | for (i = 0; i <= key->last_subkey; i++) |
| 281 | { |
| 282 | key->subkeys[i]->parent = NULL; |
| 283 | release_object( key->subkeys[i] ); |
| 284 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* duplicate a key path from the request buffer */ |
| 288 | /* returns a pointer to a static buffer, so only useable once per request */ |
| 289 | static WCHAR *copy_path( const path_t path ) |
| 290 | { |
| 291 | static WCHAR buffer[MAX_PATH+1]; |
| 292 | WCHAR *p = buffer; |
| 293 | |
| 294 | while (p < buffer + sizeof(buffer) - 1) if (!(*p++ = *path++)) break; |
| 295 | *p = 0; |
| 296 | return buffer; |
| 297 | } |
| 298 | |
| 299 | /* return the next token in a given path */ |
| 300 | /* returns a pointer to a static buffer, so only useable once per request */ |
| 301 | static WCHAR *get_path_token( const WCHAR *initpath, size_t maxlen ) |
| 302 | { |
| 303 | static const WCHAR *path; |
| 304 | static const WCHAR *end; |
| 305 | static WCHAR buffer[MAX_PATH+1]; |
| 306 | WCHAR *p = buffer; |
| 307 | |
| 308 | if (initpath) |
| 309 | { |
| 310 | path = initpath; |
| 311 | end = path + maxlen / sizeof(WCHAR); |
| 312 | } |
| 313 | while ((path < end) && (*path == '\\')) path++; |
| 314 | while ((path < end) && (p < buffer + sizeof(buffer) - 1)) |
| 315 | { |
| 316 | WCHAR ch = *path; |
| 317 | if (!ch || (ch == '\\')) break; |
| 318 | *p++ = ch; |
| 319 | path++; |
| 320 | } |
| 321 | *p = 0; |
| 322 | return buffer; |
| 323 | } |
| 324 | |
| 325 | /* duplicate a Unicode string from the request buffer */ |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 326 | static WCHAR *req_strdupW( const void *req, const WCHAR *str ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 327 | { |
| 328 | WCHAR *name; |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 329 | size_t len = get_req_strlenW( req, str ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 330 | if ((name = mem_alloc( (len + 1) * sizeof(WCHAR) )) != NULL) |
| 331 | { |
| 332 | memcpy( name, str, len * sizeof(WCHAR) ); |
| 333 | name[len] = 0; |
| 334 | } |
| 335 | return name; |
| 336 | } |
| 337 | |
| 338 | /* allocate a key object */ |
| 339 | static struct key *alloc_key( const WCHAR *name, time_t modif ) |
| 340 | { |
| 341 | struct key *key; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 342 | if ((key = (struct key *)alloc_object( &key_ops, -1 ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 343 | { |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 344 | key->class = NULL; |
| 345 | key->flags = 0; |
| 346 | key->last_subkey = -1; |
| 347 | key->nb_subkeys = 0; |
| 348 | key->subkeys = NULL; |
| 349 | key->nb_values = 0; |
| 350 | key->last_value = -1; |
| 351 | key->values = NULL; |
| 352 | key->level = current_level; |
| 353 | key->modif = modif; |
| 354 | key->parent = NULL; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 355 | if (!(key->name = strdupW( name ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 356 | { |
| 357 | release_object( key ); |
| 358 | key = NULL; |
| 359 | } |
| 360 | } |
| 361 | return key; |
| 362 | } |
| 363 | |
| 364 | /* update key modification time */ |
| 365 | static void touch_key( struct key *key ) |
| 366 | { |
| 367 | key->modif = time(NULL); |
Francois Gouget | 6d77d3a | 2000-03-25 21:44:35 +0000 | [diff] [blame] | 368 | key->level = max( key->level, current_level ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* try to grow the array of subkeys; return 1 if OK, 0 on error */ |
| 372 | static int grow_subkeys( struct key *key ) |
| 373 | { |
| 374 | struct key **new_subkeys; |
| 375 | int nb_subkeys; |
| 376 | |
| 377 | if (key->nb_subkeys) |
| 378 | { |
| 379 | nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */ |
| 380 | if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) |
| 381 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 382 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | nb_subkeys = MIN_VALUES; |
| 389 | if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0; |
| 390 | } |
| 391 | key->subkeys = new_subkeys; |
| 392 | key->nb_subkeys = nb_subkeys; |
| 393 | return 1; |
| 394 | } |
| 395 | |
| 396 | /* allocate a subkey for a given key, and return its index */ |
| 397 | static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif ) |
| 398 | { |
| 399 | struct key *key; |
| 400 | int i; |
| 401 | |
| 402 | if (parent->last_subkey + 1 == parent->nb_subkeys) |
| 403 | { |
| 404 | /* need to grow the array */ |
| 405 | if (!grow_subkeys( parent )) return NULL; |
| 406 | } |
| 407 | if ((key = alloc_key( name, modif )) != NULL) |
| 408 | { |
| 409 | key->parent = parent; |
| 410 | for (i = ++parent->last_subkey; i > index; i--) |
| 411 | parent->subkeys[i] = parent->subkeys[i-1]; |
| 412 | parent->subkeys[index] = key; |
| 413 | } |
| 414 | return key; |
| 415 | } |
| 416 | |
| 417 | /* free a subkey of a given key */ |
| 418 | static void free_subkey( struct key *parent, int index ) |
| 419 | { |
| 420 | struct key *key; |
| 421 | int i, nb_subkeys; |
| 422 | |
| 423 | assert( index >= 0 ); |
| 424 | assert( index <= parent->last_subkey ); |
| 425 | |
| 426 | key = parent->subkeys[index]; |
| 427 | for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1]; |
| 428 | parent->last_subkey--; |
| 429 | key->flags |= KEY_DELETED; |
| 430 | key->parent = NULL; |
| 431 | release_object( key ); |
| 432 | |
| 433 | /* try to shrink the array */ |
| 434 | nb_subkeys = key->nb_subkeys; |
| 435 | if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2) |
| 436 | { |
| 437 | struct key **new_subkeys; |
| 438 | nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */ |
| 439 | if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS; |
| 440 | if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return; |
| 441 | key->subkeys = new_subkeys; |
| 442 | key->nb_subkeys = nb_subkeys; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* find the named child of a given key and return its index */ |
| 447 | static struct key *find_subkey( struct key *key, const WCHAR *name, int *index ) |
| 448 | { |
| 449 | int i, min, max, res; |
| 450 | |
| 451 | min = 0; |
| 452 | max = key->last_subkey; |
| 453 | while (min <= max) |
| 454 | { |
| 455 | i = (min + max) / 2; |
| 456 | if (!(res = strcmpiW( key->subkeys[i]->name, name ))) |
| 457 | { |
| 458 | *index = i; |
| 459 | return key->subkeys[i]; |
| 460 | } |
| 461 | if (res > 0) max = i - 1; |
| 462 | else min = i + 1; |
| 463 | } |
| 464 | *index = min; /* this is where we should insert it */ |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | /* open a subkey */ |
| 469 | static struct key *open_key( struct key *key, const WCHAR *name, size_t maxlen ) |
| 470 | { |
| 471 | int index; |
| 472 | WCHAR *path; |
| 473 | |
| 474 | path = get_path_token( name, maxlen ); |
| 475 | while (*path) |
| 476 | { |
| 477 | if (!(key = find_subkey( key, path, &index ))) |
| 478 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 479 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 480 | break; |
| 481 | } |
| 482 | path = get_path_token( NULL, 0 ); |
| 483 | } |
| 484 | |
| 485 | if (debug_level > 1) dump_operation( key, NULL, "Open" ); |
| 486 | if (key) grab_object( key ); |
| 487 | return key; |
| 488 | } |
| 489 | |
| 490 | /* create a subkey */ |
| 491 | static struct key *create_key( struct key *key, const WCHAR *name, size_t maxlen, WCHAR *class, |
| 492 | unsigned int options, time_t modif, int *created ) |
| 493 | { |
| 494 | struct key *base; |
| 495 | int base_idx, index, flags = 0; |
| 496 | WCHAR *path; |
| 497 | |
| 498 | if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */ |
| 499 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 500 | set_error( STATUS_KEY_DELETED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 501 | return NULL; |
| 502 | } |
| 503 | if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE; |
| 504 | else if (key->flags & KEY_VOLATILE) |
| 505 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 506 | set_error( STATUS_CHILD_MUST_BE_VOLATILE ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 507 | return NULL; |
| 508 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 509 | if (!modif) modif = time(NULL); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 510 | |
| 511 | path = get_path_token( name, maxlen ); |
| 512 | *created = 0; |
| 513 | while (*path) |
| 514 | { |
| 515 | struct key *subkey; |
| 516 | if (!(subkey = find_subkey( key, path, &index ))) break; |
| 517 | key = subkey; |
| 518 | path = get_path_token( NULL, 0 ); |
| 519 | } |
| 520 | |
| 521 | /* create the remaining part */ |
| 522 | |
| 523 | if (!*path) goto done; |
| 524 | *created = 1; |
| 525 | base = key; |
| 526 | base_idx = index; |
| 527 | key = alloc_subkey( key, path, index, modif ); |
| 528 | while (key) |
| 529 | { |
| 530 | key->flags |= flags; |
| 531 | path = get_path_token( NULL, 0 ); |
| 532 | if (!*path) goto done; |
| 533 | /* we know the index is always 0 in a new key */ |
| 534 | key = alloc_subkey( key, path, 0, modif ); |
| 535 | } |
| 536 | if (base_idx != -1) free_subkey( base, base_idx ); |
| 537 | return NULL; |
| 538 | |
| 539 | done: |
| 540 | if (debug_level > 1) dump_operation( key, NULL, "Create" ); |
| 541 | if (class) key->class = strdupW(class); |
| 542 | grab_object( key ); |
| 543 | return key; |
| 544 | } |
| 545 | |
| 546 | /* find a subkey of a given key by its index */ |
| 547 | static void enum_key( struct key *parent, int index, WCHAR *name, WCHAR *class, time_t *modif ) |
| 548 | { |
| 549 | struct key *key; |
| 550 | |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 551 | if ((index < 0) || (index > parent->last_subkey)) set_error( STATUS_NO_MORE_ENTRIES ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 552 | else |
| 553 | { |
| 554 | key = parent->subkeys[index]; |
| 555 | *modif = key->modif; |
| 556 | strcpyW( name, key->name ); |
| 557 | if (key->class) strcpyW( class, key->class ); /* FIXME: length */ |
| 558 | else *class = 0; |
| 559 | if (debug_level > 1) dump_operation( key, NULL, "Enum" ); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | /* query information about a key */ |
| 564 | static void query_key( struct key *key, struct query_key_info_request *req ) |
| 565 | { |
| 566 | int i, len; |
| 567 | int max_subkey = 0, max_class = 0; |
| 568 | int max_value = 0, max_data = 0; |
| 569 | |
Ulrich Czekalla | 6f676cd | 1999-12-10 03:43:07 +0000 | [diff] [blame] | 570 | for (i = 0; i <= key->last_subkey; i++) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 571 | { |
| 572 | struct key *subkey = key->subkeys[i]; |
| 573 | len = strlenW( subkey->name ); |
| 574 | if (len > max_subkey) max_subkey = len; |
| 575 | if (!subkey->class) continue; |
| 576 | len = strlenW( subkey->class ); |
| 577 | if (len > max_class) max_class = len; |
| 578 | } |
Ulrich Czekalla | 6f676cd | 1999-12-10 03:43:07 +0000 | [diff] [blame] | 579 | for (i = 0; i <= key->last_value; i++) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 580 | { |
| 581 | len = strlenW( key->values[i].name ); |
| 582 | if (len > max_value) max_value = len; |
| 583 | len = key->values[i].len; |
| 584 | if (len > max_data) max_data = len; |
| 585 | } |
| 586 | req->subkeys = key->last_subkey + 1; |
| 587 | req->max_subkey = max_subkey; |
| 588 | req->max_class = max_class; |
| 589 | req->values = key->last_value + 1; |
| 590 | req->max_value = max_value; |
| 591 | req->max_data = max_data; |
| 592 | req->modif = key->modif; |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 593 | strcpyW( req->name, key->name); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 594 | if (key->class) strcpyW( req->class, key->class ); /* FIXME: length */ |
| 595 | else req->class[0] = 0; |
| 596 | if (debug_level > 1) dump_operation( key, NULL, "Query" ); |
| 597 | } |
| 598 | |
| 599 | /* delete a key and its values */ |
| 600 | static void delete_key( struct key *key, const WCHAR *name, size_t maxlen ) |
| 601 | { |
| 602 | int index; |
| 603 | struct key *parent; |
| 604 | WCHAR *path; |
| 605 | |
| 606 | path = get_path_token( name, maxlen ); |
| 607 | if (!*path) |
| 608 | { |
| 609 | /* deleting this key, must find parent and index */ |
| 610 | if (key->flags & KEY_ROOT) |
| 611 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 612 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 613 | return; |
| 614 | } |
| 615 | if (!(parent = key->parent) || (key->flags & KEY_DELETED)) |
| 616 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 617 | set_error( STATUS_KEY_DELETED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 618 | return; |
| 619 | } |
| 620 | for (index = 0; index <= parent->last_subkey; index++) |
| 621 | if (parent->subkeys[index] == key) break; |
| 622 | assert( index <= parent->last_subkey ); |
| 623 | } |
| 624 | else while (*path) |
| 625 | { |
| 626 | parent = key; |
| 627 | if (!(key = find_subkey( parent, path, &index ))) |
| 628 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 629 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 630 | return; |
| 631 | } |
| 632 | path = get_path_token( NULL, 0 ); |
| 633 | } |
| 634 | |
| 635 | /* we can only delete a key that has no subkeys (FIXME) */ |
| 636 | if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0)) |
| 637 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 638 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | if (debug_level > 1) dump_operation( key, NULL, "Delete" ); |
| 642 | free_subkey( parent, index ); |
| 643 | touch_key( parent ); |
| 644 | } |
| 645 | |
| 646 | /* try to grow the array of values; return 1 if OK, 0 on error */ |
| 647 | static int grow_values( struct key *key ) |
| 648 | { |
| 649 | struct key_value *new_val; |
| 650 | int nb_values; |
| 651 | |
| 652 | if (key->nb_values) |
| 653 | { |
| 654 | nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */ |
| 655 | if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) |
| 656 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 657 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | } |
| 661 | else |
| 662 | { |
| 663 | nb_values = MIN_VALUES; |
| 664 | if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0; |
| 665 | } |
| 666 | key->values = new_val; |
| 667 | key->nb_values = nb_values; |
| 668 | return 1; |
| 669 | } |
| 670 | |
| 671 | /* find the named value of a given key and return its index in the array */ |
| 672 | static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index ) |
| 673 | { |
| 674 | int i, min, max, res; |
| 675 | |
| 676 | min = 0; |
| 677 | max = key->last_value; |
| 678 | while (min <= max) |
| 679 | { |
| 680 | i = (min + max) / 2; |
| 681 | if (!(res = strcmpiW( key->values[i].name, name ))) |
| 682 | { |
| 683 | *index = i; |
| 684 | return &key->values[i]; |
| 685 | } |
| 686 | if (res > 0) max = i - 1; |
| 687 | else min = i + 1; |
| 688 | } |
| 689 | *index = min; /* this is where we should insert it */ |
| 690 | return NULL; |
| 691 | } |
| 692 | |
| 693 | /* insert a new value or return a pointer to an existing one */ |
| 694 | static struct key_value *insert_value( struct key *key, const WCHAR *name ) |
| 695 | { |
| 696 | struct key_value *value; |
| 697 | WCHAR *new_name; |
| 698 | int i, index; |
| 699 | |
| 700 | if (!(value = find_value( key, name, &index ))) |
| 701 | { |
| 702 | /* not found, add it */ |
| 703 | if (key->last_value + 1 == key->nb_values) |
| 704 | { |
| 705 | if (!grow_values( key )) return NULL; |
| 706 | } |
| 707 | if (!(new_name = strdupW(name))) return NULL; |
| 708 | for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1]; |
| 709 | value = &key->values[index]; |
| 710 | value->name = new_name; |
| 711 | value->len = 0; |
| 712 | value->data = NULL; |
| 713 | } |
| 714 | return value; |
| 715 | } |
| 716 | |
| 717 | /* set a key value */ |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 718 | static void set_value( struct key *key, WCHAR *name, int type, unsigned int total_len, |
| 719 | unsigned int offset, unsigned int data_len, void *data ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 720 | { |
| 721 | struct key_value *value; |
| 722 | void *ptr = NULL; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 723 | |
| 724 | if (data_len + offset > total_len) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 725 | { |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 726 | set_error( STATUS_INVALID_PARAMETER ); |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | if (offset) /* adding data to an existing value */ |
| 731 | { |
| 732 | int index; |
| 733 | if (!(value = find_value( key, name, &index ))) |
| 734 | { |
| 735 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
| 736 | return; |
| 737 | } |
| 738 | if (value->len != total_len) |
| 739 | { |
| 740 | set_error( STATUS_INVALID_PARAMETER ); |
| 741 | return; |
| 742 | } |
| 743 | memcpy( (char *)value->data + offset, data, data_len ); |
| 744 | if (debug_level > 1) dump_operation( key, value, "Set" ); |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | /* first copy the data */ |
| 749 | if (total_len) |
| 750 | { |
| 751 | if (!(ptr = mem_alloc( total_len ))) return; |
| 752 | memcpy( ptr, data, data_len ); |
| 753 | if (data_len < total_len) memset( (char *)ptr + data_len, 0, total_len - data_len ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | if (!(value = insert_value( key, name ))) |
| 757 | { |
| 758 | if (ptr) free( ptr ); |
| 759 | return; |
| 760 | } |
| 761 | if (value->data) free( value->data ); /* already existing, free previous data */ |
| 762 | value->type = type; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 763 | value->len = total_len; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 764 | value->data = ptr; |
| 765 | touch_key( key ); |
| 766 | if (debug_level > 1) dump_operation( key, value, "Set" ); |
| 767 | } |
| 768 | |
| 769 | /* get a key value */ |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 770 | static void get_value( struct key *key, WCHAR *name, unsigned int offset, |
| 771 | unsigned int maxlen, int *type, int *len, void *data ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 772 | { |
| 773 | struct key_value *value; |
| 774 | int index; |
| 775 | |
| 776 | if ((value = find_value( key, name, &index ))) |
| 777 | { |
| 778 | *type = value->type; |
| 779 | *len = value->len; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 780 | if (value->data && offset < value->len) |
| 781 | { |
| 782 | if (maxlen > value->len - offset) maxlen = value->len - offset; |
| 783 | memcpy( data, (char *)value->data + offset, maxlen ); |
| 784 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 785 | if (debug_level > 1) dump_operation( key, value, "Get" ); |
| 786 | } |
| 787 | else |
| 788 | { |
| 789 | *type = -1; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 790 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
| 794 | /* enumerate a key value */ |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 795 | static void enum_value( struct key *key, int i, WCHAR *name, unsigned int offset, |
| 796 | unsigned int maxlen, int *type, int *len, void *data ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 797 | { |
| 798 | struct key_value *value; |
| 799 | |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 800 | if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 801 | else |
| 802 | { |
| 803 | value = &key->values[i]; |
| 804 | strcpyW( name, value->name ); |
| 805 | *type = value->type; |
| 806 | *len = value->len; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 807 | if (value->data && offset < value->len) |
| 808 | { |
| 809 | if (maxlen > value->len - offset) maxlen = value->len - offset; |
| 810 | memcpy( data, (char *)value->data + offset, maxlen ); |
| 811 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 812 | if (debug_level > 1) dump_operation( key, value, "Enum" ); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | /* delete a value */ |
| 817 | static void delete_value( struct key *key, const WCHAR *name ) |
| 818 | { |
| 819 | struct key_value *value; |
| 820 | int i, index, nb_values; |
| 821 | |
| 822 | if (!(value = find_value( key, name, &index ))) |
| 823 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 824 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 825 | return; |
| 826 | } |
| 827 | if (debug_level > 1) dump_operation( key, value, "Delete" ); |
| 828 | free( value->name ); |
| 829 | if (value->data) free( value->data ); |
| 830 | for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1]; |
| 831 | key->last_value--; |
| 832 | touch_key( key ); |
| 833 | |
| 834 | /* try to shrink the array */ |
| 835 | nb_values = key->nb_values; |
| 836 | if (nb_values > MIN_VALUES && key->last_value < nb_values / 2) |
| 837 | { |
| 838 | struct key_value *new_val; |
| 839 | nb_values -= nb_values / 3; /* shrink by 33% */ |
| 840 | if (nb_values < MIN_VALUES) nb_values = MIN_VALUES; |
| 841 | if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return; |
| 842 | key->values = new_val; |
| 843 | key->nb_values = nb_values; |
| 844 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 845 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 846 | |
| 847 | static struct key *create_root_key( int hkey ) |
| 848 | { |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 849 | WCHAR keyname[80]; |
| 850 | int i, dummy; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 851 | struct key *key; |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 852 | const char *p; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 853 | |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 854 | p = special_root_names[hkey - HKEY_SPECIAL_ROOT_FIRST]; |
| 855 | i = 0; |
| 856 | while (*p) keyname[i++] = *p++; |
| 857 | |
| 858 | if (hkey == HKEY_CURRENT_USER) /* this one is special */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 859 | { |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 860 | /* get the current user name */ |
| 861 | char buffer[10]; |
| 862 | struct passwd *pwd = getpwuid( getuid() ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 863 | |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 864 | if (pwd) p = pwd->pw_name; |
| 865 | else |
| 866 | { |
| 867 | sprintf( buffer, "%ld", (long) getuid() ); |
| 868 | p = buffer; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 869 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 870 | while (*p && i < sizeof(keyname)/sizeof(WCHAR)-1) keyname[i++] = *p++; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 871 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 872 | keyname[i++] = 0; |
| 873 | |
| 874 | if ((key = create_key( root_key, keyname, i*sizeof(WCHAR), NULL, 0, time(NULL), &dummy ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 875 | { |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 876 | special_root_keys[hkey - HKEY_SPECIAL_ROOT_FIRST] = key; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 877 | key->flags |= KEY_ROOT; |
| 878 | } |
| 879 | return key; |
| 880 | } |
| 881 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 882 | /* get the registry key corresponding to an hkey handle */ |
| 883 | static struct key *get_hkey_obj( int hkey, unsigned int access ) |
| 884 | { |
| 885 | struct key *key; |
| 886 | |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 887 | if (!hkey) return (struct key *)grab_object( root_key ); |
| 888 | if (IS_SPECIAL_ROOT_HKEY(hkey)) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 889 | { |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 890 | if (!(key = special_root_keys[hkey - HKEY_SPECIAL_ROOT_FIRST])) |
| 891 | key = create_root_key( hkey ); |
| 892 | else |
| 893 | grab_object( key ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 894 | } |
| 895 | else |
| 896 | key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops ); |
| 897 | return key; |
| 898 | } |
| 899 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 900 | /* read a line from the input file */ |
| 901 | static int read_next_line( struct file_load_info *info ) |
| 902 | { |
| 903 | char *newbuf; |
| 904 | int newlen, pos = 0; |
| 905 | |
| 906 | info->line++; |
| 907 | for (;;) |
| 908 | { |
| 909 | if (!fgets( info->buffer + pos, info->len - pos, info->file )) |
| 910 | return (pos != 0); /* EOF */ |
| 911 | pos = strlen(info->buffer); |
| 912 | if (info->buffer[pos-1] == '\n') |
| 913 | { |
| 914 | /* got a full line */ |
| 915 | info->buffer[--pos] = 0; |
| 916 | if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0; |
| 917 | return 1; |
| 918 | } |
| 919 | if (pos < info->len - 1) return 1; /* EOF but something was read */ |
| 920 | |
| 921 | /* need to enlarge the buffer */ |
| 922 | newlen = info->len + info->len / 2; |
| 923 | if (!(newbuf = realloc( info->buffer, newlen ))) |
| 924 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 925 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 926 | return -1; |
| 927 | } |
| 928 | info->buffer = newbuf; |
| 929 | info->len = newlen; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | /* make sure the temp buffer holds enough space */ |
| 934 | static int get_file_tmp_space( struct file_load_info *info, int size ) |
| 935 | { |
| 936 | char *tmp; |
| 937 | if (info->tmplen >= size) return 1; |
| 938 | if (!(tmp = realloc( info->tmp, size ))) |
| 939 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 940 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 941 | return 0; |
| 942 | } |
| 943 | info->tmp = tmp; |
| 944 | info->tmplen = size; |
| 945 | return 1; |
| 946 | } |
| 947 | |
| 948 | /* report an error while loading an input file */ |
| 949 | static void file_read_error( const char *err, struct file_load_info *info ) |
| 950 | { |
| 951 | fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer ); |
| 952 | } |
| 953 | |
| 954 | /* parse an escaped string back into Unicode */ |
| 955 | /* return the number of chars read from the input, or -1 on output overflow */ |
| 956 | static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar ) |
| 957 | { |
| 958 | int count = sizeof(WCHAR); /* for terminating null */ |
| 959 | const char *p = src; |
| 960 | while (*p && *p != endchar) |
| 961 | { |
| 962 | if (*p != '\\') *dest = (WCHAR)*p++; |
| 963 | else |
| 964 | { |
| 965 | p++; |
| 966 | switch(*p) |
| 967 | { |
| 968 | case 'a': *dest = '\a'; p++; break; |
| 969 | case 'b': *dest = '\b'; p++; break; |
| 970 | case 'e': *dest = '\e'; p++; break; |
| 971 | case 'f': *dest = '\f'; p++; break; |
| 972 | case 'n': *dest = '\n'; p++; break; |
| 973 | case 'r': *dest = '\r'; p++; break; |
| 974 | case 't': *dest = '\t'; p++; break; |
| 975 | case 'v': *dest = '\v'; p++; break; |
| 976 | case 'x': /* hex escape */ |
| 977 | p++; |
| 978 | if (!isxdigit(*p)) *dest = 'x'; |
| 979 | else |
| 980 | { |
| 981 | *dest = to_hex(*p++); |
| 982 | if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++); |
| 983 | if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++); |
| 984 | if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++); |
| 985 | } |
| 986 | break; |
| 987 | case '0': |
| 988 | case '1': |
| 989 | case '2': |
| 990 | case '3': |
| 991 | case '4': |
| 992 | case '5': |
| 993 | case '6': |
| 994 | case '7': /* octal escape */ |
| 995 | *dest = *p++ - '0'; |
| 996 | if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0'); |
| 997 | if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0'); |
| 998 | break; |
| 999 | default: |
| 1000 | *dest = (WCHAR)*p++; |
| 1001 | break; |
| 1002 | } |
| 1003 | } |
| 1004 | if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */ |
| 1005 | dest++; |
| 1006 | } |
| 1007 | *dest = 0; |
| 1008 | if (!*p) return -1; /* delimiter not found */ |
| 1009 | *len = count; |
| 1010 | return p + 1 - src; |
| 1011 | } |
| 1012 | |
| 1013 | /* convert a data type tag to a value type */ |
| 1014 | static int get_data_type( const char *buffer, int *type, int *parse_type ) |
| 1015 | { |
| 1016 | struct data_type { const char *tag; int len; int type; int parse_type; }; |
| 1017 | |
| 1018 | static const struct data_type data_types[] = |
| 1019 | { /* actual type */ /* type to assume for parsing */ |
| 1020 | { "\"", 1, REG_SZ, REG_SZ }, |
| 1021 | { "str:\"", 5, REG_SZ, REG_SZ }, |
| 1022 | { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ }, |
| 1023 | { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ }, |
| 1024 | { "hex:", 4, REG_BINARY, REG_BINARY }, |
| 1025 | { "dword:", 6, REG_DWORD, REG_DWORD }, |
| 1026 | { "hex(", 4, -1, REG_BINARY }, |
| 1027 | { NULL, } |
| 1028 | }; |
| 1029 | |
| 1030 | const struct data_type *ptr; |
| 1031 | char *end; |
| 1032 | |
| 1033 | for (ptr = data_types; ptr->tag; ptr++) |
| 1034 | { |
| 1035 | if (memcmp( ptr->tag, buffer, ptr->len )) continue; |
| 1036 | *parse_type = ptr->parse_type; |
| 1037 | if ((*type = ptr->type) != -1) return ptr->len; |
| 1038 | /* "hex(xx):" is special */ |
| 1039 | *type = (int)strtoul( buffer + 4, &end, 16 ); |
| 1040 | if ((end <= buffer) || memcmp( end, "):", 2 )) return 0; |
| 1041 | return end + 2 - buffer; |
| 1042 | } |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | /* load and create a key from the input file */ |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1047 | static struct key *load_key( struct key *base, const char *buffer, unsigned int options, |
| 1048 | int prefix_len, struct file_load_info *info ) |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1049 | { |
| 1050 | WCHAR *p; |
| 1051 | int res, len, modif; |
| 1052 | |
| 1053 | len = strlen(buffer) * sizeof(WCHAR); |
| 1054 | if (!get_file_tmp_space( info, len )) return NULL; |
| 1055 | |
| 1056 | if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1) |
| 1057 | { |
| 1058 | file_read_error( "Malformed key", info ); |
| 1059 | return NULL; |
| 1060 | } |
| 1061 | if (!sscanf( buffer + res, " %d", &modif )) modif = time(NULL); |
| 1062 | |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1063 | p = (WCHAR *)info->tmp; |
| 1064 | while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; } |
| 1065 | |
| 1066 | if (!*p) |
| 1067 | { |
| 1068 | if (prefix_len > 1) |
| 1069 | { |
| 1070 | file_read_error( "Malformed key", info ); |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | /* empty key name, return base key */ |
| 1074 | return (struct key *)grab_object( base ); |
| 1075 | } |
| 1076 | return create_key( base, p, len - ((char *)p - info->tmp), NULL, options, modif, &res ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* parse a comma-separated list of hex digits */ |
| 1080 | static int parse_hex( unsigned char *dest, int *len, const char *buffer ) |
| 1081 | { |
| 1082 | const char *p = buffer; |
| 1083 | int count = 0; |
| 1084 | while (isxdigit(*p)) |
| 1085 | { |
| 1086 | int val; |
| 1087 | char buf[3]; |
| 1088 | memcpy( buf, p, 2 ); |
| 1089 | buf[2] = 0; |
| 1090 | sscanf( buf, "%x", &val ); |
| 1091 | if (count++ >= *len) return -1; /* dest buffer overflow */ |
| 1092 | *dest++ = (unsigned char )val; |
| 1093 | p += 2; |
| 1094 | if (*p == ',') p++; |
| 1095 | } |
| 1096 | *len = count; |
| 1097 | return p - buffer; |
| 1098 | } |
| 1099 | |
| 1100 | /* parse a value name and create the corresponding value */ |
| 1101 | static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len, |
| 1102 | struct file_load_info *info ) |
| 1103 | { |
| 1104 | int maxlen = strlen(buffer) * sizeof(WCHAR); |
| 1105 | if (!get_file_tmp_space( info, maxlen )) return NULL; |
| 1106 | if (buffer[0] == '@') |
| 1107 | { |
| 1108 | info->tmp[0] = info->tmp[1] = 0; |
| 1109 | *len = 1; |
| 1110 | } |
| 1111 | else |
| 1112 | { |
| 1113 | if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error; |
| 1114 | (*len)++; /* for initial quote */ |
| 1115 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 1116 | while (isspace(buffer[*len])) (*len)++; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1117 | if (buffer[*len] != '=') goto error; |
| 1118 | (*len)++; |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 1119 | while (isspace(buffer[*len])) (*len)++; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1120 | return insert_value( key, (WCHAR *)info->tmp ); |
| 1121 | |
| 1122 | error: |
| 1123 | file_read_error( "Malformed value name", info ); |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | |
| 1127 | /* load a value from the input file */ |
| 1128 | static int load_value( struct key *key, const char *buffer, struct file_load_info *info ) |
| 1129 | { |
| 1130 | DWORD dw; |
| 1131 | void *ptr, *newptr; |
| 1132 | int maxlen, len, res; |
| 1133 | int type, parse_type; |
| 1134 | struct key_value *value; |
| 1135 | |
| 1136 | if (!(value = parse_value_name( key, buffer, &len, info ))) return 0; |
| 1137 | if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error; |
| 1138 | buffer += len + res; |
| 1139 | |
| 1140 | switch(parse_type) |
| 1141 | { |
| 1142 | case REG_SZ: |
| 1143 | len = strlen(buffer) * sizeof(WCHAR); |
| 1144 | if (!get_file_tmp_space( info, len )) return 0; |
| 1145 | if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error; |
| 1146 | ptr = info->tmp; |
| 1147 | break; |
| 1148 | case REG_DWORD: |
| 1149 | dw = strtoul( buffer, NULL, 16 ); |
| 1150 | ptr = &dw; |
| 1151 | len = sizeof(dw); |
| 1152 | break; |
| 1153 | case REG_BINARY: /* hex digits */ |
| 1154 | len = 0; |
| 1155 | for (;;) |
| 1156 | { |
| 1157 | maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */ |
| 1158 | if (!get_file_tmp_space( info, len + maxlen )) return 0; |
| 1159 | if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error; |
| 1160 | len += maxlen; |
| 1161 | buffer += res; |
| 1162 | while (isspace(*buffer)) buffer++; |
| 1163 | if (!*buffer) break; |
| 1164 | if (*buffer != '\\') goto error; |
| 1165 | if (read_next_line( info) != 1) goto error; |
| 1166 | buffer = info->buffer; |
| 1167 | while (isspace(*buffer)) buffer++; |
| 1168 | } |
| 1169 | ptr = info->tmp; |
| 1170 | break; |
| 1171 | default: |
| 1172 | assert(0); |
| 1173 | ptr = NULL; /* keep compiler quiet */ |
| 1174 | break; |
| 1175 | } |
| 1176 | |
| 1177 | if (!len) newptr = NULL; |
| 1178 | else if (!(newptr = memdup( ptr, len ))) return 0; |
| 1179 | |
| 1180 | if (value->data) free( value->data ); |
| 1181 | value->data = newptr; |
| 1182 | value->len = len; |
| 1183 | value->type = type; |
| 1184 | /* update the key level but not the modification time */ |
Francois Gouget | 6d77d3a | 2000-03-25 21:44:35 +0000 | [diff] [blame] | 1185 | key->level = max( key->level, current_level ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1186 | return 1; |
| 1187 | |
| 1188 | error: |
| 1189 | file_read_error( "Malformed value", info ); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1193 | /* return the length (in path elements) of name that is part of the key name */ |
| 1194 | /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */ |
| 1195 | static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info ) |
| 1196 | { |
| 1197 | WCHAR *p; |
| 1198 | int res; |
| 1199 | int len = strlen(name) * sizeof(WCHAR); |
Patrik Stridvall | 17d1e9e | 2000-05-23 23:38:32 +0000 | [diff] [blame] | 1200 | if (!get_file_tmp_space( info, len )) return 0; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1201 | |
| 1202 | if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1) |
| 1203 | { |
| 1204 | file_read_error( "Malformed key", info ); |
Patrik Stridvall | 17d1e9e | 2000-05-23 23:38:32 +0000 | [diff] [blame] | 1205 | return 0; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1206 | } |
| 1207 | for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break; |
| 1208 | *p = 0; |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 1209 | for (res = 1; key != root_key; res++) |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1210 | { |
| 1211 | if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break; |
| 1212 | key = key->parent; |
| 1213 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 1214 | if (key == root_key) res = 0; /* no matching name */ |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1215 | return res; |
| 1216 | } |
| 1217 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1218 | /* load all the keys from the input file */ |
| 1219 | static void load_keys( struct key *key, FILE *f ) |
| 1220 | { |
| 1221 | struct key *subkey = NULL; |
| 1222 | struct file_load_info info; |
| 1223 | char *p; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1224 | unsigned int options = 0; |
| 1225 | int prefix_len = -1; /* number of key name prefixes to skip */ |
| 1226 | |
| 1227 | if (key->flags & KEY_VOLATILE) options |= REG_OPTION_VOLATILE; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1228 | |
| 1229 | info.file = f; |
| 1230 | info.len = 4; |
| 1231 | info.tmplen = 4; |
| 1232 | info.line = 0; |
| 1233 | if (!(info.buffer = mem_alloc( info.len ))) return; |
| 1234 | if (!(info.tmp = mem_alloc( info.tmplen ))) |
| 1235 | { |
| 1236 | free( info.buffer ); |
| 1237 | return; |
| 1238 | } |
| 1239 | |
| 1240 | if ((read_next_line( &info ) != 1) || |
| 1241 | strcmp( info.buffer, "WINE REGISTRY Version 2" )) |
| 1242 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1243 | set_error( STATUS_NOT_REGISTRY_FILE ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1244 | goto done; |
| 1245 | } |
| 1246 | |
| 1247 | while (read_next_line( &info ) == 1) |
| 1248 | { |
| 1249 | for (p = info.buffer; *p && isspace(*p); p++); |
| 1250 | switch(*p) |
| 1251 | { |
| 1252 | case '[': /* new key */ |
| 1253 | if (subkey) release_object( subkey ); |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1254 | if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info ); |
| 1255 | if (!(subkey = load_key( key, p + 1, options, prefix_len, &info ))) |
| 1256 | file_read_error( "Error creating key", &info ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1257 | break; |
| 1258 | case '@': /* default value */ |
| 1259 | case '\"': /* value */ |
| 1260 | if (subkey) load_value( subkey, p, &info ); |
| 1261 | else file_read_error( "Value without key", &info ); |
| 1262 | break; |
| 1263 | case '#': /* comment */ |
| 1264 | case ';': /* comment */ |
| 1265 | case 0: /* empty line */ |
| 1266 | break; |
| 1267 | default: |
| 1268 | file_read_error( "Unrecognized input", &info ); |
| 1269 | break; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | done: |
| 1274 | if (subkey) release_object( subkey ); |
| 1275 | free( info.buffer ); |
| 1276 | free( info.tmp ); |
| 1277 | } |
| 1278 | |
| 1279 | /* load a part of the registry from a file */ |
| 1280 | static void load_registry( struct key *key, int handle ) |
| 1281 | { |
| 1282 | struct object *obj; |
| 1283 | int fd; |
| 1284 | |
| 1285 | if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return; |
| 1286 | fd = obj->ops->get_read_fd( obj ); |
| 1287 | release_object( obj ); |
| 1288 | if (fd != -1) |
| 1289 | { |
| 1290 | FILE *f = fdopen( fd, "r" ); |
| 1291 | if (f) |
| 1292 | { |
| 1293 | load_keys( key, f ); |
| 1294 | fclose( f ); |
| 1295 | } |
| 1296 | else file_set_error(); |
| 1297 | } |
| 1298 | } |
| 1299 | |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 1300 | /* registry initialisation */ |
| 1301 | void init_registry(void) |
| 1302 | { |
| 1303 | static const WCHAR root_name[] = { 0 }; |
| 1304 | static const WCHAR config_name[] = |
| 1305 | { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\', |
| 1306 | 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 }; |
| 1307 | |
| 1308 | char *filename; |
| 1309 | const char *config; |
| 1310 | FILE *f; |
| 1311 | |
| 1312 | /* create the root key */ |
| 1313 | root_key = alloc_key( root_name, time(NULL) ); |
| 1314 | assert( root_key ); |
| 1315 | root_key->flags |= KEY_ROOT; |
| 1316 | |
| 1317 | /* load the config file */ |
| 1318 | config = get_config_dir(); |
| 1319 | if (!(filename = malloc( strlen(config) + 8 ))) fatal_error( "out of memory\n" ); |
| 1320 | strcpy( filename, config ); |
| 1321 | strcat( filename, "/config" ); |
| 1322 | if ((f = fopen( filename, "r" ))) |
| 1323 | { |
| 1324 | struct key *key; |
| 1325 | int dummy; |
| 1326 | |
| 1327 | /* create the config key */ |
| 1328 | if (!(key = create_key( root_key, config_name, sizeof(config_name), |
| 1329 | NULL, 0, time(NULL), &dummy ))) |
| 1330 | fatal_error( "could not create config key\n" ); |
| 1331 | key->flags |= KEY_VOLATILE; |
| 1332 | |
| 1333 | load_keys( key, f ); |
| 1334 | fclose( f ); |
| 1335 | if (get_error() == STATUS_NOT_REGISTRY_FILE) |
| 1336 | fatal_error( "%s is not a valid registry file\n", filename ); |
| 1337 | if (get_error()) |
| 1338 | fatal_error( "loading %s failed with error %x\n", filename, get_error() ); |
| 1339 | |
| 1340 | release_object( key ); |
| 1341 | } |
| 1342 | free( filename ); |
| 1343 | } |
| 1344 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1345 | /* update the level of the parents of a key (only needed for the old format) */ |
| 1346 | static int update_level( struct key *key ) |
| 1347 | { |
| 1348 | int i; |
| 1349 | int max = key->level; |
| 1350 | for (i = 0; i <= key->last_subkey; i++) |
| 1351 | { |
| 1352 | int sub = update_level( key->subkeys[i] ); |
| 1353 | if (sub > max) max = sub; |
| 1354 | } |
| 1355 | key->level = max; |
| 1356 | return max; |
| 1357 | } |
| 1358 | |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1359 | /* save a registry branch to a file */ |
| 1360 | static void save_all_subkeys( struct key *key, FILE *f ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1361 | { |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1362 | fprintf( f, "WINE REGISTRY Version 2\n" ); |
| 1363 | fprintf( f, ";; All keys relative to " ); |
| 1364 | dump_path( key, NULL, f ); |
| 1365 | fprintf( f, "\n" ); |
| 1366 | save_subkeys( key, key, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /* save a registry branch to a file handle */ |
| 1370 | static void save_registry( struct key *key, int handle ) |
| 1371 | { |
| 1372 | struct object *obj; |
| 1373 | int fd; |
| 1374 | |
| 1375 | if (key->flags & KEY_DELETED) |
| 1376 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1377 | set_error( STATUS_KEY_DELETED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1378 | return; |
| 1379 | } |
| 1380 | if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return; |
| 1381 | fd = obj->ops->get_write_fd( obj ); |
| 1382 | release_object( obj ); |
| 1383 | if (fd != -1) |
| 1384 | { |
| 1385 | FILE *f = fdopen( fd, "w" ); |
| 1386 | if (f) |
| 1387 | { |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1388 | save_all_subkeys( key, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1389 | if (fclose( f )) file_set_error(); |
| 1390 | } |
| 1391 | else |
| 1392 | { |
| 1393 | file_set_error(); |
| 1394 | close( fd ); |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1399 | /* register a key branch for being saved on exit */ |
| 1400 | static void register_branch_for_saving( struct key *key, const char *path, size_t len ) |
| 1401 | { |
| 1402 | if (save_branch_count >= MAX_SAVE_BRANCH_INFO) |
| 1403 | { |
| 1404 | set_error( STATUS_NO_MORE_ENTRIES ); |
| 1405 | return; |
| 1406 | } |
| 1407 | if (!(save_branch_info[save_branch_count].path = memdup( path, len+1 ))) return; |
| 1408 | save_branch_info[save_branch_count].path[len] = 0; |
| 1409 | save_branch_info[save_branch_count].key = (struct key *)grab_object( key ); |
| 1410 | save_branch_count++; |
| 1411 | } |
| 1412 | |
| 1413 | /* save a registry branch to a file */ |
| 1414 | static int save_branch( struct key *key, const char *path ) |
| 1415 | { |
| 1416 | char *p, *real, *tmp = NULL; |
| 1417 | int fd, count = 0, ret = 0; |
| 1418 | FILE *f; |
| 1419 | |
| 1420 | /* get the real path */ |
| 1421 | |
| 1422 | if (!(real = malloc( PATH_MAX ))) return 0; |
| 1423 | if (!realpath( path, real )) |
| 1424 | { |
| 1425 | free( real ); |
| 1426 | real = NULL; |
| 1427 | } |
| 1428 | else path = real; |
| 1429 | |
| 1430 | /* test the file type */ |
| 1431 | |
| 1432 | if ((fd = open( path, O_WRONLY )) != -1) |
| 1433 | { |
| 1434 | struct stat st; |
| 1435 | /* if file is not a regular file or has multiple links, |
| 1436 | write directly into it; otherwise use a temp file */ |
| 1437 | if (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)) |
| 1438 | { |
| 1439 | ftruncate( fd, 0 ); |
| 1440 | goto save; |
| 1441 | } |
| 1442 | close( fd ); |
| 1443 | } |
| 1444 | |
| 1445 | /* create a temp file in the same directory */ |
| 1446 | |
| 1447 | if (!(tmp = malloc( strlen(path) + 20 ))) goto done; |
| 1448 | strcpy( tmp, path ); |
| 1449 | if ((p = strrchr( tmp, '/' ))) p++; |
| 1450 | else p = tmp; |
| 1451 | for (;;) |
| 1452 | { |
Patrik Stridvall | e29dbc5 | 2000-04-24 18:04:24 +0000 | [diff] [blame] | 1453 | sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ ); |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1454 | if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break; |
| 1455 | if (errno != EEXIST) goto done; |
| 1456 | close( fd ); |
| 1457 | } |
| 1458 | |
| 1459 | /* now save to it */ |
| 1460 | |
| 1461 | save: |
| 1462 | if (!(f = fdopen( fd, "w" ))) |
| 1463 | { |
| 1464 | if (tmp) unlink( tmp ); |
| 1465 | close( fd ); |
| 1466 | goto done; |
| 1467 | } |
| 1468 | |
| 1469 | if (debug_level > 1) |
| 1470 | { |
| 1471 | fprintf( stderr, "%s: ", path ); |
| 1472 | dump_operation( key, NULL, "saving" ); |
| 1473 | } |
| 1474 | |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1475 | save_all_subkeys( key, f ); |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1476 | ret = !fclose(f); |
| 1477 | |
| 1478 | if (tmp) |
| 1479 | { |
| 1480 | /* if successfully written, rename to final name */ |
| 1481 | if (ret) ret = !rename( tmp, path ); |
| 1482 | if (!ret) unlink( tmp ); |
| 1483 | free( tmp ); |
| 1484 | } |
| 1485 | |
| 1486 | done: |
| 1487 | if (real) free( real ); |
| 1488 | return ret; |
| 1489 | } |
| 1490 | |
| 1491 | /* periodic saving of the registry */ |
| 1492 | static void periodic_save( void *arg ) |
| 1493 | { |
| 1494 | int i; |
| 1495 | for (i = 0; i < save_branch_count; i++) |
| 1496 | save_branch( save_branch_info[i].key, save_branch_info[i].path ); |
| 1497 | add_timeout( &next_save_time, save_period ); |
| 1498 | save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 ); |
| 1499 | } |
| 1500 | |
| 1501 | /* save the registry and close the top-level keys; used on server exit */ |
| 1502 | void close_registry(void) |
| 1503 | { |
| 1504 | int i; |
| 1505 | |
| 1506 | for (i = 0; i < save_branch_count; i++) |
| 1507 | { |
| 1508 | if (!save_branch( save_branch_info[i].key, save_branch_info[i].path )) |
| 1509 | { |
| 1510 | fprintf( stderr, "wineserver: could not save registry branch to %s", |
| 1511 | save_branch_info[i].path ); |
| 1512 | perror( " " ); |
| 1513 | } |
| 1514 | release_object( save_branch_info[i].key ); |
| 1515 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 1516 | release_object( root_key ); |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1520 | /* create a registry key */ |
| 1521 | DECL_HANDLER(create_key) |
| 1522 | { |
| 1523 | struct key *key, *parent; |
| 1524 | WCHAR *class; |
| 1525 | unsigned int access = req->access; |
| 1526 | |
| 1527 | if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */ |
| 1528 | req->hkey = -1; |
Alexandre Julliard | 27d31a5 | 2000-04-16 20:28:42 +0000 | [diff] [blame] | 1529 | if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1530 | { |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 1531 | if ((class = req_strdupW( req, req->class ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1532 | { |
| 1533 | if ((key = create_key( parent, req->name, sizeof(req->name), class, req->options, |
| 1534 | req->modif, &req->created ))) |
| 1535 | { |
| 1536 | req->hkey = alloc_handle( current->process, key, access, 0 ); |
| 1537 | release_object( key ); |
| 1538 | } |
| 1539 | free( class ); |
| 1540 | } |
| 1541 | release_object( parent ); |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | /* open a registry key */ |
| 1546 | DECL_HANDLER(open_key) |
| 1547 | { |
| 1548 | struct key *key, *parent; |
| 1549 | unsigned int access = req->access; |
| 1550 | |
| 1551 | if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */ |
| 1552 | req->hkey = -1; |
| 1553 | if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ ))) |
| 1554 | { |
| 1555 | if ((key = open_key( parent, req->name, sizeof(req->name) ))) |
| 1556 | { |
| 1557 | req->hkey = alloc_handle( current->process, key, access, 0 ); |
| 1558 | release_object( key ); |
| 1559 | } |
| 1560 | release_object( parent ); |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /* delete a registry key */ |
| 1565 | DECL_HANDLER(delete_key) |
| 1566 | { |
| 1567 | struct key *key; |
| 1568 | |
Alexandre Julliard | 27d31a5 | 2000-04-16 20:28:42 +0000 | [diff] [blame] | 1569 | if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1570 | { |
| 1571 | delete_key( key, req->name, sizeof(req->name) ); |
| 1572 | release_object( key ); |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | /* close a registry key */ |
| 1577 | DECL_HANDLER(close_key) |
| 1578 | { |
| 1579 | int hkey = req->hkey; |
| 1580 | /* ignore attempts to close a root key */ |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame^] | 1581 | if (hkey && !IS_SPECIAL_ROOT_HKEY(hkey)) close_handle( current->process, hkey ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /* enumerate registry subkeys */ |
| 1585 | DECL_HANDLER(enum_key) |
| 1586 | { |
| 1587 | struct key *key; |
| 1588 | |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 1589 | req->name[0] = req->class[0] = 0; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1590 | if ((key = get_hkey_obj( req->hkey, KEY_ENUMERATE_SUB_KEYS ))) |
| 1591 | { |
| 1592 | enum_key( key, req->index, req->name, req->class, &req->modif ); |
| 1593 | release_object( key ); |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | /* query information about a registry key */ |
| 1598 | DECL_HANDLER(query_key_info) |
| 1599 | { |
| 1600 | struct key *key; |
| 1601 | |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 1602 | req->name[0] = req->class[0] = 0; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1603 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE ))) |
| 1604 | { |
| 1605 | query_key( key, req ); |
| 1606 | release_object( key ); |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | /* set a value of a registry key */ |
| 1611 | DECL_HANDLER(set_key_value) |
| 1612 | { |
| 1613 | struct key *key; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1614 | unsigned int max = get_req_size( req, req->data, sizeof(req->data[0]) ); |
| 1615 | unsigned int datalen = req->len; |
| 1616 | |
| 1617 | if (datalen > max) datalen = max; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1618 | if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE ))) |
| 1619 | { |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1620 | set_value( key, copy_path( req->name ), req->type, req->total, |
| 1621 | req->offset, datalen, req->data ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1622 | release_object( key ); |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | /* retrieve the value of a registry key */ |
| 1627 | DECL_HANDLER(get_key_value) |
| 1628 | { |
| 1629 | struct key *key; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1630 | unsigned int max = get_req_size( req, req->data, sizeof(req->data[0]) ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1631 | |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 1632 | req->len = 0; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1633 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE ))) |
| 1634 | { |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1635 | get_value( key, copy_path( req->name ), req->offset, max, |
| 1636 | &req->type, &req->len, req->data ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1637 | release_object( key ); |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | /* enumerate the value of a registry key */ |
| 1642 | DECL_HANDLER(enum_key_value) |
| 1643 | { |
| 1644 | struct key *key; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1645 | unsigned int max = get_req_size( req, req->data, sizeof(req->data[0]) ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1646 | |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 1647 | req->len = 0; |
| 1648 | req->name[0] = 0; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1649 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE ))) |
| 1650 | { |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1651 | enum_value( key, req->index, req->name, req->offset, max, |
| 1652 | &req->type, &req->len, req->data ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1653 | release_object( key ); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | /* delete a value of a registry key */ |
| 1658 | DECL_HANDLER(delete_key_value) |
| 1659 | { |
| 1660 | WCHAR *name; |
| 1661 | struct key *key; |
| 1662 | |
| 1663 | if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE ))) |
| 1664 | { |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 1665 | if ((name = req_strdupW( req, req->name ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1666 | { |
| 1667 | delete_value( key, name ); |
| 1668 | free( name ); |
| 1669 | } |
| 1670 | release_object( key ); |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | /* load a registry branch from a file */ |
| 1675 | DECL_HANDLER(load_registry) |
| 1676 | { |
| 1677 | struct key *key; |
| 1678 | |
| 1679 | if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY ))) |
| 1680 | { |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1681 | /* FIXME: use subkey name */ |
| 1682 | load_registry( key, req->file ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1683 | release_object( key ); |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | /* save a registry branch to a file */ |
| 1688 | DECL_HANDLER(save_registry) |
| 1689 | { |
| 1690 | struct key *key; |
| 1691 | |
| 1692 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS ))) |
| 1693 | { |
| 1694 | save_registry( key, req->file ); |
| 1695 | release_object( key ); |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | /* set the current and saving level for the registry */ |
| 1700 | DECL_HANDLER(set_registry_levels) |
| 1701 | { |
| 1702 | current_level = req->current; |
| 1703 | saving_level = req->saving; |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1704 | |
| 1705 | /* set periodic save timer */ |
| 1706 | |
| 1707 | if (save_timeout_user) |
| 1708 | { |
| 1709 | remove_timeout_user( save_timeout_user ); |
| 1710 | save_timeout_user = NULL; |
| 1711 | } |
| 1712 | if ((save_period = req->period)) |
| 1713 | { |
| 1714 | if (save_period < 10000) save_period = 10000; /* limit rate */ |
| 1715 | gettimeofday( &next_save_time, 0 ); |
| 1716 | add_timeout( &next_save_time, save_period ); |
| 1717 | save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 ); |
| 1718 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1721 | /* save a registry branch at server exit */ |
| 1722 | DECL_HANDLER(save_registry_atexit) |
| 1723 | { |
| 1724 | struct key *key; |
| 1725 | |
| 1726 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS ))) |
| 1727 | { |
| 1728 | register_branch_for_saving( key, req->file, get_req_strlen( req, req->file ) ); |
| 1729 | release_object( key ); |
| 1730 | } |
| 1731 | } |