2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Rafal Szczesniak 2002
6 Copyright (C) Michael Adam 2007
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 "system/filesys.h"
31 /* these are little tdb utility functions that are meant to make
32 dealing with a tdb database a little less cumbersome in Samba */
34 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
35 TDB_DATA data, int flags)
37 TDB_DATA key = string_term_tdb_data(keystr);
39 return tdb_trans_store(tdb, key, data, flags);
42 /****************************************************************************
43 Useful pair of routines for packing/unpacking data consisting of
45 ****************************************************************************/
47 static size_t tdb_pack_va(uint8 *buf, int bufsize, const char *fmt, va_list ap)
58 const char *fmt0 = fmt;
59 int bufsize0 = bufsize;
62 switch ((c = *fmt++)) {
63 case 'b': /* unsigned 8-bit integer */
65 bt = (uint8)va_arg(ap, int);
66 if (bufsize && bufsize >= len)
69 case 'w': /* unsigned 16-bit integer */
71 w = (uint16)va_arg(ap, int);
72 if (bufsize && bufsize >= len)
75 case 'd': /* signed 32-bit integer (standard int in most systems) */
77 d = va_arg(ap, uint32);
78 if (bufsize && bufsize >= len)
81 case 'p': /* pointer */
83 p = va_arg(ap, void *);
85 if (bufsize && bufsize >= len)
88 case 'P': /* null-terminated string */
89 s = va_arg(ap,char *);
92 if (bufsize && bufsize >= len)
95 case 'f': /* null-terminated string */
96 s = va_arg(ap,char *);
99 if (bufsize && bufsize >= len)
102 case 'B': /* fixed-length string */
104 s = va_arg(ap, char *);
106 if (bufsize && bufsize >= len) {
112 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
125 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
126 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
128 return PTR_DIFF(buf, buf0);
131 size_t tdb_pack(uint8 *buf, int bufsize, const char *fmt, ...)
137 result = tdb_pack_va(buf, bufsize, fmt, ap);
142 bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
143 const char *fmt, ...)
149 len1 = tdb_pack_va(NULL, 0, fmt, ap);
152 if (mem_ctx != NULL) {
153 *buf = talloc_realloc(mem_ctx, *buf, uint8,
156 *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
164 len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
176 /****************************************************************************
177 Useful pair of routines for packing/unpacking data consisting of
178 integers and strings.
179 ****************************************************************************/
181 int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
192 const uint8 *buf0 = buf;
193 const char *fmt0 = fmt;
194 int bufsize0 = bufsize;
199 switch ((c=*fmt++)) {
200 case 'b': /* unsigned 8-bit integer */
202 bt = va_arg(ap, uint8 *);
207 case 'w': /* unsigned 16-bit integer */
209 w = va_arg(ap, uint16 *);
214 case 'd': /* unsigned 32-bit integer (standard int in most systems) */
216 d = va_arg(ap, uint32 *);
221 case 'p': /* pointer */
223 p = va_arg(ap, void **);
227 * This isn't a real pointer - only a token (1 or 0)
228 * to mark the fact a pointer is present.
231 *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
233 case 'P': /* null-terminated string */
234 /* Return malloc'ed string. */
235 ps = va_arg(ap,char **);
236 len = strnlen((const char *)buf, bufsize) + 1;
239 *ps = SMB_STRDUP((const char *)buf);
244 case 'f': /* null-terminated string */
245 s = va_arg(ap,char *);
246 len = strnlen((const char *)buf, bufsize) + 1;
247 if (bufsize < len || len > sizeof(fstring))
251 case 'B': /* fixed-length string */
252 i = va_arg(ap, int *);
253 b = va_arg(ap, char **);
265 *b = (char *)SMB_MALLOC(*i);
268 memcpy(*b, buf+4, *i);
271 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
284 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
285 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
287 return PTR_DIFF(buf, buf0);
295 /****************************************************************************
296 Log tdb messages via DEBUG().
297 ****************************************************************************/
299 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
305 va_start(ap, format);
306 ret = vasprintf(&ptr, format, ap);
309 if ((ret == -1) || !*ptr)
312 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
316 /****************************************************************************
317 Like tdb_open() but also setup a logging function that redirects to
318 the samba DEBUG() system.
319 ****************************************************************************/
321 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
322 int open_flags, mode_t mode)
327 tdb_flags |= TDB_NOMMAP;
329 if ((hash_size == 0) && (name != NULL)) {
330 const char *base = strrchr_m(name, '/');
337 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
340 tdb = tdb_open_compat(name, hash_size, tdb_flags,
341 open_flags, mode, tdb_log, NULL);
348 /****************************************************************************
349 tdb_store, wrapped in a transaction. This way we make sure that a process
350 that dies within writing does not leave a corrupt tdb behind.
351 ****************************************************************************/
353 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
358 if ((res = tdb_transaction_start(tdb)) != 0) {
359 DEBUG(5, ("tdb_transaction_start failed\n"));
363 if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
364 DEBUG(10, ("tdb_store failed\n"));
365 tdb_transaction_cancel(tdb);
369 if ((res = tdb_transaction_commit(tdb)) != 0) {
370 DEBUG(5, ("tdb_transaction_commit failed\n"));
376 /****************************************************************************
377 tdb_delete, wrapped in a transaction. This way we make sure that a process
378 that dies within deleting does not leave a corrupt tdb behind.
379 ****************************************************************************/
381 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
385 if ((res = tdb_transaction_start(tdb)) != 0) {
386 DEBUG(5, ("tdb_transaction_start failed\n"));
390 if ((res = tdb_delete(tdb, key)) != 0) {
391 DEBUG(10, ("tdb_delete failed\n"));
392 tdb_transaction_cancel(tdb);
396 if ((res = tdb_transaction_commit(tdb)) != 0) {
397 DEBUG(5, ("tdb_transaction_commit failed\n"));
403 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
406 if (t1.dptr == NULL && t2.dptr != NULL) {
409 if (t1.dptr != NULL && t2.dptr == NULL) {
412 if (t1.dptr == t2.dptr) {
413 return t1.dsize - t2.dsize;
415 ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
417 return t1.dsize - t2.dsize;