r16916: Implement metze's proposed changes to the tdb logging API.
[metze/samba/wip.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 2 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 "includes.h"
26 #include "ldb/include/includes.h"
27
28 #include "ldb/ldb_tdb/ldb_tdb.h"
29
30 /*
31   the purpose of this code is to work around the braindead posix locking
32   rules, to allow us to have a ldb open more than once while allowing 
33   locking to work
34 */
35
36 struct ltdb_wrap {
37         struct ltdb_wrap *next, *prev;
38         struct tdb_context *tdb;
39         dev_t device;
40         ino_t inode;
41 };
42
43 static struct ltdb_wrap *tdb_list;
44
45 /* destroy the last connection to a tdb */
46 static int ltdb_wrap_destructor(struct ltdb_wrap *w)
47 {
48         tdb_close(w->tdb);
49         if (w->next) {
50                 w->next->prev = w->prev;
51         }
52         if (w->prev) {
53                 w->prev->next = w->next;
54         }
55         if (w == tdb_list) {
56                 tdb_list = w->next;
57         }
58         return 0;
59 }                                
60
61 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
62 static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
63 {
64         va_list ap;
65         const char *name = tdb_name(tdb);
66         struct ldb_context *ldb = talloc_get_type(tdb_get_logging_private(tdb), struct ldb_context);
67         enum ldb_debug_level ldb_level;
68         char *message; 
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 /*
96   wrapped connection to a tdb database. The caller should _not_ free
97   this as it is not a talloc structure (as tdb does not use talloc
98   yet). It will auto-close when the caller frees the mem_ctx that is
99   passed to this call
100  */
101 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
102                                    const char *path, int hash_size, 
103                                    int tdb_flags,
104                                    int open_flags, mode_t mode, 
105                                    struct ldb_context *ldb)
106 {
107         struct ltdb_wrap *w;
108         struct stat st;
109         struct tdb_logging_context log_ctx;
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                                 talloc_reference(mem_ctx, w);
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