5 Unix SMB/CIFS implementation.
7 trivial database library (version 1 compat functions)
9 Copyright (C) Andrew Tridgell 1999-2004
10 Copyright (C) Rusty Russell 2011
12 ** NOTE! The following LGPL license applies to the tdb
13 ** library. This does NOT imply that all of Samba is released
16 This library is free software; you can redistribute it and/or
17 modify it under the terms of the GNU Lesser General Public
18 License as published by the Free Software Foundation; either
19 version 3 of the License, or (at your option) any later version.
21 This library is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 Lesser General Public License for more details.
26 You should have received a copy of the GNU Lesser General Public
27 License along with this library; if not, see <http://www.gnu.org/licenses/>.
32 #include <sys/types.h>
35 /* For sig_atomic_t. */
39 /** Flags to tdb1_store() */
40 #define TDB1_REPLACE 1 /** Unused */
41 #define TDB1_INSERT 2 /** Don't overwrite an existing entry */
42 #define TDB1_MODIFY 3 /** Don't create an existing entry */
44 /** Flags for tdb1_open() */
45 #define TDB1_DEFAULT 0 /** just a readability place holder */
46 #define TDB1_CLEAR_IF_FIRST 1 /** If this is the first open, wipe the db */
47 #define TDB1_INTERNAL 2 /** Don't store on disk */
48 #define TDB1_NOLOCK 4 /** Don't do any locking */
49 #define TDB1_NOMMAP 8 /** Don't use mmap */
50 #define TDB1_CONVERT 16 /** Convert endian (internal use) */
51 #define TDB1_BIGENDIAN 32 /** Header is big-endian (internal use) */
52 #define TDB1_NOSYNC 64 /** Don't use synchronous transactions */
53 #define TDB1_SEQNUM 128 /** Maintain a sequence number */
54 #define TDB1_VOLATILE 256 /** Activate the per-hashchain freelist, default 5 */
55 #define TDB1_ALLOW_NESTING 512 /** Allow transactions to nest */
56 #define TDB1_DISALLOW_NESTING 1024 /** Disallow transactions to nest */
57 #define TDB1_INCOMPATIBLE_HASH 2048 /** Better hashing: can't be opened by tdb < 1.2.6. */
59 /** The tdb error codes */
60 enum TDB1_ERROR {TDB1_SUCCESS=0, TDB1_ERR_CORRUPT, TDB1_ERR_IO, TDB1_ERR_LOCK,
61 TDB1_ERR_OOM, TDB1_ERR_EXISTS, TDB1_ERR_NOLOCK, TDB1_ERR_LOCK_TIMEOUT,
62 TDB1_ERR_NOEXIST, TDB1_ERR_EINVAL, TDB1_ERR_RDONLY,
65 /** Debugging uses one of the following levels */
66 enum tdb1_debug_level {TDB1_DEBUG_FATAL = 0, TDB1_DEBUG_ERROR,
67 TDB1_DEBUG_WARNING, TDB1_DEBUG_TRACE};
69 /** The tdb data structure */
70 typedef struct TDB1_DATA {
75 #ifndef PRINTF_ATTRIBUTE
77 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
78 * the parameter containing the format, and a2 the index of the first
79 * argument. Note that some gcc 2.x versions don't handle this
81 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
83 #define PRINTF_ATTRIBUTE(a1, a2)
87 /** This is the context structure that is returned from a db open. */
88 typedef struct tdb1_context TDB1_CONTEXT;
90 typedef int (*tdb1_traverse_func)(struct tdb1_context *, TDB1_DATA, TDB1_DATA, void *);
91 typedef void (*tdb1_log_func)(struct tdb1_context *, enum tdb1_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
92 typedef unsigned int (*tdb1_hash_func)(TDB1_DATA *key);
94 struct tdb1_logging_context {
99 struct tdb1_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
100 int open_flags, mode_t mode);
102 struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
103 int open_flags, mode_t mode,
104 const struct tdb1_logging_context *log_ctx,
105 tdb1_hash_func hash_fn);
107 void tdb1_set_max_dead(struct tdb1_context *tdb, int max_dead);
109 int tdb1_reopen(struct tdb1_context *tdb);
111 int tdb1_reopen_all(int parent_longlived);
113 void tdb1_set_logging_function(struct tdb1_context *tdb, const struct tdb1_logging_context *log_ctx);
115 enum TDB1_ERROR tdb1_error(struct tdb1_context *tdb);
117 const char *tdb1_errorstr(struct tdb1_context *tdb);
119 TDB1_DATA tdb1_fetch(struct tdb1_context *tdb, TDB1_DATA key);
121 int tdb1_parse_record(struct tdb1_context *tdb, TDB1_DATA key,
122 int (*parser)(TDB1_DATA key, TDB1_DATA data,
126 int tdb1_delete(struct tdb1_context *tdb, TDB1_DATA key);
128 int tdb1_store(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA dbuf, int flag);
130 int tdb1_append(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA new_dbuf);
132 int tdb1_close(struct tdb1_context *tdb);
134 TDB1_DATA tdb1_firstkey(struct tdb1_context *tdb);
136 TDB1_DATA tdb1_nextkey(struct tdb1_context *tdb, TDB1_DATA key);
138 int tdb1_traverse(struct tdb1_context *tdb, tdb1_traverse_func fn, void *private_data);
140 int tdb1_traverse_read(struct tdb1_context *tdb, tdb1_traverse_func fn, void *private_data);
142 int tdb1_exists(struct tdb1_context *tdb, TDB1_DATA key);
144 int tdb1_lockall(struct tdb1_context *tdb);
146 int tdb1_lockall_nonblock(struct tdb1_context *tdb);
148 int tdb1_unlockall(struct tdb1_context *tdb);
150 int tdb1_lockall_read(struct tdb1_context *tdb);
152 int tdb1_lockall_read_nonblock(struct tdb1_context *tdb);
154 int tdb1_unlockall_read(struct tdb1_context *tdb);
156 int tdb1_lockall_mark(struct tdb1_context *tdb);
158 int tdb1_lockall_unmark(struct tdb1_context *tdb);
160 const char *tdb1_name(struct tdb1_context *tdb);
162 int tdb1_fd(struct tdb1_context *tdb);
164 tdb1_log_func tdb1_log_fn(struct tdb1_context *tdb);
166 void *tdb1_get_logging_private(struct tdb1_context *tdb);
168 int tdb1_transaction_start(struct tdb1_context *tdb);
170 int tdb1_transaction_start_nonblock(struct tdb1_context *tdb);
172 int tdb1_transaction_prepare_commit(struct tdb1_context *tdb);
174 int tdb1_transaction_commit(struct tdb1_context *tdb);
176 int tdb1_transaction_cancel(struct tdb1_context *tdb);
178 int tdb1_get_seqnum(struct tdb1_context *tdb);
180 int tdb1_hash_size(struct tdb1_context *tdb);
182 size_t tdb1_map_size(struct tdb1_context *tdb);
184 int tdb1_get_flags(struct tdb1_context *tdb);
186 void tdb1_add_flags(struct tdb1_context *tdb, unsigned flag);
188 void tdb1_remove_flags(struct tdb1_context *tdb, unsigned flag);
190 void tdb1_enable_seqnum(struct tdb1_context *tdb);
192 void tdb1_increment_seqnum_nonblock(struct tdb1_context *tdb);
194 unsigned int tdb1_jenkins_hash(TDB1_DATA *key);
196 int tdb1_check(struct tdb1_context *tdb,
197 int (*check) (TDB1_DATA key, TDB1_DATA data, void *private_data),
200 /* @} ******************************************************************/
202 /* Low level locking functions: use with care */
203 int tdb1_chainlock(struct tdb1_context *tdb, TDB1_DATA key);
204 int tdb1_chainlock_nonblock(struct tdb1_context *tdb, TDB1_DATA key);
205 int tdb1_chainunlock(struct tdb1_context *tdb, TDB1_DATA key);
206 int tdb1_chainlock_read(struct tdb1_context *tdb, TDB1_DATA key);
207 int tdb1_chainunlock_read(struct tdb1_context *tdb, TDB1_DATA key);
208 int tdb1_chainlock_mark(struct tdb1_context *tdb, TDB1_DATA key);
209 int tdb1_chainlock_unmark(struct tdb1_context *tdb, TDB1_DATA key);
211 void tdb1_setalarm_sigptr(struct tdb1_context *tdb, volatile sig_atomic_t *sigptr);
213 /* wipe and repack */
214 int tdb1_wipe_all(struct tdb1_context *tdb);
215 int tdb1_repack(struct tdb1_context *tdb);
217 /* Debug functions. Not used in production. */
218 void tdb1_dump_all(struct tdb1_context *tdb);
219 int tdb1_printfreelist(struct tdb1_context *tdb);
220 int tdb1_validate_freelist(struct tdb1_context *tdb, int *pnum_entries);
221 int tdb1_freelist_size(struct tdb1_context *tdb);
222 char *tdb1_summary(struct tdb1_context *tdb);
224 extern TDB1_DATA tdb1_null;