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 #include <sys/types.h>
38 /* For sig_atomic_t. */
43 #include <ccan/compiler/compiler.h>
45 /* flags to tdb_store() */
46 #define TDB_REPLACE 1 /* Unused */
47 #define TDB_INSERT 2 /* Don't overwrite an existing entry */
48 #define TDB_MODIFY 3 /* Don't create an existing entry */
50 /* flags for tdb_open() */
51 #define TDB_DEFAULT 0 /* just a readability place holder */
52 #define TDB_CLEAR_IF_FIRST 1
53 #define TDB_INTERNAL 2 /* don't store on disk */
54 #define TDB_NOLOCK 4 /* don't do any locking */
55 #define TDB_NOMMAP 8 /* don't use mmap */
56 #define TDB_CONVERT 16 /* convert endian */
57 #define TDB_NOSYNC 64 /* don't use synchronous transactions */
58 #define TDB_SEQNUM 128 /* maintain a sequence number */
59 #define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */
60 #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
63 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
64 TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOEXIST,
65 TDB_ERR_EINVAL, TDB_ERR_RDONLY, TDB_ERR_NESTING };
67 /* flags for tdb_summary. Logical or to combine. */
68 enum tdb_summary_flags { TDB_SUMMARY_HISTOGRAMS = 1 };
70 /* debugging uses one of the following levels */
71 enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
72 TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
74 typedef struct tdb_data {
81 /* FIXME: Make typesafe */
82 typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
83 typedef void (*tdb_logfn_t)(struct tdb_context *, enum tdb_debug_level, void *priv, const char *, ...) PRINTF_FMT(4, 5);
84 typedef uint64_t (*tdb_hashfn_t)(const void *key, size_t len, uint64_t seed,
87 enum tdb_attribute_type {
88 TDB_ATTRIBUTE_LOG = 0,
89 TDB_ATTRIBUTE_HASH = 1,
90 TDB_ATTRIBUTE_SEED = 2
93 struct tdb_attribute_base {
94 enum tdb_attribute_type attr;
95 union tdb_attribute *next;
98 struct tdb_attribute_log {
99 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
104 struct tdb_attribute_hash {
105 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
106 tdb_hashfn_t hash_fn;
110 struct tdb_attribute_seed {
111 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_SEED */
115 union tdb_attribute {
116 struct tdb_attribute_base base;
117 struct tdb_attribute_log log;
118 struct tdb_attribute_hash hash;
119 struct tdb_attribute_seed seed;
122 struct tdb_context *tdb_open(const char *name, int tdb_flags,
123 int open_flags, mode_t mode,
124 union tdb_attribute *attributes);
126 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key);
127 int tdb_delete(struct tdb_context *tdb, struct tdb_data key);
128 int tdb_store(struct tdb_context *tdb, struct tdb_data key, struct tdb_data dbuf, int flag);
129 int tdb_append(struct tdb_context *tdb, struct tdb_data key, struct tdb_data dbuf);
130 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
131 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
132 int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p);
133 int64_t tdb_traverse_read(struct tdb_context *tdb,
134 tdb_traverse_func fn, void *p);
135 TDB_DATA tdb_firstkey(struct tdb_context *tdb);
136 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
137 int tdb_close(struct tdb_context *tdb);
138 int tdb_check(struct tdb_context *tdb,
139 int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
142 enum TDB_ERROR tdb_error(struct tdb_context *tdb);
143 const char *tdb_errorstr(struct tdb_context *tdb);
145 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags);
147 extern struct tdb_data tdb_null;