014a756d6e3ac258359747aab4c92b18f2e803a3
[ddiss/samba.git] / lib / ldb / ldb_tdb / ldb_tdb_wrap.c
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 #include "dlinklist.h"
26
27 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
28 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
29 {
30         va_list ap;
31         const char *name = tdb_name(tdb);
32         struct ldb_context *ldb = talloc_get_type(tdb_get_logging_private(tdb), struct ldb_context);
33         enum ldb_debug_level ldb_level;
34         char *message; 
35
36         if (ldb == NULL)
37                 return;
38
39         va_start(ap, fmt);
40         message = talloc_vasprintf(ldb, fmt, ap);
41         va_end(ap);
42
43         switch (level) {
44         case TDB_DEBUG_FATAL:
45                 ldb_level = LDB_DEBUG_FATAL;
46                 break;
47         case TDB_DEBUG_ERROR:
48                 ldb_level = LDB_DEBUG_ERROR;
49                 break;
50         case TDB_DEBUG_WARNING:
51                 ldb_level = LDB_DEBUG_WARNING;
52                 break;
53         case TDB_DEBUG_TRACE:
54                 ldb_level = LDB_DEBUG_TRACE;
55                 break;
56         default:
57                 ldb_level = LDB_DEBUG_FATAL;
58         }
59
60         ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
61         talloc_free(message);
62 }
63
64 /*
65   the purpose of this code is to work around the braindead posix locking
66   rules, to allow us to have a ldb open more than once while allowing
67   locking to work
68
69   TDB2 handles multiple opens, so we don't have this problem there.
70 */
71
72 struct ltdb_wrap {
73         struct ltdb_wrap *next, *prev;
74         struct tdb_context *tdb;
75         dev_t device;
76         ino_t inode;
77 };
78
79 static struct ltdb_wrap *tdb_list;
80
81 /* destroy the last connection to a tdb */
82 static int ltdb_wrap_destructor(struct ltdb_wrap *w)
83 {
84         tdb_close(w->tdb);
85         DLIST_REMOVE(tdb_list, w);
86         return 0;
87 }
88
89 /*
90   wrapped connection to a tdb database. The caller should _not_ free
91   this as it is not a talloc structure (as tdb does not use talloc
92   yet). It will auto-close when the caller frees the mem_ctx that is
93   passed to this call
94  */
95 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
96                                    const char *path, int hash_size, 
97                                    int tdb_flags,
98                                    int open_flags, mode_t mode, 
99                                    struct ldb_context *ldb)
100 {
101         struct ltdb_wrap *w;
102         struct stat st;
103
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;
109                                 }
110                                 return w->tdb;
111                         }
112                 }
113         }
114
115         w = talloc(mem_ctx, struct ltdb_wrap);
116         if (w == NULL) {
117                 return NULL;
118         }
119
120         w->tdb = tdb_open_compat(path, hash_size, tdb_flags, open_flags, mode, ltdb_log_fn, ldb);
121         if (w->tdb == NULL) {
122                 talloc_free(w);
123                 return NULL;
124         }
125
126         if (fstat(tdb_fd(w->tdb), &st) != 0) {
127                 tdb_close(w->tdb);
128                 talloc_free(w);
129                 return NULL;
130         }
131
132         w->device = st.st_dev;
133         w->inode  = st.st_ino;
134
135         talloc_set_destructor(w, ltdb_wrap_destructor);
136
137         DLIST_ADD(tdb_list, w);
138         
139         return w->tdb;
140 }