s4-ldb: update ldb_tdb to use new DLIST_ macros
[Samba/gebeck_regimport.git] / source4 / lib / ldb / ldb_tdb / ldb_tdb_wrap.c
blobb9f3e79f205342de2c809d16045bd1cf0d117e32
1 /*
2 ldb database library
4 Copyright (C) Andrew Tridgell 2005
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "ldb_tdb.h"
25 #include "dlinklist.h"
28 the purpose of this code is to work around the braindead posix locking
29 rules, to allow us to have a ldb open more than once while allowing
30 locking to work
33 struct ltdb_wrap {
34 struct ltdb_wrap *next, *prev;
35 struct tdb_context *tdb;
36 dev_t device;
37 ino_t inode;
40 static struct ltdb_wrap *tdb_list;
42 /* destroy the last connection to a tdb */
43 static int ltdb_wrap_destructor(struct ltdb_wrap *w)
45 tdb_close(w->tdb);
46 DLIST_REMOVE(tdb_list, w);
47 return 0;
50 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
51 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
53 va_list ap;
54 const char *name = tdb_name(tdb);
55 struct ldb_context *ldb = talloc_get_type(tdb_get_logging_private(tdb), struct ldb_context);
56 enum ldb_debug_level ldb_level;
57 char *message;
59 if (ldb == NULL)
60 return;
62 va_start(ap, fmt);
63 message = talloc_vasprintf(ldb, fmt, ap);
64 va_end(ap);
66 switch (level) {
67 case TDB_DEBUG_FATAL:
68 ldb_level = LDB_DEBUG_FATAL;
69 break;
70 case TDB_DEBUG_ERROR:
71 ldb_level = LDB_DEBUG_ERROR;
72 break;
73 case TDB_DEBUG_WARNING:
74 ldb_level = LDB_DEBUG_WARNING;
75 break;
76 case TDB_DEBUG_TRACE:
77 ldb_level = LDB_DEBUG_TRACE;
78 break;
79 default:
80 ldb_level = LDB_DEBUG_FATAL;
83 ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
84 talloc_free(message);
88 wrapped connection to a tdb database. The caller should _not_ free
89 this as it is not a talloc structure (as tdb does not use talloc
90 yet). It will auto-close when the caller frees the mem_ctx that is
91 passed to this call
93 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
94 const char *path, int hash_size,
95 int tdb_flags,
96 int open_flags, mode_t mode,
97 struct ldb_context *ldb)
99 struct ltdb_wrap *w;
100 struct stat st;
101 struct tdb_logging_context log_ctx;
103 log_ctx.log_fn = ltdb_log_fn;
104 log_ctx.log_private = ldb;
106 if (stat(path, &st) == 0) {
107 for (w=tdb_list;w;w=w->next) {
108 if (st.st_dev == w->device && st.st_ino == w->inode) {
109 if (!talloc_reference(mem_ctx, w)) {
110 return NULL;
112 return w->tdb;
117 w = talloc(mem_ctx, struct ltdb_wrap);
118 if (w == NULL) {
119 return NULL;
122 w->tdb = tdb_open_ex(path, hash_size, tdb_flags, open_flags, mode, &log_ctx, NULL);
123 if (w->tdb == NULL) {
124 talloc_free(w);
125 return NULL;
128 if (fstat(tdb_fd(w->tdb), &st) != 0) {
129 tdb_close(w->tdb);
130 talloc_free(w);
131 return NULL;
134 w->device = st.st_dev;
135 w->inode = st.st_ino;
137 talloc_set_destructor(w, ltdb_wrap_destructor);
139 DLIST_ADD(tdb_list, w);
141 return w->tdb;