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);
241 case 'f': /* null-terminated string */
242 s = va_arg(ap,char *);
243 len = strnlen((const char *)buf, bufsize) + 1;
244 if (bufsize < len || len > sizeof(fstring))
248 case 'B': /* fixed-length string */
249 i = va_arg(ap, int *);
250 b = va_arg(ap, char **);
262 *b = (char *)SMB_MALLOC(*i);
265 memcpy(*b, buf+4, *i);
268 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
281 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
282 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
284 return PTR_DIFF(buf, buf0);
292 /****************************************************************************
293 Log tdb messages via DEBUG().
294 ****************************************************************************/
296 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
302 va_start(ap, format);
303 ret = vasprintf(&ptr, format, ap);
306 if ((ret == -1) || !*ptr)
309 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
313 /****************************************************************************
314 Like tdb_open() but also setup a logging function that redirects to
315 the samba DEBUG() system.
316 ****************************************************************************/
318 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
319 int open_flags, mode_t mode)
324 tdb_flags |= TDB_NOMMAP;
326 if ((hash_size == 0) && (name != NULL)) {
327 const char *base = strrchr_m(name, '/');
334 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
337 tdb = tdb_open_compat(name, hash_size, tdb_flags,
338 open_flags, mode, tdb_log, NULL);
345 /****************************************************************************
346 tdb_store, wrapped in a transaction. This way we make sure that a process
347 that dies within writing does not leave a corrupt tdb behind.
348 ****************************************************************************/
350 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
355 if ((res = tdb_transaction_start(tdb)) != 0) {
356 DEBUG(5, ("tdb_transaction_start failed\n"));
360 if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
361 DEBUG(10, ("tdb_store failed\n"));
362 tdb_transaction_cancel(tdb);
366 if ((res = tdb_transaction_commit(tdb)) != 0) {
367 DEBUG(5, ("tdb_transaction_commit failed\n"));
373 /****************************************************************************
374 tdb_delete, wrapped in a transaction. This way we make sure that a process
375 that dies within deleting does not leave a corrupt tdb behind.
376 ****************************************************************************/
378 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
382 if ((res = tdb_transaction_start(tdb)) != 0) {
383 DEBUG(5, ("tdb_transaction_start failed\n"));
387 if ((res = tdb_delete(tdb, key)) != 0) {
388 DEBUG(10, ("tdb_delete failed\n"));
389 tdb_transaction_cancel(tdb);
393 if ((res = tdb_transaction_commit(tdb)) != 0) {
394 DEBUG(5, ("tdb_transaction_commit failed\n"));
400 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
403 if (t1.dptr == NULL && t2.dptr != NULL) {
406 if (t1.dptr != NULL && t2.dptr == NULL) {
409 if (t1.dptr == t2.dptr) {
410 return t1.dsize - t2.dsize;
412 ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
414 return t1.dsize - t2.dsize;