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
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/>.
25 #include "dlinklist.h"
27 /* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
29 static void ltdb_log_fn(struct tdb_context
*tdb
,
30 enum tdb_log_level level
,
33 struct ldb_context
*ldb
)
35 enum ldb_debug_level ldb_level
;
36 const char *name
= tdb_name(tdb
);
40 ldb_level
= LDB_DEBUG_WARNING
;
41 case TDB_LOG_USE_ERROR
:
43 ldb_level
= LDB_DEBUG_FATAL
;
46 ldb_level
= LDB_DEBUG_FATAL
;
49 ldb_debug(ldb
, ldb_level
, "ltdb: tdb(%s): %s: %s", name
,
50 tdb_errorstr(ecode
), message
);
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
, ...)
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
;
66 message
= talloc_vasprintf(ldb
, fmt
, ap
);
71 ldb_level
= LDB_DEBUG_FATAL
;
74 ldb_level
= LDB_DEBUG_ERROR
;
76 case TDB_DEBUG_WARNING
:
77 ldb_level
= LDB_DEBUG_WARNING
;
80 ldb_level
= LDB_DEBUG_TRACE
;
83 ldb_level
= LDB_DEBUG_FATAL
;
86 ldb_debug(ldb
, ldb_level
, "ltdb: tdb(%s): %s", name
, message
);
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
96 TDB2 handles multiple opens, so we don't have this problem there.
100 struct ltdb_wrap
*next
, *prev
;
101 struct tdb_context
*tdb
;
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
)
112 DLIST_REMOVE(tdb_list
, w
);
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
122 struct tdb_context
*ltdb_wrap_open(TALLOC_CTX
*mem_ctx
,
123 const char *path
, int hash_size
,
125 int open_flags
, mode_t mode
,
126 struct ldb_context
*ldb
)
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
)) {
142 w
= talloc(mem_ctx
, struct ltdb_wrap
);
147 w
->tdb
= tdb_open_compat(path
, hash_size
, tdb_flags
, open_flags
, mode
, ltdb_log_fn
, ldb
);
148 if (w
->tdb
== NULL
) {
153 if (fstat(tdb_fd(w
->tdb
), &st
) != 0) {
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
);