Add detection for need of update to the registry db.
[Samba/gebeck_regimport.git] / source / registry / reg_cachehook.c
blob74670aac30ec211c9b50bd1646cf3cdcc3fa2725
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* Implementation of registry hook cache tree */
22 #include "includes.h"
23 #include "adt_tree.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_REGISTRY
28 static SORTED_TREE *cache_tree = NULL;
29 extern REGISTRY_OPS regdb_ops; /* these are the default */
30 static REGISTRY_HOOK default_hook = { KEY_TREE_ROOT, &regdb_ops };
32 /**********************************************************************
33 Initialize the cache tree if it has not been initialized yet.
34 *********************************************************************/
36 bool reghook_cache_init( void )
38 if (cache_tree == NULL) {
39 cache_tree = pathtree_init(&default_hook, NULL);
42 return (cache_tree != NULL);
45 /**********************************************************************
46 Add a new REGISTRY_HOOK to the cache. Note that the keyname
47 is not in the exact format that a SORTED_TREE expects.
48 *********************************************************************/
50 bool reghook_cache_add( REGISTRY_HOOK *hook )
52 TALLOC_CTX *ctx = talloc_tos();
53 char *key = NULL;
55 if (!hook) {
56 return false;
59 key = talloc_asprintf(ctx, "//%s", hook->keyname);
60 if (!key) {
61 return false;
63 key = talloc_string_sub(ctx, key, "\\", "/");
64 if (!key) {
65 return false;
68 DEBUG(10,("reghook_cache_add: Adding key [%s]\n", key));
70 return pathtree_add( cache_tree, key, hook );
73 /**********************************************************************
74 Initialize the cache tree
75 *********************************************************************/
77 REGISTRY_HOOK* reghook_cache_find( const char *keyname )
79 char *key;
80 int len;
81 REGISTRY_HOOK *hook;
83 if ( !keyname )
84 return NULL;
86 /* prepend the string with a '\' character */
88 len = strlen( keyname );
89 if ( !(key = (char *)SMB_MALLOC( len + 2 )) ) {
90 DEBUG(0,("reghook_cache_find: malloc failed for string [%s] !?!?!\n",
91 keyname));
92 return NULL;
95 *key = '\\';
96 strncpy( key+1, keyname, len+1);
98 /* swap to a form understood by the SORTED_TREE */
100 string_sub( key, "\\", "/", 0 );
102 DEBUG(10,("reghook_cache_find: Searching for keyname [%s]\n", key));
104 hook = (REGISTRY_HOOK *)pathtree_find( cache_tree, key ) ;
106 SAFE_FREE( key );
108 return hook;
111 /**********************************************************************
112 Initialize the cache tree
113 *********************************************************************/
115 void reghook_dump_cache( int debuglevel )
117 DEBUG(debuglevel,("reghook_dump_cache: Starting cache dump now...\n"));
119 pathtree_print_keys( cache_tree, debuglevel );