Improve the VFS Makefile so that it is easier for use out of tree but still works...
[Samba/gebeck_regimport.git] / lib / ldb / ldb_tdb / ldb_tdb_wrap.c
blob3ddcba5cea9da55432a27670979dda7a04911718
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"
27 /* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
28 #if BUILD_TDB2
29 static void ltdb_log_fn(struct tdb_context *tdb,
30 enum tdb_log_level level,
31 enum TDB_ERROR ecode,
32 const char *message,
33 struct ldb_context *ldb)
35 enum ldb_debug_level ldb_level;
36 const char *name = tdb_name(tdb);
38 switch (level) {
39 case TDB_LOG_WARNING:
40 ldb_level = LDB_DEBUG_WARNING;
41 case TDB_LOG_USE_ERROR:
42 case TDB_LOG_ERROR:
43 ldb_level = LDB_DEBUG_FATAL;
44 break;
45 default:
46 ldb_level = LDB_DEBUG_FATAL;
49 ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s: %s", name,
50 tdb_errorstr(ecode), message);
52 #else /* !TDB2 */
53 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
54 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
56 va_list ap;
57 const char *name = tdb_name(tdb);
58 struct ldb_context *ldb = talloc_get_type(tdb_get_logging_private(tdb), struct ldb_context);
59 enum ldb_debug_level ldb_level;
60 char *message;
62 if (ldb == NULL)
63 return;
65 va_start(ap, fmt);
66 message = talloc_vasprintf(ldb, fmt, ap);
67 va_end(ap);
69 switch (level) {
70 case TDB_DEBUG_FATAL:
71 ldb_level = LDB_DEBUG_FATAL;
72 break;
73 case TDB_DEBUG_ERROR:
74 ldb_level = LDB_DEBUG_ERROR;
75 break;
76 case TDB_DEBUG_WARNING:
77 ldb_level = LDB_DEBUG_WARNING;
78 break;
79 case TDB_DEBUG_TRACE:
80 ldb_level = LDB_DEBUG_TRACE;
81 break;
82 default:
83 ldb_level = LDB_DEBUG_FATAL;
86 ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
87 talloc_free(message);
89 #endif
92 the purpose of this code is to work around the braindead posix locking
93 rules, to allow us to have a ldb open more than once while allowing
94 locking to work
96 TDB2 handles multiple opens, so we don't have this problem there.
99 struct ltdb_wrap {
100 struct ltdb_wrap *next, *prev;
101 struct tdb_context *tdb;
102 dev_t device;
103 ino_t inode;
106 static struct ltdb_wrap *tdb_list;
108 /* destroy the last connection to a tdb */
109 static int ltdb_wrap_destructor(struct ltdb_wrap *w)
111 tdb_close(w->tdb);
112 DLIST_REMOVE(tdb_list, w);
113 return 0;
117 wrapped connection to a tdb database. The caller should _not_ free
118 this as it is not a talloc structure (as tdb does not use talloc
119 yet). It will auto-close when the caller frees the mem_ctx that is
120 passed to this call
122 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
123 const char *path, int hash_size,
124 int tdb_flags,
125 int open_flags, mode_t mode,
126 struct ldb_context *ldb)
128 struct ltdb_wrap *w;
129 struct stat st;
131 if (stat(path, &st) == 0) {
132 for (w=tdb_list;w;w=w->next) {
133 if (st.st_dev == w->device && st.st_ino == w->inode) {
134 if (!talloc_reference(mem_ctx, w)) {
135 return NULL;
137 return w->tdb;
142 w = talloc(mem_ctx, struct ltdb_wrap);
143 if (w == NULL) {
144 return NULL;
147 w->tdb = tdb_open_compat(path, hash_size, tdb_flags, open_flags, mode, ltdb_log_fn, ldb);
148 if (w->tdb == NULL) {
149 talloc_free(w);
150 return NULL;
153 if (fstat(tdb_fd(w->tdb), &st) != 0) {
154 tdb_close(w->tdb);
155 talloc_free(w);
156 return NULL;
159 w->device = st.st_dev;
160 w->inode = st.st_ino;
162 talloc_set_destructor(w, ltdb_wrap_destructor);
164 DLIST_ADD(tdb_list, w);
166 return w->tdb;