r168: - Cleanups in rpc backend
[kai/samba.git] / source / lib / registry / common / reg_objects.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Gerald Carter                     2002.
5  *  Copyright (C) Jelmer Vernooij                                       2003-2004.
6  *
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 2 of the License, or
10  *  (at your option) any later version.
11  *  
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.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 /* Implementation of registry frontend view functions. */
23
24 #include "includes.h"
25 #include "lib/registry/common/registry.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_SRV
29
30 /***********************************************************************
31  allocate memory for and duplicate a REG_VAL.
32  This is malloc'd memory so the caller should free it when done
33  **********************************************************************/
34
35 REG_VAL* reg_val_dup( REG_VAL *val )
36 {
37         REG_VAL         *copy = NULL;
38         TALLOC_CTX *new_mem_ctx = talloc_init(val->name);
39         
40         if ( !val ) 
41                 return NULL;
42         
43         if ( !(copy = talloc( new_mem_ctx, sizeof(REG_VAL) )) ) {
44                 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
45                 return NULL;
46         }
47         
48         /* copy all the non-pointer initial data */
49         
50         memcpy( copy, val, sizeof(REG_VAL) );
51         if ( val->data_blk ) 
52         {
53                 if ( !(copy->data_blk = talloc_memdup( new_mem_ctx, val->data_blk, val->data_len )) ) {
54                         DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
55                                 val->data_len));
56                         SAFE_FREE( copy );
57                 }
58         }
59         copy->mem_ctx = new_mem_ctx;
60         
61         return copy;    
62 }
63
64 /**********************************************************************
65  free the memory allocated to a REG_VAL 
66  *********************************************************************/
67  
68 void reg_val_free( REG_VAL *val )
69 {
70         if ( !val )
71                 return;
72
73         if(val->handle->functions->free_val_backend_data)
74                 val->handle->functions->free_val_backend_data(val);
75                 
76         talloc_destroy( val->mem_ctx );
77
78         return;
79 }
80
81 /**********************************************************************
82  *********************************************************************/
83
84 uint8* reg_val_data_blk( REG_VAL *val )
85 {
86         return val->data_blk;
87 }
88
89 /**********************************************************************
90  *********************************************************************/
91
92 int reg_val_size( REG_VAL *val )
93 {
94         return val->data_len;
95 }
96
97 /**********************************************************************
98  *********************************************************************/
99
100 char *reg_val_name( REG_VAL *val )
101 {
102         return val->name;
103 }
104
105 /**********************************************************************
106  *********************************************************************/
107
108 uint32 reg_val_type( REG_VAL *val )
109 {
110         return val->data_type;
111 }
112
113 /**********************************************************************
114  *********************************************************************/
115
116 char *reg_key_name( REG_KEY *key )
117 {
118         return key->name;
119 }
120
121 REG_KEY *reg_key_dup(REG_KEY *key)
122 {
123         key->ref++;
124         return key;
125 }
126
127 void reg_key_free(REG_KEY *key)
128 {
129         if(!key)
130                 return;
131         
132         key->ref--;
133         if(key->ref) return;
134         
135         if(key->handle->functions->free_key_backend_data)
136                 key->handle->functions->free_key_backend_data(key);
137
138         if(key->cache_values) {
139                 int i;
140                 for(i = 0; i < key->cache_values_count; i++) {
141                         reg_val_free(key->cache_values[i]);
142                 }
143         }
144
145         if(key->cache_subkeys) {
146                 int i;
147                 for(i = 0; i < key->cache_subkeys_count; i++) {
148                         reg_key_free(key->cache_subkeys[i]);
149                 }
150         }
151
152         talloc_destroy(key->mem_ctx);
153 }
154
155 char *reg_val_get_path(REG_VAL *v)
156 {
157         /* FIXME */
158         return NULL;
159 }
160
161 const char *reg_key_get_path(REG_KEY *k)
162 {
163         SMB_REG_ASSERT(k);
164         return k->path;
165 }
166
167 /* For use by the backends _ONLY_ */
168 REG_KEY *reg_key_new_abs(const char *path, REG_HANDLE *h, void *data)
169 {
170         REG_KEY *r;
171         TALLOC_CTX *mem_ctx = talloc_init(path);
172         r = talloc(mem_ctx, sizeof(REG_KEY));
173         ZERO_STRUCTP(r);
174         r->handle = h;
175         r->mem_ctx = mem_ctx;
176         r->path = talloc_strdup(mem_ctx, path);
177         r->name = talloc_strdup(mem_ctx, strrchr(path, '\\')?strrchr(path,'\\')+1:path);
178         r->backend_data = data;
179         r->ref = 1;
180         return r;
181 }
182
183 REG_KEY *reg_key_new_rel(const char *name, REG_KEY *k, void *data)
184 {
185         REG_KEY *r;
186         TALLOC_CTX *mem_ctx = talloc_init(name);
187         r = talloc(mem_ctx, sizeof(REG_KEY));
188         ZERO_STRUCTP(r);
189         r->handle = k->handle;
190         r->name = talloc_strdup(mem_ctx, name);
191         r->backend_data = data;
192         r->mem_ctx = mem_ctx;
193         r->ref = 1;
194         return r;
195 }
196
197 REG_VAL *reg_val_new(REG_KEY *parent, void *data)
198 {
199         REG_VAL *r;
200         TALLOC_CTX *mem_ctx = talloc_init("value");
201         r = talloc(mem_ctx, sizeof(REG_VAL));
202         ZERO_STRUCTP(r);
203         r->mem_ctx = mem_ctx;
204         r->handle = parent->handle;
205         r->backend_data = data;
206         r->ref = 1;
207         return r;
208 }