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/>.
23 #include "lib/util/dlinklist.h"
24 #include "lib/util/tdb_wrap.h"
26 /* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
28 static void tdb_wrap_log(struct tdb_context *tdb,
29 enum tdb_log_level level,
35 const char *name = tdb_name(tdb);
38 case TDB_LOG_USE_ERROR:
49 DEBUG(dl, ("tdb(%s):%s: %s", name ? name : "unnamed",
50 tdb_errorstr(ecode), message));
54 Log tdb messages via DEBUG().
56 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
57 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
59 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
60 const char *format, ...)
74 case TDB_DEBUG_WARNING:
85 ret = vasprintf(&ptr, format, ap);
89 const char *name = tdb_name(tdb);
90 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
96 struct tdb_wrap_private {
97 struct tdb_context *tdb;
99 struct tdb_wrap_private *next, *prev;
102 static struct tdb_wrap_private *tdb_list;
104 /* destroy the last connection to a tdb */
105 static int tdb_wrap_private_destructor(struct tdb_wrap_private *w)
108 DLIST_REMOVE(tdb_list, w);
112 static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
119 struct tdb_wrap_private *result;
121 result = talloc(mem_ctx, struct tdb_wrap_private);
122 if (result == NULL) {
125 result->name = talloc_strdup(result, name);
126 if (result->name == NULL) {
130 #if _SAMBA_BUILD_ == 3
131 /* This #if _SAMBA_BUILD == 3 is very unfortunate, as it means
132 * that in the top level build, these options are not
133 * available for these databases. However, having two
134 * different tdb_wrap lists is a worse fate, so this will do
137 if (!lp_use_mmap()) {
138 tdb_flags |= TDB_NOMMAP;
141 if ((hash_size == 0) && (name != NULL)) {
143 base = strrchr_m(name, '/');
150 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
154 result->tdb = tdb_open_compat(name, hash_size, tdb_flags,
155 open_flags, mode, tdb_wrap_log, NULL);
156 if (result->tdb == NULL) {
159 talloc_set_destructor(result, tdb_wrap_private_destructor);
160 DLIST_ADD(tdb_list, result);
169 wrapped connection to a tdb database
170 to close just talloc_free() the tdb_wrap pointer
172 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
173 const char *name, int hash_size, int tdb_flags,
174 int open_flags, mode_t mode)
176 struct tdb_wrap *result;
177 struct tdb_wrap_private *w;
179 result = talloc(mem_ctx, struct tdb_wrap);
180 if (result == NULL) {
184 for (w=tdb_list;w;w=w->next) {
185 if (strcmp(name, w->name) == 0) {
191 w = tdb_wrap_private_open(result, name, hash_size, tdb_flags,
195 * Correctly use talloc_reference: The tdb will be
196 * closed when "w" is being freed. The caller never
197 * sees "w", so an incorrect use of talloc_free(w)
198 * instead of calling talloc_unlink is not possible.
199 * To avoid having to refcount ourselves, "w" will
200 * have multiple parents that hang off all the
201 * tdb_wrap's being returned from here. Those parents
202 * can be freed without problem.
204 if (talloc_reference(result, w) == NULL) {
211 result->tdb = w->tdb;