registry: adapt copied function header comments.
[kai/samba.git] / source3 / registry / reg_cachehook.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
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 3 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 /* Implementation of registry hook cache tree */
21
22 #include "includes.h"
23 #include "adt_tree.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_REGISTRY
27
28 static SORTED_TREE *cache_tree = NULL;
29 extern REGISTRY_OPS regdb_ops;          /* these are the default */
30
31 /**********************************************************************
32  Initialize the cache tree if it has not been initialized yet.
33  *********************************************************************/
34
35 bool reghook_cache_init( void )
36 {
37         if (cache_tree == NULL) {
38                 cache_tree = pathtree_init(&regdb_ops, NULL);
39                 if (cache_tree !=0) {
40                         DEBUG(10, ("reghook_cache_init: new tree with default "
41                                    "ops %p for key [%s]\n", (void *)&regdb_ops,
42                                    KEY_TREE_ROOT));
43                 }
44         }
45
46         return (cache_tree != NULL);
47 }
48
49 /**********************************************************************
50  Add a new registry hook to the cache.  Note that the keyname
51  is not in the exact format that a SORTED_TREE expects.
52  *********************************************************************/
53
54 bool reghook_cache_add(const char *keyname, REGISTRY_OPS *ops)
55 {
56         TALLOC_CTX *ctx = talloc_tos();
57         char *key = NULL;
58
59         if ((keyname == NULL) || (ops == NULL)) {
60                 return false;
61         }
62
63         key = talloc_asprintf(ctx, "\\%s", keyname);
64         if (!key) {
65                 return false;
66         }
67         key = talloc_string_sub(ctx, key, "\\", "/");
68         if (!key) {
69                 return false;
70         }
71
72         DEBUG(10, ("reghook_cache_add: Adding ops %p for key [%s]\n",
73                    (void *)ops, key));
74
75         return pathtree_add(cache_tree, key, ops);
76 }
77
78 /**********************************************************************
79  Find a key in the cache.
80  *********************************************************************/
81
82 REGISTRY_OPS *reghook_cache_find(const char *keyname)
83 {
84         char *key;
85         int len;
86         REGISTRY_OPS *ops;
87         
88         if ( !keyname )
89                 return NULL;
90         
91         /* prepend the string with a '\' character */
92         
93         len = strlen( keyname );
94         if ( !(key = (char *)SMB_MALLOC( len + 2 )) ) {
95                 DEBUG(0,("reghook_cache_find: malloc failed for string [%s] !?!?!\n",
96                         keyname));
97                 return NULL;
98         }
99
100         *key = '\\';
101         strncpy( key+1, keyname, len+1);
102         
103         /* swap to a form understood by the SORTED_TREE */
104
105         string_sub( key, "\\", "/", 0 );
106                 
107         DEBUG(10,("reghook_cache_find: Searching for keyname [%s]\n", key));
108         
109         ops = (REGISTRY_OPS *)pathtree_find(cache_tree, key);
110
111         DEBUG(10, ("reghook_cache_find: found ops %p for key [%s]\n",
112                    ops ? (void *)ops : 0, key));
113         
114         SAFE_FREE( key );
115         
116         return ops;
117 }
118
119 /**********************************************************************
120  Print out the cache tree structure for debugging.
121  *********************************************************************/
122
123 void reghook_dump_cache( int debuglevel )
124 {
125         DEBUG(debuglevel,("reghook_dump_cache: Starting cache dump now...\n"));
126         
127         pathtree_print_keys( cache_tree, debuglevel );
128 }