2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 2004
6 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 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/>.
24 #include "../lib/util/dlinklist.h"
28 static struct tdb_wrap *tdb_list;
30 /* destroy the last connection to a tdb */
31 static int tdb_wrap_destructor(struct tdb_wrap *w)
34 DLIST_REMOVE(tdb_list, w);
39 Log tdb messages via DEBUG().
41 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
42 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
44 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
45 const char *format, ...)
52 vasprintf(&ptr, format, ap);
62 case TDB_DEBUG_WARNING:
73 const char *name = tdb_name(tdb);
74 DEBUG(dl, ("tdb(%s): %s", name ? name : "unnamed", ptr));
81 wrapped connection to a tdb database
82 to close just talloc_free() the tdb_wrap pointer
84 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
85 const char *name, int hash_size, int tdb_flags,
86 int open_flags, mode_t mode)
89 struct tdb_logging_context log_ctx;
90 log_ctx.log_fn = tdb_wrap_log;
92 for (w=tdb_list;w;w=w->next) {
93 if (strcmp(name, w->name) == 0) {
94 return talloc_reference(mem_ctx, w);
98 w = talloc(mem_ctx, struct tdb_wrap);
103 w->name = talloc_strdup(w, name);
105 w->tdb = tdb_open_ex(name, hash_size, tdb_flags,
106 open_flags, mode, &log_ctx, NULL);
107 if (w->tdb == NULL) {
112 talloc_set_destructor(w, tdb_wrap_destructor);
114 DLIST_ADD(tdb_list, w);