libndr: add support for relative_rap_convert.
[Samba/ekacnet.git] / source3 / registry / reg_cachehook.c
blob6b00ab4aa17024933f437eed128097006435eb24
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002.
5 * Copyright (C) Michael Adam 2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* Implementation of registry hook cache tree */
23 #include "includes.h"
24 #include "adt_tree.h"
25 #include "registry.h"
26 #include "reg_cachehook.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_REGISTRY
31 static struct sorted_tree *cache_tree = NULL;
32 extern struct registry_ops regdb_ops; /* these are the default */
34 static WERROR keyname_to_path(TALLOC_CTX *mem_ctx, const char *keyname,
35 char **path)
37 char *tmp_path = NULL;
39 if ((keyname == NULL) || (path == NULL)) {
40 return WERR_INVALID_PARAM;
43 tmp_path = talloc_asprintf(mem_ctx, "\\%s", keyname);
44 if (tmp_path == NULL) {
45 DEBUG(0, ("talloc_asprintf failed!\n"));
46 return WERR_NOMEM;
49 tmp_path = talloc_string_sub(mem_ctx, tmp_path, "\\", "/");
50 if (tmp_path == NULL) {
51 DEBUG(0, ("talloc_string_sub_failed!\n"));
52 return WERR_NOMEM;
55 *path = tmp_path;
57 return WERR_OK;
60 /**********************************************************************
61 Initialize the cache tree if it has not been initialized yet.
62 *********************************************************************/
64 WERROR reghook_cache_init(void)
66 if (cache_tree != NULL) {
67 return WERR_OK;
70 cache_tree = pathtree_init(&regdb_ops);
71 if (cache_tree == NULL) {
72 return WERR_NOMEM;
74 DEBUG(10, ("reghook_cache_init: new tree with default "
75 "ops %p for key [%s]\n", (void *)&regdb_ops,
76 KEY_TREE_ROOT));
77 return WERR_OK;
80 /**********************************************************************
81 Add a new registry hook to the cache. Note that the keyname
82 is not in the exact format that a struct sorted_tree expects.
83 *********************************************************************/
85 WERROR reghook_cache_add(const char *keyname, struct registry_ops *ops)
87 WERROR werr;
88 char *key = NULL;
90 if ((keyname == NULL) || (ops == NULL)) {
91 return WERR_INVALID_PARAM;
94 werr = keyname_to_path(talloc_tos(), keyname, &key);
95 if (!W_ERROR_IS_OK(werr)) {
96 goto done;
99 DEBUG(10, ("reghook_cache_add: Adding ops %p for key [%s]\n",
100 (void *)ops, key));
102 werr = pathtree_add(cache_tree, key, ops);
104 done:
105 TALLOC_FREE(key);
106 return werr;
109 /**********************************************************************
110 Find a key in the cache.
111 *********************************************************************/
113 struct registry_ops *reghook_cache_find(const char *keyname)
115 WERROR werr;
116 char *key = NULL;
117 struct registry_ops *ops = NULL;
119 if (keyname == NULL) {
120 return NULL;
123 werr = keyname_to_path(talloc_tos(), keyname, &key);
124 if (!W_ERROR_IS_OK(werr)) {
125 goto done;
128 DEBUG(10,("reghook_cache_find: Searching for keyname [%s]\n", key));
130 ops = (struct registry_ops *)pathtree_find(cache_tree, key);
132 DEBUG(10, ("reghook_cache_find: found ops %p for key [%s]\n",
133 ops ? (void *)ops : 0, key));
135 done:
136 TALLOC_FREE(key);
138 return ops;
141 /**********************************************************************
142 Print out the cache tree structure for debugging.
143 *********************************************************************/
145 void reghook_dump_cache( int debuglevel )
147 DEBUG(debuglevel,("reghook_dump_cache: Starting cache dump now...\n"));
149 pathtree_print_keys( cache_tree, debuglevel );