]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1.h
8ce5fa9d0777515aede35c4151ff55b4bccf2466
[ccan] / ccan / tdb2 / tdb1.h
1 #ifndef TDB1_H
2 #define TDB1_H
3
4 /*
5    Unix SMB/CIFS implementation.
6
7    trivial database library (version 1 compat functions)
8
9    Copyright (C) Andrew Tridgell 1999-2004
10    Copyright (C) Rusty Russell 2011
11
12      ** NOTE! The following LGPL license applies to the tdb
13      ** library. This does NOT imply that all of Samba is released
14      ** under the LGPL
15
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.
20
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.
25
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/>.
28 */
29 #include "tdb2.h"
30
31 #ifndef _SAMBA_BUILD_
32 /* For mode_t */
33 #include <sys/types.h>
34 /* For O_* flags. */
35 #include <sys/stat.h>
36 #endif
37
38 /** Flags to tdb1_store() */
39 #define TDB1_REPLACE 1          /** Unused */
40 #define TDB1_INSERT 2           /** Don't overwrite an existing entry */
41 #define TDB1_MODIFY 3           /** Don't create an existing entry    */
42
43 /** Flags for tdb1_open() */
44 #define TDB1_DEFAULT 0 /** just a readability place holder */
45 #define TDB1_CLEAR_IF_FIRST 1 /** If this is the first open, wipe the db */
46 #define TDB1_INTERNAL 2 /** Don't store on disk */
47 #define TDB1_NOLOCK   4 /** Don't do any locking */
48 #define TDB1_NOMMAP   8 /** Don't use mmap */
49 #define TDB1_CONVERT 16 /** Convert endian (internal use) */
50 #define TDB1_BIGENDIAN 32 /** Header is big-endian (internal use) */
51 #define TDB1_NOSYNC   64 /** Don't use synchronous transactions */
52 #define TDB1_SEQNUM   128 /** Maintain a sequence number */
53 #define TDB1_VOLATILE   256 /** Activate the per-hashchain freelist, default 5 */
54 #define TDB1_ALLOW_NESTING 512 /** Allow transactions to nest */
55 #define TDB1_DISALLOW_NESTING 1024 /** Disallow transactions to nest */
56 #define TDB1_INCOMPATIBLE_HASH 2048 /** Better hashing: can't be opened by tdb < 1.2.6. */
57
58 /** The tdb error codes */
59 enum TDB1_ERROR {TDB1_SUCCESS=0, TDB1_ERR_CORRUPT, TDB1_ERR_IO, TDB1_ERR_LOCK,
60                 TDB1_ERR_OOM, TDB1_ERR_EXISTS, TDB1_ERR_NOLOCK, TDB1_ERR_LOCK_TIMEOUT,
61                 TDB1_ERR_NOEXIST, TDB1_ERR_EINVAL, TDB1_ERR_RDONLY,
62                 TDB1_ERR_NESTING};
63
64 /** Debugging uses one of the following levels */
65 enum tdb1_debug_level {TDB1_DEBUG_FATAL = 0, TDB1_DEBUG_ERROR,
66                       TDB1_DEBUG_WARNING, TDB1_DEBUG_TRACE};
67
68 /** The tdb data structure */
69 typedef struct TDB1_DATA {
70         unsigned char *dptr;
71         size_t dsize;
72 } TDB1_DATA;
73
74 #ifndef PRINTF_ATTRIBUTE
75 #if (__GNUC__ >= 3)
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
79  * properly **/
80 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
81 #else
82 #define PRINTF_ATTRIBUTE(a1, a2)
83 #endif
84 #endif
85
86 /** This is the context structure that is returned from a db open. */
87 typedef struct tdb1_context TDB1_CONTEXT;
88
89 typedef int (*tdb1_traverse_func)(struct tdb1_context *, TDB1_DATA, TDB1_DATA, void *);
90 typedef void (*tdb1_log_func)(struct tdb1_context *, enum tdb1_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
91 typedef unsigned int (*tdb1_hash_func)(TDB1_DATA *key);
92
93 struct tdb1_logging_context {
94         tdb1_log_func log_fn;
95         void *log_private;
96 };
97
98 struct tdb1_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
99                       int open_flags, mode_t mode);
100
101 struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
102                          int open_flags, mode_t mode,
103                          const struct tdb1_logging_context *log_ctx,
104                          tdb1_hash_func hash_fn);
105
106 void tdb1_set_max_dead(struct tdb1_context *tdb, int max_dead);
107
108 TDB1_DATA tdb1_fetch(struct tdb1_context *tdb, TDB1_DATA key);
109
110 int tdb1_parse_record(struct tdb1_context *tdb, TDB1_DATA key,
111                               int (*parser)(TDB1_DATA key, TDB1_DATA data,
112                                             void *private_data),
113                               void *private_data);
114
115 int tdb1_delete(struct tdb1_context *tdb, TDB1_DATA key);
116
117 int tdb1_store(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA dbuf, int flag);
118
119 int tdb1_append(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA new_dbuf);
120
121 int tdb1_close(struct tdb1_context *tdb);
122
123 TDB1_DATA tdb1_firstkey(struct tdb1_context *tdb);
124
125 TDB1_DATA tdb1_nextkey(struct tdb1_context *tdb, TDB1_DATA key);
126
127 int tdb1_traverse(struct tdb1_context *tdb, tdb1_traverse_func fn, void *private_data);
128
129 int tdb1_traverse_read(struct tdb1_context *tdb, tdb1_traverse_func fn, void *private_data);
130
131 int tdb1_exists(struct tdb1_context *tdb, TDB1_DATA key);
132
133 int tdb1_lockall(struct tdb1_context *tdb);
134
135 int tdb1_unlockall(struct tdb1_context *tdb);
136
137 int tdb1_lockall_read(struct tdb1_context *tdb);
138
139 int tdb1_unlockall_read(struct tdb1_context *tdb);
140
141 tdb1_log_func tdb1_log_fn(struct tdb1_context *tdb);
142
143 int tdb1_transaction_start(struct tdb1_context *tdb);
144
145 int tdb1_transaction_prepare_commit(struct tdb1_context *tdb);
146
147 int tdb1_transaction_commit(struct tdb1_context *tdb);
148
149 int tdb1_transaction_cancel(struct tdb1_context *tdb);
150
151 int tdb1_get_seqnum(struct tdb1_context *tdb);
152
153 int tdb1_hash_size(struct tdb1_context *tdb);
154
155 void tdb1_increment_seqnum_nonblock(struct tdb1_context *tdb);
156
157 unsigned int tdb1_jenkins_hash(TDB1_DATA *key);
158
159 int tdb1_check(struct tdb1_context *tdb,
160               int (*check) (TDB1_DATA key, TDB1_DATA data, void *private_data),
161               void *private_data);
162
163 /* @} ******************************************************************/
164
165 /* Low level locking functions: use with care */
166 int tdb1_chainlock(struct tdb1_context *tdb, TDB1_DATA key);
167 int tdb1_chainunlock(struct tdb1_context *tdb, TDB1_DATA key);
168 int tdb1_chainlock_read(struct tdb1_context *tdb, TDB1_DATA key);
169 int tdb1_chainunlock_read(struct tdb1_context *tdb, TDB1_DATA key);
170
171
172 /* wipe and repack */
173 int tdb1_wipe_all(struct tdb1_context *tdb);
174 int tdb1_repack(struct tdb1_context *tdb);
175
176 /* Debug functions. Not used in production. */
177 char *tdb1_summary(struct tdb1_context *tdb);
178
179 extern TDB1_DATA tdb1_null;
180
181 #endif /* tdb1.h */