/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- make_tdb_data
- string_tdb_data
- string_term_tdb_data
- tdb_lock_bystring
- tdb_unlock_bystring
- tdb_read_lock_bystring
- tdb_read_unlock_bystring
- tdb_fetch_int32_byblob
- tdb_fetch_int32
- tdb_store_int32_byblob
- tdb_store_int32
- tdb_fetch_uint32_byblob
- tdb_fetch_uint32
- tdb_store_uint32_byblob
- tdb_store_uint32
- tdb_store_bystring
- tdb_fetch_bystring
- tdb_delete_bystring
- tdb_change_int32_atomic
- tdb_change_uint32_atomic
- tdb_traverse_delete_fn
1 /*
2 Unix SMB/CIFS implementation.
3
4 tdb utility functions
5
6 Copyright (C) Andrew Tridgell 1992-2006
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "../tdb/include/tdb.h"
24 #include "../lib/util/util_tdb.h"
25
26 /* these are little tdb utility functions that are meant to make
27 dealing with a tdb database a little less cumbersome in Samba */
28
29 /***************************************************************
30 Make a TDB_DATA and keep the const warning in one place
31 ****************************************************************/
32
33 TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
/* [<][>][^][v][top][bottom][index][help] */
34 {
35 TDB_DATA ret;
36 ret.dptr = discard_const_p(uint8_t, dptr);
37 ret.dsize = dsize;
38 return ret;
39 }
40
41 TDB_DATA string_tdb_data(const char *string)
/* [<][>][^][v][top][bottom][index][help] */
42 {
43 return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
44 }
45
46 TDB_DATA string_term_tdb_data(const char *string)
/* [<][>][^][v][top][bottom][index][help] */
47 {
48 return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
49 }
50
51 /****************************************************************************
52 Lock a chain by string. Return -1 if lock failed.
53 ****************************************************************************/
54
55 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
/* [<][>][^][v][top][bottom][index][help] */
56 {
57 TDB_DATA key = string_term_tdb_data(keyval);
58
59 return tdb_chainlock(tdb, key);
60 }
61
62 /****************************************************************************
63 Unlock a chain by string.
64 ****************************************************************************/
65
66 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
/* [<][>][^][v][top][bottom][index][help] */
67 {
68 TDB_DATA key = string_term_tdb_data(keyval);
69
70 tdb_chainunlock(tdb, key);
71 }
72
73 /****************************************************************************
74 Read lock a chain by string. Return -1 if lock failed.
75 ****************************************************************************/
76
77 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
/* [<][>][^][v][top][bottom][index][help] */
78 {
79 TDB_DATA key = string_term_tdb_data(keyval);
80
81 return tdb_chainlock_read(tdb, key);
82 }
83
84 /****************************************************************************
85 Read unlock a chain by string.
86 ****************************************************************************/
87
88 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
/* [<][>][^][v][top][bottom][index][help] */
89 {
90 TDB_DATA key = string_term_tdb_data(keyval);
91
92 tdb_chainunlock_read(tdb, key);
93 }
94
95
96 /****************************************************************************
97 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
98 Output is int32_t in native byte order.
99 ****************************************************************************/
100
101 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
/* [<][>][^][v][top][bottom][index][help] */
102 {
103 TDB_DATA data;
104 int32_t ret;
105
106 data = tdb_fetch(tdb, key);
107 if (!data.dptr || data.dsize != sizeof(int32_t)) {
108 SAFE_FREE(data.dptr);
109 return -1;
110 }
111
112 ret = IVAL(data.dptr,0);
113 SAFE_FREE(data.dptr);
114 return ret;
115 }
116
117 /****************************************************************************
118 Fetch a int32_t value by string key, return -1 if not found.
119 Output is int32_t in native byte order.
120 ****************************************************************************/
121
122 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
/* [<][>][^][v][top][bottom][index][help] */
123 {
124 return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
125 }
126
127 /****************************************************************************
128 Store a int32_t value by an arbitary blob key, return 0 on success, -1 on failure.
129 Input is int32_t in native byte order. Output in tdb is in little-endian.
130 ****************************************************************************/
131
132 int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key, int32_t v)
/* [<][>][^][v][top][bottom][index][help] */
133 {
134 TDB_DATA data;
135 int32_t v_store;
136
137 SIVAL(&v_store,0,v);
138 data.dptr = (unsigned char *)&v_store;
139 data.dsize = sizeof(int32_t);
140
141 return tdb_store(tdb, key, data, TDB_REPLACE);
142 }
143
144 /****************************************************************************
145 Store a int32_t value by string key, return 0 on success, -1 on failure.
146 Input is int32_t in native byte order. Output in tdb is in little-endian.
147 ****************************************************************************/
148
149 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
/* [<][>][^][v][top][bottom][index][help] */
150 {
151 return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
152 }
153
154 /****************************************************************************
155 Fetch a uint32_t value by a arbitrary blob key, return false if not found.
156 Output is uint32_t in native byte order.
157 ****************************************************************************/
158
159 bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t *value)
/* [<][>][^][v][top][bottom][index][help] */
160 {
161 TDB_DATA data;
162
163 data = tdb_fetch(tdb, key);
164 if (!data.dptr || data.dsize != sizeof(uint32_t)) {
165 SAFE_FREE(data.dptr);
166 return false;
167 }
168
169 *value = IVAL(data.dptr,0);
170 SAFE_FREE(data.dptr);
171 return true;
172 }
173
174 /****************************************************************************
175 Fetch a uint32_t value by string key, return false if not found.
176 Output is uint32_t in native byte order.
177 ****************************************************************************/
178
179 bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
/* [<][>][^][v][top][bottom][index][help] */
180 {
181 return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
182 }
183
184 /****************************************************************************
185 Store a uint32_t value by an arbitary blob key, return 0 on success, -1 on failure.
186 Input is uint32_t in native byte order. Output in tdb is in little-endian.
187 ****************************************************************************/
188
189 bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t value)
/* [<][>][^][v][top][bottom][index][help] */
190 {
191 TDB_DATA data;
192 uint32_t v_store;
193 bool ret = true;
194
195 SIVAL(&v_store, 0, value);
196 data.dptr = (unsigned char *)&v_store;
197 data.dsize = sizeof(uint32_t);
198
199 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
200 ret = false;
201
202 return ret;
203 }
204
205 /****************************************************************************
206 Store a uint32_t value by string key, return 0 on success, -1 on failure.
207 Input is uint32_t in native byte order. Output in tdb is in little-endian.
208 ****************************************************************************/
209
210 bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
/* [<][>][^][v][top][bottom][index][help] */
211 {
212 return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
213 }
214 /****************************************************************************
215 Store a buffer by a null terminated string key. Return 0 on success, -1
216 on failure.
217 ****************************************************************************/
218
219 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
/* [<][>][^][v][top][bottom][index][help] */
220 {
221 TDB_DATA key = string_term_tdb_data(keystr);
222
223 return tdb_store(tdb, key, data, flags);
224 }
225
226 /****************************************************************************
227 Fetch a buffer using a null terminated string key. Don't forget to call
228 free() on the result dptr.
229 ****************************************************************************/
230
231 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
/* [<][>][^][v][top][bottom][index][help] */
232 {
233 TDB_DATA key = string_term_tdb_data(keystr);
234
235 return tdb_fetch(tdb, key);
236 }
237
238 /****************************************************************************
239 Delete an entry using a null terminated string key.
240 ****************************************************************************/
241
242 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
/* [<][>][^][v][top][bottom][index][help] */
243 {
244 TDB_DATA key = string_term_tdb_data(keystr);
245
246 return tdb_delete(tdb, key);
247 }
248
249 /****************************************************************************
250 Atomic integer change. Returns old value. To create, set initial value in *oldval.
251 ****************************************************************************/
252
253 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
/* [<][>][^][v][top][bottom][index][help] */
254 {
255 int32_t val;
256 int32_t ret = -1;
257
258 if (tdb_lock_bystring(tdb, keystr) == -1)
259 return -1;
260
261 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
262 /* The lookup failed */
263 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
264 /* but not because it didn't exist */
265 goto err_out;
266 }
267
268 /* Start with 'old' value */
269 val = *oldval;
270
271 } else {
272 /* It worked, set return value (oldval) to tdb data */
273 *oldval = val;
274 }
275
276 /* Increment value for storage and return next time */
277 val += change_val;
278
279 if (tdb_store_int32(tdb, keystr, val) == -1)
280 goto err_out;
281
282 ret = 0;
283
284 err_out:
285
286 tdb_unlock_bystring(tdb, keystr);
287 return ret;
288 }
289
290 /****************************************************************************
291 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
292 ****************************************************************************/
293
294 bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
/* [<][>][^][v][top][bottom][index][help] */
295 {
296 uint32_t val;
297 bool ret = false;
298
299 if (tdb_lock_bystring(tdb, keystr) == -1)
300 return false;
301
302 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
303 /* It failed */
304 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
305 /* and not because it didn't exist */
306 goto err_out;
307 }
308
309 /* Start with 'old' value */
310 val = *oldval;
311
312 } else {
313 /* it worked, set return value (oldval) to tdb data */
314 *oldval = val;
315
316 }
317
318 /* get a new value to store */
319 val += change_val;
320
321 if (!tdb_store_uint32(tdb, keystr, val))
322 goto err_out;
323
324 ret = true;
325
326 err_out:
327
328 tdb_unlock_bystring(tdb, keystr);
329 return ret;
330 }
331
332 /****************************************************************************
333 Allow tdb_delete to be used as a tdb_traversal_fn.
334 ****************************************************************************/
335
336 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
/* [<][>][^][v][top][bottom][index][help] */
337 void *state)
338 {
339 return tdb_delete(the_tdb, key);
340 }