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"
32 /* these are little tdb utility functions that are meant to make
33 dealing with a tdb database a little less cumbersome in Samba */
35 int tdb_trans_store_bystring(TDB_CONTEXT
*tdb
, const char *keystr
,
36 TDB_DATA data
, int flags
)
38 TDB_DATA key
= string_term_tdb_data(keystr
);
40 return tdb_trans_store(tdb
, key
, data
, flags
);
43 /****************************************************************************
44 Useful pair of routines for packing/unpacking data consisting of
46 ****************************************************************************/
48 static size_t tdb_pack_va(uint8
*buf
, int bufsize
, const char *fmt
, va_list ap
)
59 const char *fmt0
= fmt
;
60 int bufsize0
= bufsize
;
63 switch ((c
= *fmt
++)) {
64 case 'b': /* unsigned 8-bit integer */
66 bt
= (uint8
)va_arg(ap
, int);
67 if (bufsize
&& bufsize
>= len
)
70 case 'w': /* unsigned 16-bit integer */
72 w
= (uint16
)va_arg(ap
, int);
73 if (bufsize
&& bufsize
>= len
)
76 case 'd': /* signed 32-bit integer (standard int in most systems) */
78 d
= va_arg(ap
, uint32
);
79 if (bufsize
&& bufsize
>= len
)
82 case 'p': /* pointer */
84 p
= va_arg(ap
, void *);
86 if (bufsize
&& bufsize
>= len
)
89 case 'P': /* null-terminated string */
90 s
= va_arg(ap
,char *);
93 if (bufsize
&& bufsize
>= len
)
96 case 'f': /* null-terminated string */
97 s
= va_arg(ap
,char *);
100 if (bufsize
&& bufsize
>= len
)
103 case 'B': /* fixed-length string */
105 s
= va_arg(ap
, char *);
107 if (bufsize
&& bufsize
>= len
) {
113 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
126 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
127 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
129 return PTR_DIFF(buf
, buf0
);
132 size_t tdb_pack(uint8
*buf
, int bufsize
, const char *fmt
, ...)
138 result
= tdb_pack_va(buf
, bufsize
, fmt
, ap
);
143 bool tdb_pack_append(TALLOC_CTX
*mem_ctx
, uint8
**buf
, size_t *len
,
144 const char *fmt
, ...)
150 len1
= tdb_pack_va(NULL
, 0, fmt
, ap
);
153 if (mem_ctx
!= NULL
) {
154 *buf
= talloc_realloc(mem_ctx
, *buf
, uint8
,
157 *buf
= SMB_REALLOC_ARRAY(*buf
, uint8
, (*len
) + len1
);
165 len2
= tdb_pack_va((*buf
)+(*len
), len1
, fmt
, ap
);
177 /****************************************************************************
178 Useful pair of routines for packing/unpacking data consisting of
179 integers and strings.
180 ****************************************************************************/
182 int tdb_unpack(const uint8
*buf
, int bufsize
, const char *fmt
, ...)
193 const uint8
*buf0
= buf
;
194 const char *fmt0
= fmt
;
195 int bufsize0
= bufsize
;
200 switch ((c
=*fmt
++)) {
201 case 'b': /* unsigned 8-bit integer */
203 bt
= va_arg(ap
, uint8
*);
208 case 'w': /* unsigned 16-bit integer */
210 w
= va_arg(ap
, uint16
*);
215 case 'd': /* unsigned 32-bit integer (standard int in most systems) */
217 d
= va_arg(ap
, uint32
*);
222 case 'p': /* pointer */
224 p
= va_arg(ap
, void **);
228 * This isn't a real pointer - only a token (1 or 0)
229 * to mark the fact a pointer is present.
232 *p
= (void *)(IVAL(buf
, 0) ? (void *)1 : NULL
);
234 case 'P': /* null-terminated string */
235 /* Return malloc'ed string. */
236 ps
= va_arg(ap
,char **);
237 len
= strnlen((const char *)buf
, bufsize
) + 1;
240 *ps
= SMB_STRDUP((const char *)buf
);
245 case 'f': /* null-terminated string */
246 s
= va_arg(ap
,char *);
247 len
= strnlen((const char *)buf
, bufsize
) + 1;
248 if (bufsize
< len
|| len
> sizeof(fstring
))
252 case 'B': /* fixed-length string */
253 i
= va_arg(ap
, int *);
254 b
= va_arg(ap
, char **);
266 *b
= (char *)SMB_MALLOC(*i
);
269 memcpy(*b
, buf
+4, *i
);
272 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
285 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
286 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
288 return PTR_DIFF(buf
, buf0
);
296 /****************************************************************************
297 Log tdb messages via DEBUG().
298 ****************************************************************************/
300 static void tdb_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
, const char *format
, ...)
306 va_start(ap
, format
);
307 ret
= vasprintf(&ptr
, format
, ap
);
310 if ((ret
== -1) || !*ptr
)
313 DEBUG((int)level
, ("tdb(%s): %s", tdb_name(tdb
) ? tdb_name(tdb
) : "unnamed", ptr
));
317 /****************************************************************************
318 Like tdb_open() but also setup a logging function that redirects to
319 the samba DEBUG() system.
320 ****************************************************************************/
322 TDB_CONTEXT
*tdb_open_log(const char *name
, int hash_size
, int tdb_flags
,
323 int open_flags
, mode_t mode
)
328 tdb_flags
|= TDB_NOMMAP
;
330 if ((hash_size
== 0) && (name
!= NULL
)) {
331 const char *base
= strrchr_m(name
, '/');
338 hash_size
= lp_parm_int(-1, "tdb_hashsize", base
, 0);
341 tdb
= tdb_open_compat(name
, hash_size
, tdb_flags
,
342 open_flags
, mode
, tdb_log
, NULL
);
349 /****************************************************************************
350 tdb_store, wrapped in a transaction. This way we make sure that a process
351 that dies within writing does not leave a corrupt tdb behind.
352 ****************************************************************************/
354 int tdb_trans_store(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
359 if ((res
= tdb_transaction_start(tdb
)) != 0) {
360 DEBUG(5, ("tdb_transaction_start failed\n"));
364 if ((res
= tdb_store(tdb
, key
, dbuf
, flag
)) != 0) {
365 DEBUG(10, ("tdb_store failed\n"));
366 tdb_transaction_cancel(tdb
);
370 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
371 DEBUG(5, ("tdb_transaction_commit failed\n"));
377 /****************************************************************************
378 tdb_delete, wrapped in a transaction. This way we make sure that a process
379 that dies within deleting does not leave a corrupt tdb behind.
380 ****************************************************************************/
382 int tdb_trans_delete(struct tdb_context
*tdb
, TDB_DATA key
)
386 if ((res
= tdb_transaction_start(tdb
)) != 0) {
387 DEBUG(5, ("tdb_transaction_start failed\n"));
391 if ((res
= tdb_delete(tdb
, key
)) != 0) {
392 DEBUG(10, ("tdb_delete failed\n"));
393 tdb_transaction_cancel(tdb
);
397 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
398 DEBUG(5, ("tdb_transaction_commit failed\n"));
404 int tdb_data_cmp(TDB_DATA t1
, TDB_DATA t2
)
407 if (t1
.dptr
== NULL
&& t2
.dptr
!= NULL
) {
410 if (t1
.dptr
!= NULL
&& t2
.dptr
== NULL
) {
413 if (t1
.dptr
== t2
.dptr
) {
414 return t1
.dsize
- t2
.dsize
;
416 ret
= memcmp(t1
.dptr
, t2
.dptr
, MIN(t1
.dsize
, t2
.dsize
));
418 return t1
.dsize
- t2
.dsize
;
423 char *tdb_data_string(TALLOC_CTX
*mem_ctx
, TDB_DATA d
)
427 cbuf
*ost
= cbuf_new(mem_ctx
);
433 len
= cbuf_printf(ost
, "%d:");
438 if (d
.dptr
== NULL
) {
439 len
= cbuf_puts(ost
, "<NULL>", -1);
441 len
= cbuf_print_quoted(ost
, (const char*)d
.dptr
, d
.dsize
);
447 cbuf_swapptr(ost
, &ret
, 0);
448 talloc_steal(mem_ctx
, ret
);