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