s3-rpc_client: move protos to cli_lsarpc.h
[Samba/ekacnet.git] / source3 / registry / reg_cachehook.c
blob6812ecbd7cf6962a1a0df957e2c496591e6129ca
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002.
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.
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.
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/>.
20 /* Implementation of registry hook cache tree */
22 #include "includes.h"
23 #include "adt_tree.h"
24 #include "registry.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_REGISTRY
29 static struct sorted_tree *cache_tree = NULL;
30 extern struct registry_ops regdb_ops; /* these are the default */
32 static WERROR keyname_to_path(TALLOC_CTX *mem_ctx, const char *keyname,
33 char **path)
35 char *tmp_path = NULL;
37 if ((keyname == NULL) || (path == NULL)) {
38 return WERR_INVALID_PARAM;
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;
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;
53 *path = tmp_path;
55 return WERR_OK;
58 /**********************************************************************
59 Initialize the cache tree if it has not been initialized yet.
60 *********************************************************************/
62 WERROR reghook_cache_init(void)
64 if (cache_tree != NULL) {
65 return WERR_OK;
68 cache_tree = pathtree_init(&regdb_ops);
69 if (cache_tree == NULL) {
70 return WERR_NOMEM;
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;
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 *********************************************************************/
83 WERROR reghook_cache_add(const char *keyname, struct registry_ops *ops)
85 WERROR werr;
86 char *key = NULL;
88 if ((keyname == NULL) || (ops == NULL)) {
89 return WERR_INVALID_PARAM;
92 werr = keyname_to_path(talloc_tos(), keyname, &key);
93 if (!W_ERROR_IS_OK(werr)) {
94 goto done;
97 DEBUG(10, ("reghook_cache_add: Adding ops %p for key [%s]\n",
98 (void *)ops, key));
100 werr = pathtree_add(cache_tree, key, ops);
102 done:
103 TALLOC_FREE(key);
104 return werr;
107 /**********************************************************************
108 Find a key in the cache.
109 *********************************************************************/
111 struct registry_ops *reghook_cache_find(const char *keyname)
113 WERROR werr;
114 char *key = NULL;
115 struct registry_ops *ops = NULL;
117 if (keyname == NULL) {
118 return NULL;
121 werr = keyname_to_path(talloc_tos(), keyname, &key);
122 if (!W_ERROR_IS_OK(werr)) {
123 goto done;
126 DEBUG(10,("reghook_cache_find: Searching for keyname [%s]\n", key));
128 ops = (struct registry_ops *)pathtree_find(cache_tree, key);
130 DEBUG(10, ("reghook_cache_find: found ops %p for key [%s]\n",
131 ops ? (void *)ops : 0, key));
133 done:
134 TALLOC_FREE(key);
136 return ops;
139 /**********************************************************************
140 Print out the cache tree structure for debugging.
141 *********************************************************************/
143 void reghook_dump_cache( int debuglevel )
145 DEBUG(debuglevel,("reghook_dump_cache: Starting cache dump now...\n"));
147 pathtree_print_keys( cache_tree, debuglevel );