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