s3-ldb: update the old ldb in s3 to use new DLIST macros
[Samba/gebeck_regimport.git] / source3 / lib / ldb / ldb_tdb / ldb_tdb_wrap.c
blob64463f7c910244391009db16262672db847f16c7
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 "includes.h"
25 #include "ldb/include/includes.h"
27 #include "ldb/ldb_tdb/ldb_tdb.h"
30 the purpose of this code is to work around the braindead posix locking
31 rules, to allow us to have a ldb open more than once while allowing
32 locking to work
35 struct ltdb_wrap {
36 struct ltdb_wrap *next, *prev;
37 struct tdb_context *tdb;
38 dev_t device;
39 ino_t inode;
42 static struct ltdb_wrap *tdb_list;
44 /* destroy the last connection to a tdb */
45 static int ltdb_wrap_destructor(struct ltdb_wrap *w)
47 tdb_close(w->tdb);
48 DLIST_REMOVE(tdb_list, w);
49 return 0;
52 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
53 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
55 va_list ap;
56 const char *name = tdb_name(tdb);
57 struct ldb_context *ldb = talloc_get_type(tdb_get_logging_private(tdb), struct ldb_context);
58 enum ldb_debug_level ldb_level;
59 char *message;
60 va_start(ap, fmt);
61 message = talloc_vasprintf(ldb, fmt, ap);
62 va_end(ap);
64 switch (level) {
65 case TDB_DEBUG_FATAL:
66 ldb_level = LDB_DEBUG_FATAL;
67 break;
68 case TDB_DEBUG_ERROR:
69 ldb_level = LDB_DEBUG_ERROR;
70 break;
71 case TDB_DEBUG_WARNING:
72 ldb_level = LDB_DEBUG_WARNING;
73 break;
74 case TDB_DEBUG_TRACE:
75 ldb_level = LDB_DEBUG_TRACE;
76 break;
77 default:
78 ldb_level = LDB_DEBUG_FATAL;
81 ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
82 talloc_free(message);
86 wrapped connection to a tdb database. The caller should _not_ free
87 this as it is not a talloc structure (as tdb does not use talloc
88 yet). It will auto-close when the caller frees the mem_ctx that is
89 passed to this call
91 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
92 const char *path, int hash_size,
93 int tdb_flags,
94 int open_flags, mode_t mode,
95 struct ldb_context *ldb)
97 struct ltdb_wrap *w;
98 struct stat st;
99 struct tdb_logging_context log_ctx;
101 log_ctx.log_fn = ltdb_log_fn;
102 log_ctx.log_private = ldb;
104 if (stat(path, &st) == 0) {
105 for (w=tdb_list;w;w=w->next) {
106 if (st.st_dev == w->device && st.st_ino == w->inode) {
107 if (!talloc_reference(mem_ctx, w)) {
108 return NULL;
110 return w->tdb;
115 w = talloc(mem_ctx, struct ltdb_wrap);
116 if (w == NULL) {
117 return NULL;
120 w->tdb = tdb_open_ex(path, hash_size, tdb_flags, open_flags, mode, &log_ctx, NULL);
121 if (w->tdb == NULL) {
122 talloc_free(w);
123 return NULL;
126 if (fstat(tdb_fd(w->tdb), &st) != 0) {
127 tdb_close(w->tdb);
128 talloc_free(w);
129 return NULL;
132 w->device = st.st_dev;
133 w->inode = st.st_ino;
135 talloc_set_destructor(w, ltdb_wrap_destructor);
137 DLIST_ADD(tdb_list, w);
139 return w->tdb;