TDB2: make SAMBA use tdb1 again for the moment.
[asn/samba.git] / lib / tdb_wrap / tdb_wrap.c
1 /* 
2    Unix SMB/CIFS implementation.
3    TDB wrap functions
4
5    Copyright (C) Andrew Tridgell 2004
6    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/util/dlinklist.h"
24 #include "lib/tdb_wrap/tdb_wrap.h"
25 #include "lib/param/param.h"
26
27 /*
28  Log tdb messages via DEBUG().
29 */
30 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
31                          const char *format, ...) PRINTF_ATTRIBUTE(3,4);
32
33 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
34                          const char *format, ...)
35 {
36         va_list ap;
37         char *ptr = NULL;
38         int debuglevel = 0;
39         int ret;
40
41         switch (level) {
42         case TDB_DEBUG_FATAL:
43                 debuglevel = 0;
44                 break;
45         case TDB_DEBUG_ERROR:
46                 debuglevel = 1;
47                 break;
48         case TDB_DEBUG_WARNING:
49                 debuglevel = 2;
50                 break;
51         case TDB_DEBUG_TRACE:
52                 debuglevel = 5;
53                 break;
54         default:
55                 debuglevel = 0;
56         }               
57
58         va_start(ap, format);
59         ret = vasprintf(&ptr, format, ap);
60         va_end(ap);
61
62         if (ret != -1) {
63                 const char *name = tdb_name(tdb);
64                 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
65                 free(ptr);
66         }
67 }
68
69 struct tdb_wrap_private {
70         struct tdb_context *tdb;
71         const char *name;
72         struct tdb_wrap_private *next, *prev;
73 };
74
75 static struct tdb_wrap_private *tdb_list;
76
77 /* destroy the last connection to a tdb */
78 static int tdb_wrap_private_destructor(struct tdb_wrap_private *w)
79 {
80         tdb_close(w->tdb);
81         DLIST_REMOVE(tdb_list, w);
82         return 0;
83 }                                
84
85 static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
86                                                       const char *name,
87                                                       int hash_size,
88                                                       int tdb_flags,
89                                                       int open_flags,
90                                                       mode_t mode,
91                                                       struct loadparm_context *lp_ctx)
92 {
93         struct tdb_wrap_private *result;
94
95         result = talloc(mem_ctx, struct tdb_wrap_private);
96         if (result == NULL) {
97                 return NULL;
98         }
99         result->name = talloc_strdup(result, name);
100         if (result->name == NULL) {
101                 goto fail;
102         }
103
104         if (!lpcfg_use_mmap(lp_ctx)) {
105                 tdb_flags |= TDB_NOMMAP;
106         }
107
108         if ((hash_size == 0) && (name != NULL)) {
109                 const char *base;
110                 base = strrchr_m(name, '/');
111
112                 if (base != NULL) {
113                         base += 1;
114                 } else {
115                         base = name;
116                 }
117                 hash_size = lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
118         }
119
120         result->tdb = tdb_open_compat(name, hash_size, tdb_flags,
121                                       open_flags, mode, tdb_wrap_log, NULL);
122         if (result->tdb == NULL) {
123                 goto fail;
124         }
125         talloc_set_destructor(result, tdb_wrap_private_destructor);
126         DLIST_ADD(tdb_list, result);
127         return result;
128
129 fail:
130         TALLOC_FREE(result);
131         return NULL;
132 }
133
134 /*
135   wrapped connection to a tdb database
136   to close just talloc_free() the tdb_wrap pointer
137  */
138 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
139                                const char *name, int hash_size, int tdb_flags,
140                                int open_flags, mode_t mode,
141                                struct loadparm_context *lp_ctx)
142 {
143         struct tdb_wrap *result;
144         struct tdb_wrap_private *w;
145
146         result = talloc(mem_ctx, struct tdb_wrap);
147         if (result == NULL) {
148                 return NULL;
149         }
150
151         for (w=tdb_list;w;w=w->next) {
152                 if (strcmp(name, w->name) == 0) {
153                         break;
154                 }
155         }
156
157         if (w == NULL) {
158                 w = tdb_wrap_private_open(result, name, hash_size, tdb_flags,
159                                           open_flags, mode, lp_ctx);
160         } else {
161                 /*
162                  * Correctly use talloc_reference: The tdb will be
163                  * closed when "w" is being freed. The caller never
164                  * sees "w", so an incorrect use of talloc_free(w)
165                  * instead of calling talloc_unlink is not possible.
166                  * To avoid having to refcount ourselves, "w" will
167                  * have multiple parents that hang off all the
168                  * tdb_wrap's being returned from here. Those parents
169                  * can be freed without problem.
170                  */
171                 if (talloc_reference(result, w) == NULL) {
172                         goto fail;
173                 }
174         }
175         if (w == NULL) {
176                 goto fail;
177         }
178         result->tdb = w->tdb;
179         return result;
180 fail:
181         TALLOC_FREE(result);
182         return NULL;
183 }
184