fixed a logic error in the sorted_tree_find_child() routine
[kai/samba.git] / source3 / registry / reg_cachehook.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Gerald Carter                     2002.
5  *
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 2 of the License, or
9  *  (at your option) any later version.
10  *  
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.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /* Implementation of registry hook cache tree */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
27
28 static SORTED_TREE *cache_tree;
29 extern REGISTRY_OPS regdb_ops;          /* these are the default */
30 static REGISTRY_HOOK default_hook = { KEY_TREE_ROOT, &regdb_ops };
31
32 /**********************************************************************
33  Initialize the cache tree
34  *********************************************************************/
35
36 BOOL reghook_cache_init( void )
37 {
38         cache_tree = sorted_tree_init( &default_hook, NULL, NULL );
39
40         return ( cache_tree == NULL );
41 }
42
43 /**********************************************************************
44  Add a new REGISTRY_HOOK to the cache.  Note that the keyname
45  is not in the exact format that a SORTED_TREE expects.
46  *********************************************************************/
47
48 BOOL reghook_cache_add( REGISTRY_HOOK *hook )
49 {
50         pstring key;
51         
52         if ( !hook )
53                 return False;
54                 
55         pstrcpy( key, "\\");
56         pstrcat( key, hook->keyname );  
57         
58         pstring_sub( key, "\\", "/" );
59
60         DEBUG(10,("reghook_cache_add: Adding key [%s]\n", key));
61                 
62         return sorted_tree_add( cache_tree, key, hook );
63 }
64
65 /**********************************************************************
66  Initialize the cache tree
67  *********************************************************************/
68
69 REGISTRY_HOOK* reghook_cache_find( char *keyname )
70 {
71         char *key;
72         int len;
73         
74         if ( !keyname )
75                 return NULL;
76         
77         /* prepend the string with a '\' character */
78         
79         len = strlen( keyname );
80         if ( !(key = malloc( len + 2 )) ) {
81                 DEBUG(0,("reghook_cache_find: malloc failed for string [%s] !?!?!\n",
82                         keyname));
83                 return NULL;
84         }
85
86         *key = '\\';
87         strncpy( key+1, keyname, len+1);
88         
89         /* swap to a form understood by the SORTED_TREE */
90
91         string_sub( key, "\\", "/", 0 );
92                 
93         DEBUG(10,("reghook_cache_find: Searching for keyname [%s]\n", key));
94         
95         return sorted_tree_find( cache_tree, key ) ;
96 }
97
98 /**********************************************************************
99  Initialize the cache tree
100  *********************************************************************/
101
102 void reghook_dump_cache( int debuglevel )
103 {
104         DEBUG(debuglevel,("reghook_dump_cache: Starting cache dump now...\n"));
105         
106         sorted_tree_print_keys( cache_tree, debuglevel );
107 }