2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 1992-2006
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.
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.
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/>.
23 #include "lib/tdb/include/tdb.h"
26 /* these are little tdb utility functions that are meant to make
27 dealing with a tdb database a little less cumbersome in Samba */
29 /***************************************************************
30 Make a TDB_DATA and keep the const warning in one place
31 ****************************************************************/
33 static TDB_DATA make_tdb_data(const char *dptr, size_t dsize)
36 ret.dptr = discard_const_p(unsigned char, dptr);
41 /****************************************************************************
42 Lock a chain by string. Return -1 if lock failed.
43 ****************************************************************************/
45 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
47 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
49 return tdb_chainlock(tdb, key);
52 /****************************************************************************
53 Unlock a chain by string.
54 ****************************************************************************/
56 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
58 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
60 tdb_chainunlock(tdb, key);
63 /****************************************************************************
64 Read lock a chain by string. Return -1 if lock failed.
65 ****************************************************************************/
67 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
69 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
71 return tdb_chainlock_read(tdb, key);
74 /****************************************************************************
75 Read unlock a chain by string.
76 ****************************************************************************/
78 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
80 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
82 tdb_chainunlock_read(tdb, key);
86 /****************************************************************************
87 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
88 Output is int32_t in native byte order.
89 ****************************************************************************/
91 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, const char *keyval, size_t len)
93 TDB_DATA key = make_tdb_data(keyval, len);
97 data = tdb_fetch(tdb, key);
98 if (!data.dptr || data.dsize != sizeof(int32_t)) {
103 ret = IVAL(data.dptr,0);
104 SAFE_FREE(data.dptr);
108 /****************************************************************************
109 Fetch a int32_t value by string key, return -1 if not found.
110 Output is int32_t in native byte order.
111 ****************************************************************************/
113 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
115 return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
118 /****************************************************************************
119 Store a int32_t value by an arbitary blob key, return 0 on success, -1 on failure.
120 Input is int32_t in native byte order. Output in tdb is in little-endian.
121 ****************************************************************************/
123 int tdb_store_int32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, int32_t v)
125 TDB_DATA key = make_tdb_data(keystr, len);
130 data.dptr = (void *)&v_store;
131 data.dsize = sizeof(int32_t);
133 return tdb_store(tdb, key, data, TDB_REPLACE);
136 /****************************************************************************
137 Store a int32_t value by string key, return 0 on success, -1 on failure.
138 Input is int32_t in native byte order. Output in tdb is in little-endian.
139 ****************************************************************************/
141 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
143 return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
146 /****************************************************************************
147 Fetch a uint32_t value by a arbitrary blob key, return -1 if not found.
148 Output is uint32_t in native byte order.
149 ****************************************************************************/
151 BOOL tdb_fetch_uint32_byblob(struct tdb_context *tdb, const char *keyval, size_t len, uint32_t *value)
153 TDB_DATA key = make_tdb_data(keyval, len);
156 data = tdb_fetch(tdb, key);
157 if (!data.dptr || data.dsize != sizeof(uint32_t)) {
158 SAFE_FREE(data.dptr);
162 *value = IVAL(data.dptr,0);
163 SAFE_FREE(data.dptr);
167 /****************************************************************************
168 Fetch a uint32_t value by string key, return -1 if not found.
169 Output is uint32_t in native byte order.
170 ****************************************************************************/
172 BOOL tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
174 return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
177 /****************************************************************************
178 Store a uint32_t value by an arbitary blob key, return 0 on success, -1 on failure.
179 Input is uint32_t in native byte order. Output in tdb is in little-endian.
180 ****************************************************************************/
182 BOOL tdb_store_uint32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, uint32_t value)
184 TDB_DATA key = make_tdb_data(keystr, len);
189 SIVAL(&v_store, 0, value);
190 data.dptr = (void *)&v_store;
191 data.dsize = sizeof(uint32_t);
193 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
199 /****************************************************************************
200 Store a uint32_t value by string key, return 0 on success, -1 on failure.
201 Input is uint32_t in native byte order. Output in tdb is in little-endian.
202 ****************************************************************************/
204 BOOL tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
206 return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
208 /****************************************************************************
209 Store a buffer by a null terminated string key. Return 0 on success, -1
211 ****************************************************************************/
213 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
215 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
217 return tdb_store(tdb, key, data, flags);
220 /****************************************************************************
221 Fetch a buffer using a null terminated string key. Don't forget to call
222 free() on the result dptr.
223 ****************************************************************************/
225 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
227 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
229 return tdb_fetch(tdb, key);
232 /****************************************************************************
233 Delete an entry using a null terminated string key.
234 ****************************************************************************/
236 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
238 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
240 return tdb_delete(tdb, key);
243 /****************************************************************************
244 Atomic integer change. Returns old value. To create, set initial value in *oldval.
245 ****************************************************************************/
247 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
252 if (tdb_lock_bystring(tdb, keystr) == -1)
255 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
256 /* The lookup failed */
257 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
258 /* but not because it didn't exist */
262 /* Start with 'old' value */
266 /* It worked, set return value (oldval) to tdb data */
270 /* Increment value for storage and return next time */
273 if (tdb_store_int32(tdb, keystr, val) == -1)
280 tdb_unlock_bystring(tdb, keystr);
284 /****************************************************************************
285 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
286 ****************************************************************************/
288 BOOL tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
293 if (tdb_lock_bystring(tdb, keystr) == -1)
296 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
298 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
299 /* and not because it didn't exist */
303 /* Start with 'old' value */
307 /* it worked, set return value (oldval) to tdb data */
312 /* get a new value to store */
315 if (!tdb_store_uint32(tdb, keystr, val))
322 tdb_unlock_bystring(tdb, keystr);
326 /****************************************************************************
327 Allow tdb_delete to be used as a tdb_traversal_fn.
328 ****************************************************************************/
330 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
333 return tdb_delete(the_tdb, key);
338 /****************************************************************************
339 Useful pair of routines for packing/unpacking data consisting of
340 integers and strings.
341 ****************************************************************************/
343 size_t tdb_pack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
355 const char *fmt0 = fmt;
356 int bufsize0 = bufsize;
357 tdb_log_func log_fn = tdb_log_fn(tdb);
362 switch ((c = *fmt++)) {
363 case 'b': /* unsigned 8-bit integer */
365 bt = (uint8_t)va_arg(ap, int);
366 if (bufsize && bufsize >= len)
369 case 'w': /* unsigned 16-bit integer */
371 w = (uint16_t)va_arg(ap, int);
372 if (bufsize && bufsize >= len)
375 case 'd': /* signed 32-bit integer (standard int in most systems) */
377 d = va_arg(ap, uint32_t);
378 if (bufsize && bufsize >= len)
381 case 'p': /* pointer */
383 p = va_arg(ap, void *);
385 if (bufsize && bufsize >= len)
388 case 'P': /* null-terminated string */
389 s = va_arg(ap,char *);
392 if (bufsize && bufsize >= len)
395 case 'f': /* null-terminated string */
396 s = va_arg(ap,char *);
399 if (bufsize && bufsize >= len)
402 case 'B': /* fixed-length string */
404 s = va_arg(ap, char *);
406 if (bufsize && bufsize >= len) {
412 log_fn(tdb, 0,"Unknown tdb_pack format %c in %s\n",
427 log_fn(tdb, 18,"tdb_pack(%s, %d) -> %d\n",
428 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
430 return PTR_DIFF(buf, buf0);
433 /****************************************************************************
434 Useful pair of routines for packing/unpacking data consisting of
435 integers and strings.
436 ****************************************************************************/
438 int tdb_unpack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
450 const char *fmt0 = fmt;
451 int bufsize0 = bufsize;
452 tdb_log_func log_fn = tdb_log_fn(tdb);
457 switch ((c=*fmt++)) {
460 bt = va_arg(ap, uint8_t *);
467 w = va_arg(ap, uint16_t *);
474 d = va_arg(ap, uint32_t *);
481 p = va_arg(ap, void **);
484 *p = (void *)IVAL(buf, 0);
487 s = va_arg(ap,char *);
488 len = strlen(buf) + 1;
489 if (bufsize < len || len > sizeof(pstring))
494 s = va_arg(ap,char *);
495 len = strlen(buf) + 1;
496 if (bufsize < len || len > sizeof(fstring))
501 i = va_arg(ap, int *);
502 b = va_arg(ap, char **);
514 *b = (char *)malloc(*i);
517 memcpy(*b, buf+4, *i);
520 log_fn(tdb, 0, "Unknown tdb_unpack format %c in %s\n",
533 log_fn(tdb, 18, "tdb_unpack(%s, %d) -> %d\n",
534 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
536 return PTR_DIFF(buf, buf0);