s3-registry: only include registry headers when really needed.
[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 #include "registry.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_REGISTRY
28
29 static struct sorted_tree *cache_tree = NULL;
30 extern struct registry_ops regdb_ops;           /* these are the default */
31
32 static WERROR keyname_to_path(TALLOC_CTX *mem_ctx, const char *keyname,
33                               char **path)
34 {
35         char *tmp_path = NULL;
36
37         if ((keyname == NULL) || (path == NULL)) {
38                 return WERR_INVALID_PARAM;
39         }
40
41         tmp_path = talloc_asprintf(mem_ctx, "\\%s", keyname);
42         if (tmp_path == NULL) {
43                 DEBUG(0, ("talloc_asprintf failed!\n"));
44                 return WERR_NOMEM;
45         }
46
47         tmp_path = talloc_string_sub(mem_ctx, tmp_path, "\\", "/");
48         if (tmp_path == NULL) {
49                 DEBUG(0, ("talloc_string_sub_failed!\n"));
50                 return WERR_NOMEM;
51         }
52
53         *path = tmp_path;
54
55         return WERR_OK;
56 }
57
58 /**********************************************************************
59  Initialize the cache tree if it has not been initialized yet.
60  *********************************************************************/
61
62 WERROR reghook_cache_init(void)
63 {
64         if (cache_tree != NULL) {
65                 return WERR_OK;
66         }
67
68         cache_tree = pathtree_init(&regdb_ops);
69         if (cache_tree == NULL) {
70                 return WERR_NOMEM;
71         }
72         DEBUG(10, ("reghook_cache_init: new tree with default "
73                    "ops %p for key [%s]\n", (void *)&regdb_ops,
74                    KEY_TREE_ROOT));
75         return WERR_OK;
76 }
77
78 /**********************************************************************
79  Add a new registry hook to the cache.  Note that the keyname
80  is not in the exact format that a struct sorted_tree expects.
81  *********************************************************************/
82
83 WERROR reghook_cache_add(const char *keyname, struct registry_ops *ops)
84 {
85         WERROR werr;
86         char *key = NULL;
87
88         if ((keyname == NULL) || (ops == NULL)) {
89                 return WERR_INVALID_PARAM;
90         }
91
92         werr = keyname_to_path(talloc_tos(), keyname, &key);
93         if (!W_ERROR_IS_OK(werr)) {
94                 goto done;
95         }
96
97         DEBUG(10, ("reghook_cache_add: Adding ops %p for key [%s]\n",
98                    (void *)ops, key));
99
100         werr = pathtree_add(cache_tree, key, ops);
101
102 done:
103         TALLOC_FREE(key);
104         return werr;
105 }
106
107 /**********************************************************************
108  Find a key in the cache.
109  *********************************************************************/
110
111 struct registry_ops *reghook_cache_find(const char *keyname)
112 {
113         WERROR werr;
114         char *key = NULL;
115         struct registry_ops *ops = NULL;
116
117         if (keyname == NULL) {
118                 return NULL;
119         }
120
121         werr = keyname_to_path(talloc_tos(), keyname, &key);
122         if (!W_ERROR_IS_OK(werr)) {
123                 goto done;
124         }
125
126         DEBUG(10,("reghook_cache_find: Searching for keyname [%s]\n", key));
127
128         ops = (struct registry_ops *)pathtree_find(cache_tree, key);
129
130         DEBUG(10, ("reghook_cache_find: found ops %p for key [%s]\n",
131                    ops ? (void *)ops : 0, key));
132
133 done:
134         TALLOC_FREE(key);
135
136         return ops;
137 }
138
139 /**********************************************************************
140  Print out the cache tree structure for debugging.
141  *********************************************************************/
142
143 void reghook_dump_cache( int debuglevel )
144 {
145         DEBUG(debuglevel,("reghook_dump_cache: Starting cache dump now...\n"));
146
147         pathtree_print_keys( cache_tree, debuglevel );
148 }