tdb_compat: support tdb_reopen/tdb_reopen_all for TDB2
[rusty/samba.git] / lib / tdb_compat / tdb_compat.h
1 /*
2    Unix SMB/CIFS implementation.
3
4    Compatibility layer for TDB1 vs TDB2.
5
6    Copyright (C) Rusty Russell 2011
7
8      ** NOTE! The following LGPL license applies to the tdb_compat
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 */
25 #ifndef TDB_COMPAT_H
26 #define TDB_COMPAT_H
27
28 #include "replace.h"
29 #include <ccan/typesafe_cb/typesafe_cb.h>
30 #if BUILD_TDB2
31 #include <tdb2.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 extern TDB_DATA tdb_null;
36
37 /* Old-style tdb_fetch. */
38 static inline TDB_DATA tdb_fetch_compat(struct tdb_context *tdb, TDB_DATA k)
39 {
40         TDB_DATA dbuf;
41         if (tdb_fetch(tdb, k, &dbuf) != TDB_SUCCESS) {
42                 return tdb_null;
43         }
44         return dbuf;
45 }
46
47 static inline TDB_DATA tdb_firstkey_compat(struct tdb_context *tdb)
48 {
49         TDB_DATA k;
50         if (tdb_firstkey(tdb, &k) != TDB_SUCCESS) {
51                 return tdb_null;
52         }
53         return k;
54 }
55
56 /* Note: this frees the old key.dptr. */
57 static inline TDB_DATA tdb_nextkey_compat(struct tdb_context *tdb, TDB_DATA k)
58 {
59         if (tdb_nextkey(tdb, &k) != TDB_SUCCESS) {
60                 return tdb_null;
61         }
62         return k;
63 }
64
65 #define tdb_traverse_read(tdb, fn, p)                                   \
66         tdb_traverse_read_(tdb, typesafe_cb_preargs(int, void *, (fn), (p), \
67                                                     struct tdb_context *, \
68                                                     TDB_DATA, TDB_DATA), (p))
69 int64_t tdb_traverse_read_(struct tdb_context *tdb,
70                            int (*fn)(struct tdb_context *,
71                                      TDB_DATA, TDB_DATA, void *), void *p);
72
73 /* Old-style tdb_errorstr */
74 #define tdb_errorstr_compat(tdb) tdb_errorstr(tdb_error(tdb))
75
76 /* This typedef doesn't exist in TDB2. */
77 typedef struct tdb_context TDB_CONTEXT;
78
79 /* We only need these for the CLEAR_IF_FIRST lock. */
80 int tdb_reopen(struct tdb_context *tdb);
81 int tdb_reopen_all(int parent_longlived);
82
83 /* These no longer exist in tdb2. */
84 #define TDB_CLEAR_IF_FIRST 1048576
85 #define TDB_INCOMPATIBLE_HASH 0
86 #define TDB_VOLATILE 0
87
88 /* tdb2 does nonblocking functions via attibutes. */
89 enum TDB_ERROR tdb_transaction_start_nonblock(struct tdb_context *tdb);
90
91 /* Convenient (typesafe) wrapper for tdb open with logging */
92 #define tdb_open_compat(name, hsize, tdb_fl, open_fl, mode, log_fn, log_data) \
93         tdb_open_compat_((name), (hsize), (tdb_fl), (open_fl), (mode),  \
94                          typesafe_cb_preargs(void, void *,              \
95                                              (log_fn), (log_data),      \
96                                              struct tdb_context *,      \
97                                              enum tdb_log_level,        \
98                                              enum TDB_ERROR,            \
99                                              const char *),             \
100                          (log_data))
101
102 struct tdb_context *
103 tdb_open_compat_(const char *name, int hash_size_unused,
104                  int tdb_flags, int open_flags, mode_t mode,
105                  void (*log_fn)(struct tdb_context *,
106                                 enum tdb_log_level,
107                                 enum TDB_ERROR ecode,
108                                 const char *message,
109                                 void *data),
110                  void *log_data);
111 #else
112 #include <tdb.h>
113
114 /* FIXME: Inlining this is a bit lazy, but eases S3 build. */
115 static inline struct tdb_context *
116 tdb_open_compat(const char *name, int hash_size,
117                 int tdb_flags, int open_flags, mode_t mode,
118                 tdb_log_func log_fn, void *log_private)
119 {
120         struct tdb_logging_context lctx;
121         lctx.log_fn = log_fn;
122         lctx.log_private = log_private;
123
124         if (log_fn)
125                 return tdb_open_ex(name, hash_size, tdb_flags, open_flags,
126                                    mode, &lctx, NULL);
127         else
128                 return tdb_open(name, hash_size, tdb_flags, open_flags, mode);
129 }
130
131 #define tdb_firstkey_compat tdb_firstkey
132 /* Note: this frees the old key.dptr. */
133 static inline TDB_DATA tdb_nextkey_compat(struct tdb_context *tdb, TDB_DATA k)
134 {
135         TDB_DATA next = tdb_nextkey(tdb, k);
136         free(k.dptr);
137         return next;
138 }
139 #define tdb_errorstr_compat(tdb) tdb_errorstr(tdb)
140 #define tdb_fetch_compat tdb_fetch
141 #endif
142
143 #endif /* TDB_COMPAT_H */