r828: Some fixes in the core and regshell concerning hives and
[gd/samba-autobuild/.git] / source4 / 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         val->ref++;
38         return val;     
39 }
40
41 /**********************************************************************
42  free the memory allocated to a REG_VAL 
43  *********************************************************************/
44  
45 void reg_val_free( REG_VAL *val )
46 {
47         if ( !val )
48                 return;
49
50         val->ref--;
51         if(val->ref) return;
52
53         if(val->handle->functions->free_val_backend_data)
54                 val->handle->functions->free_val_backend_data(val);
55                 
56         talloc_destroy( val->mem_ctx );
57
58         return;
59 }
60
61 /**********************************************************************
62  *********************************************************************/
63
64 uint8* reg_val_data_blk( REG_VAL *val )
65 {
66         return val->data_blk;
67 }
68
69 /**********************************************************************
70  *********************************************************************/
71
72 int reg_val_size( REG_VAL *val )
73 {
74         return val->data_len;
75 }
76
77 /**********************************************************************
78  *********************************************************************/
79
80 char *reg_val_name( REG_VAL *val )
81 {
82         return val->name;
83 }
84
85 /**********************************************************************
86  *********************************************************************/
87
88 uint32 reg_val_type( REG_VAL *val )
89 {
90         return val->data_type;
91 }
92
93 /**********************************************************************
94  *********************************************************************/
95
96 char *reg_key_name( REG_KEY *key )
97 {
98         return key->name;
99 }
100
101 REG_KEY *reg_key_dup(REG_KEY *key)
102 {
103         key->ref++;
104         return key;
105 }
106
107 void reg_key_free(REG_KEY *key)
108 {
109         if(!key)
110                 return;
111         
112         key->ref--;
113         if(key->ref) return;
114         
115         if(key->handle->functions->free_key_backend_data)
116                 key->handle->functions->free_key_backend_data(key);
117
118         if(key->cache_values) {
119                 int i;
120                 for(i = 0; i < key->cache_values_count; i++) {
121                         reg_val_free(key->cache_values[i]);
122                 }
123         }
124
125         if(key->cache_subkeys) {
126                 int i;
127                 for(i = 0; i < key->cache_subkeys_count; i++) {
128                         reg_key_free(key->cache_subkeys[i]);
129                 }
130         }
131
132         talloc_destroy(key->mem_ctx);
133 }
134
135 char *reg_val_get_path(REG_VAL *v)
136 {
137         /* FIXME */
138         return NULL;
139 }
140
141 const char *reg_key_get_path(REG_KEY *k)
142 {
143         SMB_REG_ASSERT(k);
144         return strchr(k->path, '\\')?strchr(k->path, '\\')+1:"";
145 }
146
147 const char *reg_key_get_path_abs(REG_KEY *k)
148 {
149         SMB_REG_ASSERT(k);
150         return k->path;
151 }
152
153 /* For use by the backends _ONLY_ */
154 REG_KEY *reg_key_new_abs(const char *path, REG_HANDLE *h, void *data)
155 {
156         REG_KEY *r;
157         TALLOC_CTX *mem_ctx = talloc_init(path);
158         r = talloc(mem_ctx, sizeof(REG_KEY));
159         ZERO_STRUCTP(r);
160         r->handle = h;
161         r->mem_ctx = mem_ctx;
162         r->path = talloc_strdup(mem_ctx, path);
163         r->name = talloc_strdup(mem_ctx, strrchr(path, '\\')?strrchr(path,'\\')+1:path);
164         r->backend_data = data;
165         r->ref = 1;
166         return r;
167 }
168
169 REG_KEY *reg_key_new_rel(const char *name, REG_KEY *k, void *data)
170 {
171         REG_KEY *r;
172         const char *parent_path = k?reg_key_get_path(k):"";
173         TALLOC_CTX *mem_ctx = talloc_init(name);
174         r = talloc(mem_ctx, sizeof(REG_KEY));
175         ZERO_STRUCTP(r);
176         r->handle = k->handle;
177         r->hive = k->hive;
178         r->name = talloc_strdup(mem_ctx, name);
179         
180         r->path = talloc_asprintf(mem_ctx, "%s%s%s", parent_path, *parent_path && parent_path[strlen(parent_path)-1] != '\\'?"\\":"", name);
181         r->backend_data = data;
182         r->mem_ctx = mem_ctx;
183         r->ref = 1;
184         return r;
185 }
186
187 REG_VAL *reg_val_new(REG_KEY *parent, void *data)
188 {
189         REG_VAL *r;
190         TALLOC_CTX *mem_ctx = talloc_init("value");
191         r = talloc(mem_ctx, sizeof(REG_VAL));
192         ZERO_STRUCTP(r);
193         r->mem_ctx = mem_ctx;
194         r->handle = parent->handle;
195         r->backend_data = data;
196         r->ref = 1;
197         return r;
198 }