5 Unix SMB/CIFS implementation.
7 trivial database library
9 Copyright (C) Andrew Tridgell 1999-2004
11 ** NOTE! The following LGPL license applies to the tdb
12 ** library. This does NOT imply that all of Samba is released
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 3 of the License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, see <http://www.gnu.org/licenses/>.
35 /* Samba sets hidden attribute when building libraries: we don't. */
40 /* flags to tdb_store() */
41 #define TDB_REPLACE 1 /* Unused */
42 #define TDB_INSERT 2 /* Don't overwrite an existing entry */
43 #define TDB_MODIFY 3 /* Don't create an existing entry */
45 /* flags for tdb_open() */
46 #define TDB_DEFAULT 0 /* just a readability place holder */
47 #define TDB_CLEAR_IF_FIRST 1
48 #define TDB_INTERNAL 2 /* don't store on disk */
49 #define TDB_NOLOCK 4 /* don't do any locking */
50 #define TDB_NOMMAP 8 /* don't use mmap */
51 #define TDB_CONVERT 16 /* convert endian (internal use) */
52 #define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
53 #define TDB_NOSYNC 64 /* don't use synchronous transactions */
54 #define TDB_SEQNUM 128 /* maintain a sequence number */
55 #define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */
56 #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
57 #define TDB_DISALLOW_NESTING 1024 /* Disallow transactions to nest */
60 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
61 TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
62 TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
65 /* debugging uses one of the following levels */
66 enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
67 TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
69 typedef struct TDB_DATA {
74 #ifndef PRINTF_ATTRIBUTE
76 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
77 * the parameter containing the format, and a2 the index of the first
78 * argument. Note that some gcc 2.x versions don't handle this
80 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
82 #define PRINTF_ATTRIBUTE(a1, a2)
86 /* this is the context structure that is returned from a db open */
87 typedef struct tdb_context TDB_CONTEXT;
89 typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
90 typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
91 typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
93 struct tdb_logging_context {
98 _PUBLIC_ struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
99 int open_flags, mode_t mode);
100 _PUBLIC_ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
101 int open_flags, mode_t mode,
102 const struct tdb_logging_context *log_ctx,
103 tdb_hash_func hash_fn);
104 _PUBLIC_ void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
106 _PUBLIC_ int tdb_reopen(struct tdb_context *tdb);
107 _PUBLIC_ int tdb_reopen_all(int parent_longlived);
108 _PUBLIC_ void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
109 _PUBLIC_ enum TDB_ERROR tdb_error(struct tdb_context *tdb);
110 _PUBLIC_ const char *tdb_errorstr(struct tdb_context *tdb);
111 _PUBLIC_ TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
112 _PUBLIC_ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
113 int (*parser)(TDB_DATA key, TDB_DATA data,
116 _PUBLIC_ int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
117 _PUBLIC_ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
118 _PUBLIC_ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
119 _PUBLIC_ int tdb_close(struct tdb_context *tdb);
120 _PUBLIC_ TDB_DATA tdb_firstkey(struct tdb_context *tdb);
121 _PUBLIC_ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
122 _PUBLIC_ int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *);
123 _PUBLIC_ int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *);
124 _PUBLIC_ int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
125 _PUBLIC_ int tdb_lockall(struct tdb_context *tdb);
126 _PUBLIC_ int tdb_lockall_nonblock(struct tdb_context *tdb);
127 _PUBLIC_ int tdb_unlockall(struct tdb_context *tdb);
128 _PUBLIC_ int tdb_lockall_read(struct tdb_context *tdb);
129 _PUBLIC_ int tdb_lockall_read_nonblock(struct tdb_context *tdb);
130 _PUBLIC_ int tdb_unlockall_read(struct tdb_context *tdb);
131 _PUBLIC_ int tdb_lockall_mark(struct tdb_context *tdb);
132 _PUBLIC_ int tdb_lockall_unmark(struct tdb_context *tdb);
133 _PUBLIC_ const char *tdb_name(struct tdb_context *tdb);
134 _PUBLIC_ int tdb_fd(struct tdb_context *tdb);
135 _PUBLIC_ tdb_log_func tdb_log_fn(struct tdb_context *tdb);
136 _PUBLIC_ void *tdb_get_logging_private(struct tdb_context *tdb);
137 _PUBLIC_ int tdb_transaction_start(struct tdb_context *tdb);
138 _PUBLIC_ int tdb_transaction_start_nonblock(struct tdb_context *tdb);
139 _PUBLIC_ int tdb_transaction_prepare_commit(struct tdb_context *tdb);
140 _PUBLIC_ int tdb_transaction_commit(struct tdb_context *tdb);
141 _PUBLIC_ int tdb_transaction_cancel(struct tdb_context *tdb);
142 _PUBLIC_ int tdb_get_seqnum(struct tdb_context *tdb);
143 _PUBLIC_ int tdb_hash_size(struct tdb_context *tdb);
144 _PUBLIC_ size_t tdb_map_size(struct tdb_context *tdb);
145 _PUBLIC_ int tdb_get_flags(struct tdb_context *tdb);
146 _PUBLIC_ void tdb_add_flags(struct tdb_context *tdb, unsigned flag);
147 _PUBLIC_ void tdb_remove_flags(struct tdb_context *tdb, unsigned flag);
148 _PUBLIC_ void tdb_enable_seqnum(struct tdb_context *tdb);
149 _PUBLIC_ void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
150 _PUBLIC_ unsigned int tdb_jenkins_hash(TDB_DATA *key);
151 _PUBLIC_ int tdb_check(struct tdb_context *tdb,
152 int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
155 /* Low level locking functions: use with care */
156 _PUBLIC_ int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
157 _PUBLIC_ int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key);
158 _PUBLIC_ int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
159 _PUBLIC_ int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
160 _PUBLIC_ int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
161 _PUBLIC_ int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key);
162 _PUBLIC_ int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key);
164 _PUBLIC_ void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *sigptr);
166 /* wipe and repack */
167 _PUBLIC_ int tdb_wipe_all(struct tdb_context *tdb);
168 _PUBLIC_ int tdb_repack(struct tdb_context *tdb);
170 /* Debug functions. Not used in production. */
171 _PUBLIC_ void tdb_dump_all(struct tdb_context *tdb);
172 _PUBLIC_ int tdb_printfreelist(struct tdb_context *tdb);
173 _PUBLIC_ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
174 _PUBLIC_ int tdb_freelist_size(struct tdb_context *tdb);
176 _PUBLIC_ extern TDB_DATA tdb_null;