/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- ltdb_wrap_destructor
- ltdb_log_fn
- ltdb_wrap_open
1 /*
2 ldb database library
3
4 Copyright (C) Andrew Tridgell 2005
5
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
9
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.
14
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.
19
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/>.
22 */
23
24 #include "ldb_tdb.h"
25
26 /*
27 the purpose of this code is to work around the braindead posix locking
28 rules, to allow us to have a ldb open more than once while allowing
29 locking to work
30 */
31
32 struct ltdb_wrap {
33 struct ltdb_wrap *next, *prev;
34 struct tdb_context *tdb;
35 dev_t device;
36 ino_t inode;
37 };
38
39 static struct ltdb_wrap *tdb_list;
40
41 /* destroy the last connection to a tdb */
42 static int ltdb_wrap_destructor(struct ltdb_wrap *w)
/* [<][>][^][v][top][bottom][index][help] */
43 {
44 tdb_close(w->tdb);
45 if (w->next) {
46 w->next->prev = w->prev;
47 }
48 if (w->prev) {
49 w->prev->next = w->next;
50 }
51 if (w == tdb_list) {
52 tdb_list = w->next;
53 }
54 return 0;
55 }
56
57 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
/* [<][>][^][v][top][bottom][index][help] */
58 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
59 {
60 va_list ap;
61 const char *name = tdb_name(tdb);
62 struct ldb_context *ldb = talloc_get_type(tdb_get_logging_private(tdb), struct ldb_context);
63 enum ldb_debug_level ldb_level;
64 char *message;
65
66 if (ldb == NULL)
67 return;
68
69 va_start(ap, fmt);
70 message = talloc_vasprintf(ldb, fmt, ap);
71 va_end(ap);
72
73 switch (level) {
74 case TDB_DEBUG_FATAL:
75 ldb_level = LDB_DEBUG_FATAL;
76 break;
77 case TDB_DEBUG_ERROR:
78 ldb_level = LDB_DEBUG_ERROR;
79 break;
80 case TDB_DEBUG_WARNING:
81 ldb_level = LDB_DEBUG_WARNING;
82 break;
83 case TDB_DEBUG_TRACE:
84 ldb_level = LDB_DEBUG_TRACE;
85 break;
86 default:
87 ldb_level = LDB_DEBUG_FATAL;
88 }
89
90 ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
91 talloc_free(message);
92 }
93
94 /*
95 wrapped connection to a tdb database. The caller should _not_ free
96 this as it is not a talloc structure (as tdb does not use talloc
97 yet). It will auto-close when the caller frees the mem_ctx that is
98 passed to this call
99 */
100 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
/* [<][>][^][v][top][bottom][index][help] */
101 const char *path, int hash_size,
102 int tdb_flags,
103 int open_flags, mode_t mode,
104 struct ldb_context *ldb)
105 {
106 struct ltdb_wrap *w;
107 struct stat st;
108 struct tdb_logging_context log_ctx;
109
110 log_ctx.log_fn = ltdb_log_fn;
111 log_ctx.log_private = ldb;
112
113 if (stat(path, &st) == 0) {
114 for (w=tdb_list;w;w=w->next) {
115 if (st.st_dev == w->device && st.st_ino == w->inode) {
116 if (!talloc_reference(mem_ctx, w)) {
117 return NULL;
118 }
119 return w->tdb;
120 }
121 }
122 }
123
124 w = talloc(mem_ctx, struct ltdb_wrap);
125 if (w == NULL) {
126 return NULL;
127 }
128
129 w->tdb = tdb_open_ex(path, hash_size, tdb_flags, open_flags, mode, &log_ctx, NULL);
130 if (w->tdb == NULL) {
131 talloc_free(w);
132 return NULL;
133 }
134
135 if (fstat(tdb_fd(w->tdb), &st) != 0) {
136 tdb_close(w->tdb);
137 talloc_free(w);
138 return NULL;
139 }
140
141 w->device = st.st_dev;
142 w->inode = st.st_ino;
143
144 talloc_set_destructor(w, ltdb_wrap_destructor);
145
146 w->next = tdb_list;
147 w->prev = NULL;
148 if (tdb_list) {
149 tdb_list->prev = w;
150 }
151 tdb_list = w;
152
153 return w->tdb;
154 }
155