2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 1992-2005
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 NOTE: these utility functions are specific to Samba, and are not part
28 #include "lib/tdb/include/tdbutil.h"
29 #include "system/glob.h"
30 #include "system/wait.h"
31 #include "system/filesys.h"
32 #include "lib/util/dlinklist.h"
35 /* these are little tdb utility functions that are meant to make
36 dealing with a tdb database a little less cumbersome in Samba */
38 /***************************************************************
39 Make a TDB_DATA and keep the const warning in one place
40 ****************************************************************/
42 static TDB_DATA make_tdb_data(const char *dptr, size_t dsize)
45 ret.dptr = discard_const_p(unsigned char, dptr);
50 /****************************************************************************
51 Lock a chain by string. Return -1 if lock failed.
52 ****************************************************************************/
54 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
56 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
58 return tdb_chainlock(tdb, key);
61 /****************************************************************************
62 Unlock a chain by string.
63 ****************************************************************************/
65 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
67 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
69 tdb_chainunlock(tdb, key);
72 /****************************************************************************
73 Read lock a chain by string. Return -1 if lock failed.
74 ****************************************************************************/
76 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
78 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
80 return tdb_chainlock_read(tdb, key);
83 /****************************************************************************
84 Read unlock a chain by string.
85 ****************************************************************************/
87 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
89 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
91 tdb_chainunlock_read(tdb, key);
95 /****************************************************************************
96 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
97 Output is int32_t in native byte order.
98 ****************************************************************************/
100 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, const char *keyval, size_t len)
102 TDB_DATA key = make_tdb_data(keyval, len);
106 data = tdb_fetch(tdb, key);
107 if (!data.dptr || data.dsize != sizeof(int32_t)) {
108 SAFE_FREE(data.dptr);
112 ret = IVAL(data.dptr,0);
113 SAFE_FREE(data.dptr);
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 ****************************************************************************/
122 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
124 return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
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 ****************************************************************************/
132 int tdb_store_int32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, int32_t v)
134 TDB_DATA key = make_tdb_data(keystr, len);
139 data.dptr = (void *)&v_store;
140 data.dsize = sizeof(int32_t);
142 return tdb_store(tdb, key, data, TDB_REPLACE);
145 /****************************************************************************
146 Store a int32_t value by string key, return 0 on success, -1 on failure.
147 Input is int32_t in native byte order. Output in tdb is in little-endian.
148 ****************************************************************************/
150 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
152 return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
155 /****************************************************************************
156 Fetch a uint32_t value by a arbitrary blob key, return -1 if not found.
157 Output is uint32_t in native byte order.
158 ****************************************************************************/
160 BOOL tdb_fetch_uint32_byblob(struct tdb_context *tdb, const char *keyval, size_t len, uint32_t *value)
162 TDB_DATA key = make_tdb_data(keyval, len);
165 data = tdb_fetch(tdb, key);
166 if (!data.dptr || data.dsize != sizeof(uint32_t)) {
167 SAFE_FREE(data.dptr);
171 *value = IVAL(data.dptr,0);
172 SAFE_FREE(data.dptr);
176 /****************************************************************************
177 Fetch a uint32_t value by string key, return -1 if not found.
178 Output is uint32_t in native byte order.
179 ****************************************************************************/
181 BOOL tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
183 return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
186 /****************************************************************************
187 Store a uint32_t value by an arbitary blob key, return 0 on success, -1 on failure.
188 Input is uint32_t in native byte order. Output in tdb is in little-endian.
189 ****************************************************************************/
191 BOOL tdb_store_uint32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, uint32_t value)
193 TDB_DATA key = make_tdb_data(keystr, len);
198 SIVAL(&v_store, 0, value);
199 data.dptr = (void *)&v_store;
200 data.dsize = sizeof(uint32_t);
202 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
208 /****************************************************************************
209 Store a uint32_t value by string key, return 0 on success, -1 on failure.
210 Input is uint32_t in native byte order. Output in tdb is in little-endian.
211 ****************************************************************************/
213 BOOL tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
215 return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
217 /****************************************************************************
218 Store a buffer by a null terminated string key. Return 0 on success, -1
220 ****************************************************************************/
222 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
224 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
226 return tdb_store(tdb, key, data, flags);
229 /****************************************************************************
230 Fetch a buffer using a null terminated string key. Don't forget to call
231 free() on the result dptr.
232 ****************************************************************************/
234 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
236 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
238 return tdb_fetch(tdb, key);
241 /****************************************************************************
242 Delete an entry using a null terminated string key.
243 ****************************************************************************/
245 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
247 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
249 return tdb_delete(tdb, key);
252 /****************************************************************************
253 Atomic integer change. Returns old value. To create, set initial value in *oldval.
254 ****************************************************************************/
256 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
261 if (tdb_lock_bystring(tdb, keystr) == -1)
264 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
265 /* The lookup failed */
266 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
267 /* but not because it didn't exist */
271 /* Start with 'old' value */
275 /* It worked, set return value (oldval) to tdb data */
279 /* Increment value for storage and return next time */
282 if (tdb_store_int32(tdb, keystr, val) == -1)
289 tdb_unlock_bystring(tdb, keystr);
293 /****************************************************************************
294 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
295 ****************************************************************************/
297 BOOL tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
302 if (tdb_lock_bystring(tdb, keystr) == -1)
305 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
307 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
308 /* and not because it didn't exist */
312 /* Start with 'old' value */
316 /* it worked, set return value (oldval) to tdb data */
321 /* get a new value to store */
324 if (!tdb_store_uint32(tdb, keystr, val))
331 tdb_unlock_bystring(tdb, keystr);
335 /****************************************************************************
336 Allow tdb_delete to be used as a tdb_traversal_fn.
337 ****************************************************************************/
339 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
342 return tdb_delete(the_tdb, key);
347 /****************************************************************************
348 Useful pair of routines for packing/unpacking data consisting of
349 integers and strings.
350 ****************************************************************************/
352 size_t tdb_pack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
364 const char *fmt0 = fmt;
365 int bufsize0 = bufsize;
366 tdb_log_func log_fn = tdb_log_fn(tdb);
371 switch ((c = *fmt++)) {
372 case 'b': /* unsigned 8-bit integer */
374 bt = (uint8_t)va_arg(ap, int);
375 if (bufsize && bufsize >= len)
378 case 'w': /* unsigned 16-bit integer */
380 w = (uint16_t)va_arg(ap, int);
381 if (bufsize && bufsize >= len)
384 case 'd': /* signed 32-bit integer (standard int in most systems) */
386 d = va_arg(ap, uint32_t);
387 if (bufsize && bufsize >= len)
390 case 'p': /* pointer */
392 p = va_arg(ap, void *);
394 if (bufsize && bufsize >= len)
397 case 'P': /* null-terminated string */
398 s = va_arg(ap,char *);
401 if (bufsize && bufsize >= len)
404 case 'f': /* null-terminated string */
405 s = va_arg(ap,char *);
408 if (bufsize && bufsize >= len)
411 case 'B': /* fixed-length string */
413 s = va_arg(ap, char *);
415 if (bufsize && bufsize >= len) {
421 log_fn(tdb, 0,"Unknown tdb_pack format %c in %s\n",
436 log_fn(tdb, 18,"tdb_pack(%s, %d) -> %d\n",
437 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
439 return PTR_DIFF(buf, buf0);
442 /****************************************************************************
443 Useful pair of routines for packing/unpacking data consisting of
444 integers and strings.
445 ****************************************************************************/
447 int tdb_unpack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
459 const char *fmt0 = fmt;
460 int bufsize0 = bufsize;
461 tdb_log_func log_fn = tdb_log_fn(tdb);
466 switch ((c=*fmt++)) {
469 bt = va_arg(ap, uint8_t *);
476 w = va_arg(ap, uint16_t *);
483 d = va_arg(ap, uint32_t *);
490 p = va_arg(ap, void **);
493 *p = (void *)IVAL(buf, 0);
496 s = va_arg(ap,char *);
497 len = strlen(buf) + 1;
498 if (bufsize < len || len > sizeof(pstring))
503 s = va_arg(ap,char *);
504 len = strlen(buf) + 1;
505 if (bufsize < len || len > sizeof(fstring))
510 i = va_arg(ap, int *);
511 b = va_arg(ap, char **);
523 *b = (char *)malloc(*i);
526 memcpy(*b, buf+4, *i);
529 log_fn(tdb, 0, "Unknown tdb_unpack format %c in %s\n",
542 log_fn(tdb, 18, "tdb_unpack(%s, %d) -> %d\n",
543 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
545 return PTR_DIFF(buf, buf0);