s3-winbind: We need to use internal rpc connections in winbind.
[Samba/bjacke.git] / lib / tdb_compat / tdb_compat.h
blobea401cba498764d27a8cef86f6262cd641245dc1
1 /*
2 Unix SMB/CIFS implementation.
4 Compatibility layer for TDB1 vs TDB2.
6 Copyright (C) Rusty Russell 2011
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
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.
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.
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/>.
25 #ifndef TDB_COMPAT_H
26 #define TDB_COMPAT_H
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>
35 extern TDB_DATA tdb_null;
37 /* Old-style tdb_fetch. */
38 static inline TDB_DATA tdb_fetch_compat(struct tdb_context *tdb, TDB_DATA k)
40 TDB_DATA dbuf;
41 if (tdb_fetch(tdb, k, &dbuf) != TDB_SUCCESS) {
42 return tdb_null;
44 return dbuf;
47 static inline TDB_DATA tdb_firstkey_compat(struct tdb_context *tdb)
49 TDB_DATA k;
50 if (tdb_firstkey(tdb, &k) != TDB_SUCCESS) {
51 return tdb_null;
53 return k;
56 /* Note: this frees the old key.dptr. */
57 static inline TDB_DATA tdb_nextkey_compat(struct tdb_context *tdb, TDB_DATA k)
59 if (tdb_nextkey(tdb, &k) != TDB_SUCCESS) {
60 return tdb_null;
62 return k;
65 /* tdb_traverse_read and tdb_traverse are equal: both only take read locks. */
66 #define tdb_traverse_read tdb_traverse
68 /* Old-style tdb_errorstr */
69 #define tdb_errorstr_compat(tdb) tdb_errorstr(tdb_error(tdb))
71 /* This typedef doesn't exist in TDB2. */
72 typedef struct tdb_context TDB_CONTEXT;
74 /* We don't need these any more. */
75 #define tdb_reopen_all(flag) 0
76 #define tdb_reopen(tdb) 0
78 /* These no longer exist in tdb2. */
79 #define TDB_CLEAR_IF_FIRST 1048576
80 #define TDB_INCOMPATIBLE_HASH 0
81 #define TDB_VOLATILE 0
83 /* tdb2 does nonblocking functions via attibutes. */
84 enum TDB_ERROR tdb_transaction_start_nonblock(struct tdb_context *tdb);
86 /* Convenient (typesafe) wrapper for tdb open with logging */
87 #define tdb_open_compat(name, hsize, tdb_fl, open_fl, mode, log_fn, log_data) \
88 tdb_open_compat_((name), (hsize), (tdb_fl), (open_fl), (mode), \
89 typesafe_cb_preargs(void, void *, \
90 (log_fn), (log_data), \
91 struct tdb_context *, \
92 enum tdb_log_level, \
93 const char *), \
94 (log_data))
96 struct tdb_context *
97 tdb_open_compat_(const char *name, int hash_size_unused,
98 int tdb_flags, int open_flags, mode_t mode,
99 void (*log_fn)(struct tdb_context *,
100 enum tdb_log_level,
101 const char *message,
102 void *data),
103 void *log_data);
104 #else
105 #include <tdb.h>
107 /* FIXME: Inlining this is a bit lazy, but eases S3 build. */
108 static inline struct tdb_context *
109 tdb_open_compat(const char *name, int hash_size,
110 int tdb_flags, int open_flags, mode_t mode,
111 tdb_log_func log_fn, void *log_private)
113 struct tdb_logging_context lctx;
114 lctx.log_fn = log_fn;
115 lctx.log_private = log_private;
117 if (log_fn)
118 return tdb_open_ex(name, hash_size, tdb_flags, open_flags,
119 mode, &lctx, NULL);
120 else
121 return tdb_open(name, hash_size, tdb_flags, open_flags, mode);
124 #define tdb_firstkey_compat tdb_firstkey
125 /* Note: this frees the old key.dptr. */
126 static inline TDB_DATA tdb_nextkey_compat(struct tdb_context *tdb, TDB_DATA k)
128 TDB_DATA next = tdb_nextkey(tdb, k);
129 free(k.dptr);
130 return next;
132 #define tdb_errorstr_compat(tdb) tdb_errorstr(tdb)
133 #define tdb_fetch_compat tdb_fetch
134 #endif
136 #endif /* TDB_COMPAT_H */