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