1 | #ifndef __TDB_H__
|
---|
2 | #define __TDB_H__
|
---|
3 |
|
---|
4 | /*
|
---|
5 | Unix SMB/CIFS implementation.
|
---|
6 |
|
---|
7 | trivial database library
|
---|
8 |
|
---|
9 | Copyright (C) Andrew Tridgell 1999-2004
|
---|
10 |
|
---|
11 | ** NOTE! The following LGPL license applies to the tdb
|
---|
12 | ** library. This does NOT imply that all of Samba is released
|
---|
13 | ** under the LGPL
|
---|
14 |
|
---|
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.
|
---|
19 |
|
---|
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.
|
---|
24 |
|
---|
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/>.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifdef __cplusplus
|
---|
30 | extern "C" {
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <signal.h>
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @defgroup tdb The tdb API
|
---|
37 | *
|
---|
38 | * tdb is a Trivial database. In concept, it is very much like GDBM, and BSD's
|
---|
39 | * DB except that it allows multiple simultaneous writers and uses locking
|
---|
40 | * internally to keep writers from trampling on each other. tdb is also
|
---|
41 | * extremely small.
|
---|
42 | *
|
---|
43 | * @section tdb_interface Interface
|
---|
44 | *
|
---|
45 | * The interface is very similar to gdbm except for the following:
|
---|
46 | *
|
---|
47 | * <ul>
|
---|
48 | * <li>different open interface. The tdb_open call is more similar to a
|
---|
49 | * traditional open()</li>
|
---|
50 | * <li>no tdbm_reorganise() function</li>
|
---|
51 | * <li>no tdbm_sync() function. No operations are cached in the library
|
---|
52 | * anyway</li>
|
---|
53 | * <li>added a tdb_traverse() function for traversing the whole database</li>
|
---|
54 | * <li>added transactions support</li>
|
---|
55 | * </ul>
|
---|
56 | *
|
---|
57 | * A general rule for using tdb is that the caller frees any returned TDB_DATA
|
---|
58 | * structures. Just call free(p.dptr) to free a TDB_DATA return value called p.
|
---|
59 | * This is the same as gdbm.
|
---|
60 | *
|
---|
61 | * @{
|
---|
62 | */
|
---|
63 |
|
---|
64 | /** Flags to tdb_store() */
|
---|
65 | #define TDB_REPLACE 1 /** Unused */
|
---|
66 | #define TDB_INSERT 2 /** Don't overwrite an existing entry */
|
---|
67 | #define TDB_MODIFY 3 /** Don't create an existing entry */
|
---|
68 |
|
---|
69 | /** Flags for tdb_open() */
|
---|
70 | #define TDB_DEFAULT 0 /** just a readability place holder */
|
---|
71 | #define TDB_CLEAR_IF_FIRST 1 /** If this is the first open, wipe the db */
|
---|
72 | #define TDB_INTERNAL 2 /** Don't store on disk */
|
---|
73 | #define TDB_NOLOCK 4 /** Don't do any locking */
|
---|
74 | #define TDB_NOMMAP 8 /** Don't use mmap */
|
---|
75 | #define TDB_CONVERT 16 /** Convert endian (internal use) */
|
---|
76 | #define TDB_BIGENDIAN 32 /** Header is big-endian (internal use) */
|
---|
77 | #define TDB_NOSYNC 64 /** Don't use synchronous transactions */
|
---|
78 | #define TDB_SEQNUM 128 /** Maintain a sequence number */
|
---|
79 | #define TDB_VOLATILE 256 /** Activate the per-hashchain freelist, default 5 */
|
---|
80 | #define TDB_ALLOW_NESTING 512 /** Allow transactions to nest */
|
---|
81 | #define TDB_DISALLOW_NESTING 1024 /** Disallow transactions to nest */
|
---|
82 | #define TDB_INCOMPATIBLE_HASH 2048 /** Better hashing: can't be opened by tdb < 1.2.6. */
|
---|
83 |
|
---|
84 | /** The tdb error codes */
|
---|
85 | enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
|
---|
86 | TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
|
---|
87 | TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
|
---|
88 | TDB_ERR_NESTING};
|
---|
89 |
|
---|
90 | /** Debugging uses one of the following levels */
|
---|
91 | enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
|
---|
92 | TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
|
---|
93 |
|
---|
94 | /** The tdb data structure */
|
---|
95 | typedef struct TDB_DATA {
|
---|
96 | unsigned char *dptr;
|
---|
97 | size_t dsize;
|
---|
98 | } TDB_DATA;
|
---|
99 |
|
---|
100 | #ifndef PRINTF_ATTRIBUTE
|
---|
101 | #if (__GNUC__ >= 3)
|
---|
102 | /** Use gcc attribute to check printf fns. a1 is the 1-based index of
|
---|
103 | * the parameter containing the format, and a2 the index of the first
|
---|
104 | * argument. Note that some gcc 2.x versions don't handle this
|
---|
105 | * properly **/
|
---|
106 | #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
|
---|
107 | #else
|
---|
108 | #define PRINTF_ATTRIBUTE(a1, a2)
|
---|
109 | #endif
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | /** This is the context structure that is returned from a db open. */
|
---|
113 | typedef struct tdb_context TDB_CONTEXT;
|
---|
114 |
|
---|
115 | typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
|
---|
116 | typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
|
---|
117 | typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
|
---|
118 |
|
---|
119 | struct tdb_logging_context {
|
---|
120 | tdb_log_func log_fn;
|
---|
121 | void *log_private;
|
---|
122 | };
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * @brief Open the database and creating it if necessary.
|
---|
126 | *
|
---|
127 | * @param[in] name The name of the db to open.
|
---|
128 | *
|
---|
129 | * @param[in] hash_size The hash size is advisory, use zero for a default
|
---|
130 | * value.
|
---|
131 | *
|
---|
132 | * @param[in] tdb_flags The flags to use to open the db:\n\n
|
---|
133 | * TDB_CLEAR_IF_FIRST - Clear database if we are the
|
---|
134 | * only one with it open\n
|
---|
135 | * TDB_INTERNAL - Don't use a file, instaed store the
|
---|
136 | * data in memory. The filename is
|
---|
137 | * ignored in this case.\n
|
---|
138 | * TDB_NOLOCK - Don't do any locking\n
|
---|
139 | * TDB_NOMMAP - Don't use mmap\n
|
---|
140 | * TDB_NOSYNC - Don't synchronise transactions to disk\n
|
---|
141 | * TDB_SEQNUM - Maintain a sequence number\n
|
---|
142 | * TDB_VOLATILE - activate the per-hashchain freelist,
|
---|
143 | * default 5.\n
|
---|
144 | * TDB_ALLOW_NESTING - Allow transactions to nest.\n
|
---|
145 | * TDB_DISALLOW_NESTING - Disallow transactions to nest.\n
|
---|
146 | *
|
---|
147 | * @param[in] open_flags Flags for the open(2) function.
|
---|
148 | *
|
---|
149 | * @param[in] mode The mode for the open(2) function.
|
---|
150 | *
|
---|
151 | * @return A tdb context structure, NULL on error.
|
---|
152 | */
|
---|
153 | struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
|
---|
154 | int open_flags, mode_t mode);
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * @brief Open the database and creating it if necessary.
|
---|
158 | *
|
---|
159 | * This is like tdb_open(), but allows you to pass an initial logging and
|
---|
160 | * hash function. Be careful when passing a hash function - all users of the
|
---|
161 | * database must use the same hash function or you will get data corruption.
|
---|
162 | *
|
---|
163 | * @param[in] name The name of the db to open.
|
---|
164 | *
|
---|
165 | * @param[in] hash_size The hash size is advisory, use zero for a default
|
---|
166 | * value.
|
---|
167 | *
|
---|
168 | * @param[in] tdb_flags The flags to use to open the db:\n\n
|
---|
169 | * TDB_CLEAR_IF_FIRST - Clear database if we are the
|
---|
170 | * only one with it open\n
|
---|
171 | * TDB_INTERNAL - Don't use a file, instaed store the
|
---|
172 | * data in memory. The filename is
|
---|
173 | * ignored in this case.\n
|
---|
174 | * TDB_NOLOCK - Don't do any locking\n
|
---|
175 | * TDB_NOMMAP - Don't use mmap\n
|
---|
176 | * TDB_NOSYNC - Don't synchronise transactions to disk\n
|
---|
177 | * TDB_SEQNUM - Maintain a sequence number\n
|
---|
178 | * TDB_VOLATILE - activate the per-hashchain freelist,
|
---|
179 | * default 5.\n
|
---|
180 | * TDB_ALLOW_NESTING - Allow transactions to nest.\n
|
---|
181 | * TDB_DISALLOW_NESTING - Disallow transactions to nest.\n
|
---|
182 | *
|
---|
183 | * @param[in] open_flags Flags for the open(2) function.
|
---|
184 | *
|
---|
185 | * @param[in] mode The mode for the open(2) function.
|
---|
186 | *
|
---|
187 | * @param[in] log_ctx The logging function to use.
|
---|
188 | *
|
---|
189 | * @param[in] hash_fn The hash function you want to use.
|
---|
190 | *
|
---|
191 | * @return A tdb context structure, NULL on error.
|
---|
192 | *
|
---|
193 | * @see tdb_open()
|
---|
194 | */
|
---|
195 | struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
|
---|
196 | int open_flags, mode_t mode,
|
---|
197 | const struct tdb_logging_context *log_ctx,
|
---|
198 | tdb_hash_func hash_fn);
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * @brief Set the maximum number of dead records per hash chain.
|
---|
202 | *
|
---|
203 | * @param[in] tdb The database handle to set the maximum.
|
---|
204 | *
|
---|
205 | * @param[in] max_dead The maximum number of dead records per hash chain.
|
---|
206 | */
|
---|
207 | void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * @brief Reopen a tdb.
|
---|
211 | *
|
---|
212 | * This can be used after a fork to ensure that we have an independent seek
|
---|
213 | * pointer from our parent and to re-establish locks.
|
---|
214 | *
|
---|
215 | * @param[in] tdb The database to reopen.
|
---|
216 | *
|
---|
217 | * @return 0 on success, -1 on error.
|
---|
218 | */
|
---|
219 | int tdb_reopen(struct tdb_context *tdb);
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * @brief Reopen all tdb's
|
---|
223 | *
|
---|
224 | * If the parent is longlived (ie. a parent daemon architecture), we know it
|
---|
225 | * will keep it's active lock on a tdb opened with CLEAR_IF_FIRST. Thus for
|
---|
226 | * child processes we don't have to add an active lock. This is essential to
|
---|
227 | * improve performance on systems that keep POSIX locks as a non-scalable data
|
---|
228 | * structure in the kernel.
|
---|
229 | *
|
---|
230 | * @param[in] parent_longlived Wether the parent is longlived or not.
|
---|
231 | *
|
---|
232 | * @return 0 on success, -1 on error.
|
---|
233 | */
|
---|
234 | int tdb_reopen_all(int parent_longlived);
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * @brief Set a different tdb logging function.
|
---|
238 | *
|
---|
239 | * @param[in] tdb The tdb to set the logging function.
|
---|
240 | *
|
---|
241 | * @param[in] log_ctx The logging function to set.
|
---|
242 | */
|
---|
243 | void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * @brief Get the tdb last error code.
|
---|
247 | *
|
---|
248 | * @param[in] tdb The tdb to get the error code from.
|
---|
249 | *
|
---|
250 | * @return A TDB_ERROR code.
|
---|
251 | *
|
---|
252 | * @see TDB_ERROR
|
---|
253 | */
|
---|
254 | enum TDB_ERROR tdb_error(struct tdb_context *tdb);
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * @brief Get a error string for the last tdb error
|
---|
258 | *
|
---|
259 | * @param[in] tdb The tdb to get the error code from.
|
---|
260 | *
|
---|
261 | * @return An error string.
|
---|
262 | */
|
---|
263 | const char *tdb_errorstr(struct tdb_context *tdb);
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * @brief Fetch an entry in the database given a key.
|
---|
267 | *
|
---|
268 | * The caller must free the resulting data.
|
---|
269 | *
|
---|
270 | * @param[in] tdb The tdb to fetch the key.
|
---|
271 | *
|
---|
272 | * @param[in] key The key to fetch.
|
---|
273 | *
|
---|
274 | * @return The key entry found in the database, NULL on error with
|
---|
275 | * TDB_ERROR set.
|
---|
276 | *
|
---|
277 | * @see tdb_error()
|
---|
278 | * @see tdb_errorstr()
|
---|
279 | */
|
---|
280 | TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * @brief Hand a record to a parser function without allocating it.
|
---|
284 | *
|
---|
285 | * This function is meant as a fast tdb_fetch alternative for large records
|
---|
286 | * that are frequently read. The "key" and "data" arguments point directly
|
---|
287 | * into the tdb shared memory, they are not aligned at any boundary.
|
---|
288 | *
|
---|
289 | * @warning The parser is called while tdb holds a lock on the record. DO NOT
|
---|
290 | * call other tdb routines from within the parser. Also, for good performance
|
---|
291 | * you should make the parser fast to allow parallel operations.
|
---|
292 | *
|
---|
293 | * @param[in] tdb The tdb to parse the record.
|
---|
294 | *
|
---|
295 | * @param[in] key The key to parse.
|
---|
296 | *
|
---|
297 | * @param[in] parser The parser to use to parse the data.
|
---|
298 | *
|
---|
299 | * @param[in] private_data A private data pointer which is passed to the parser
|
---|
300 | * function.
|
---|
301 | *
|
---|
302 | * @return -1 if the record was not found. If the record was found,
|
---|
303 | * the return value of "parser" is passed up to the caller.
|
---|
304 | */
|
---|
305 | int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
|
---|
306 | int (*parser)(TDB_DATA key, TDB_DATA data,
|
---|
307 | void *private_data),
|
---|
308 | void *private_data);
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * @brief Delete an entry in the database given a key.
|
---|
312 | *
|
---|
313 | * @param[in] tdb The tdb to delete the key.
|
---|
314 | *
|
---|
315 | * @param[in] key The key to delete.
|
---|
316 | *
|
---|
317 | * @return 0 on success, -1 if the key doesn't exist.
|
---|
318 | */
|
---|
319 | int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * @brief Store an element in the database.
|
---|
323 | *
|
---|
324 | * This replaces any existing element with the same key.
|
---|
325 | *
|
---|
326 | * @param[in] tdb The tdb to store the entry.
|
---|
327 | *
|
---|
328 | * @param[in] key The key to use to store the entry.
|
---|
329 | *
|
---|
330 | * @param[in] dbuf The data to store under the key.
|
---|
331 | *
|
---|
332 | * @param[in] flag The flags to store the key:\n\n
|
---|
333 | * TDB_INSERT: Don't overwrite an existing entry.\n
|
---|
334 | * TDB_MODIFY: Don't create a new entry\n
|
---|
335 | *
|
---|
336 | * @return 0 on success, -1 on error with error code set.
|
---|
337 | *
|
---|
338 | * @see tdb_error()
|
---|
339 | * @see tdb_errorstr()
|
---|
340 | */
|
---|
341 | int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * @brief Append data to an entry.
|
---|
345 | *
|
---|
346 | * If the entry doesn't exist, it will create a new one.
|
---|
347 | *
|
---|
348 | * @param[in] tdb The database to use.
|
---|
349 | *
|
---|
350 | * @param[in] key The key to append the data.
|
---|
351 | *
|
---|
352 | * @param[in] new_dbuf The data to append to the key.
|
---|
353 | *
|
---|
354 | * @return 0 on success, -1 on error with error code set.
|
---|
355 | *
|
---|
356 | * @see tdb_error()
|
---|
357 | * @see tdb_errorstr()
|
---|
358 | */
|
---|
359 | int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * @brief Close a database.
|
---|
363 | *
|
---|
364 | * @param[in] tdb The database to close.
|
---|
365 | *
|
---|
366 | * @return 0 for success, -1 on error.
|
---|
367 | */
|
---|
368 | int tdb_close(struct tdb_context *tdb);
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * @brief Find the first entry in the database and return its key.
|
---|
372 | *
|
---|
373 | * The caller must free the returned data.
|
---|
374 | *
|
---|
375 | * @param[in] tdb The database to use.
|
---|
376 | *
|
---|
377 | * @return The first entry of the database, an empty TDB_DATA entry
|
---|
378 | * if the database is empty.
|
---|
379 | */
|
---|
380 | TDB_DATA tdb_firstkey(struct tdb_context *tdb);
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * @brief Find the next entry in the database, returning its key.
|
---|
384 | *
|
---|
385 | * The caller must free the returned data.
|
---|
386 | *
|
---|
387 | * @param[in] tdb The database to use.
|
---|
388 | *
|
---|
389 | * @param[in] key The key from which you want the next key.
|
---|
390 | *
|
---|
391 | * @return The next entry of the current key, an empty TDB_DATA
|
---|
392 | * entry if there is no entry.
|
---|
393 | */
|
---|
394 | TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * @brief Traverse the entire database.
|
---|
398 | *
|
---|
399 | * While travering the function fn(tdb, key, data, state) is called on each
|
---|
400 | * element. If fn is NULL then it is not called. A non-zero return value from
|
---|
401 | * fn() indicates that the traversal should stop. Traversal callbacks may not
|
---|
402 | * start transactions.
|
---|
403 | *
|
---|
404 | * @warning The data buffer given to the callback fn does NOT meet the alignment
|
---|
405 | * restrictions malloc gives you.
|
---|
406 | *
|
---|
407 | * @param[in] tdb The database to traverse.
|
---|
408 | *
|
---|
409 | * @param[in] fn The function to call on each entry.
|
---|
410 | *
|
---|
411 | * @param[in] private_data The private data which should be passed to the
|
---|
412 | * traversing function.
|
---|
413 | *
|
---|
414 | * @return The record count traversed, -1 on error.
|
---|
415 | */
|
---|
416 | int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *private_data);
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * @brief Traverse the entire database.
|
---|
420 | *
|
---|
421 | * While traversing the database the function fn(tdb, key, data, state) is
|
---|
422 | * called on each element, but marking the database read only during the
|
---|
423 | * traversal, so any write operations will fail. This allows tdb to use read
|
---|
424 | * locks, which increases the parallelism possible during the traversal.
|
---|
425 | *
|
---|
426 | * @param[in] tdb The database to traverse.
|
---|
427 | *
|
---|
428 | * @param[in] fn The function to call on each entry.
|
---|
429 | *
|
---|
430 | * @param[in] private_data The private data which should be passed to the
|
---|
431 | * traversing function.
|
---|
432 | *
|
---|
433 | * @return The record count traversed, -1 on error.
|
---|
434 | */
|
---|
435 | int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *private_data);
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * @brief Check if an entry in the database exists.
|
---|
439 | *
|
---|
440 | * @note 1 is returned if the key is found and 0 is returned if not found this
|
---|
441 | * doesn't match the conventions in the rest of this module, but is compatible
|
---|
442 | * with gdbm.
|
---|
443 | *
|
---|
444 | * @param[in] tdb The database to check if the entry exists.
|
---|
445 | *
|
---|
446 | * @param[in] key The key to check if the entry exists.
|
---|
447 | *
|
---|
448 | * @return 1 if the key is found, 0 if not.
|
---|
449 | */
|
---|
450 | int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * @brief Lock entire database with a write lock.
|
---|
454 | *
|
---|
455 | * @param[in] tdb The database to lock.
|
---|
456 | *
|
---|
457 | * @return 0 on success, -1 on error with error code set.
|
---|
458 | *
|
---|
459 | * @see tdb_error()
|
---|
460 | * @see tdb_errorstr()
|
---|
461 | */
|
---|
462 | int tdb_lockall(struct tdb_context *tdb);
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * @brief Lock entire database with a write lock.
|
---|
466 | *
|
---|
467 | * This is the non-blocking call.
|
---|
468 | *
|
---|
469 | * @param[in] tdb The database to lock.
|
---|
470 | *
|
---|
471 | * @return 0 on success, -1 on error with error code set.
|
---|
472 | *
|
---|
473 | * @see tdb_lockall()
|
---|
474 | * @see tdb_error()
|
---|
475 | * @see tdb_errorstr()
|
---|
476 | */
|
---|
477 | int tdb_lockall_nonblock(struct tdb_context *tdb);
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * @brief Unlock entire database with write lock.
|
---|
481 | *
|
---|
482 | * @param[in] tdb The database to unlock.
|
---|
483 | *
|
---|
484 | * @return 0 on success, -1 on error with error code set.
|
---|
485 | *
|
---|
486 | * @see tdb_lockall()
|
---|
487 | * @see tdb_error()
|
---|
488 | * @see tdb_errorstr()
|
---|
489 | */
|
---|
490 | int tdb_unlockall(struct tdb_context *tdb);
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * @brief Lock entire database with a read lock.
|
---|
494 | *
|
---|
495 | * @param[in] tdb The database to lock.
|
---|
496 | *
|
---|
497 | * @return 0 on success, -1 on error with error code set.
|
---|
498 | *
|
---|
499 | * @see tdb_error()
|
---|
500 | * @see tdb_errorstr()
|
---|
501 | */
|
---|
502 | int tdb_lockall_read(struct tdb_context *tdb);
|
---|
503 |
|
---|
504 | /**
|
---|
505 | * @brief Lock entire database with a read lock.
|
---|
506 | *
|
---|
507 | * This is the non-blocking call.
|
---|
508 | *
|
---|
509 | * @param[in] tdb The database to lock.
|
---|
510 | *
|
---|
511 | * @return 0 on success, -1 on error with error code set.
|
---|
512 | *
|
---|
513 | * @see tdb_lockall_read()
|
---|
514 | * @see tdb_error()
|
---|
515 | * @see tdb_errorstr()
|
---|
516 | */
|
---|
517 | int tdb_lockall_read_nonblock(struct tdb_context *tdb);
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * @brief Unlock entire database with read lock.
|
---|
521 | *
|
---|
522 | * @param[in] tdb The database to unlock.
|
---|
523 | *
|
---|
524 | * @return 0 on success, -1 on error with error code set.
|
---|
525 | *
|
---|
526 | * @see tdb_lockall_read()
|
---|
527 | * @see tdb_error()
|
---|
528 | * @see tdb_errorstr()
|
---|
529 | */
|
---|
530 | int tdb_unlockall_read(struct tdb_context *tdb);
|
---|
531 |
|
---|
532 | /**
|
---|
533 | * @brief Lock entire database with write lock - mark only.
|
---|
534 | *
|
---|
535 | * @todo Add more details.
|
---|
536 | *
|
---|
537 | * @param[in] tdb The database to mark.
|
---|
538 | *
|
---|
539 | * @return 0 on success, -1 on error with error code set.
|
---|
540 | *
|
---|
541 | * @see tdb_error()
|
---|
542 | * @see tdb_errorstr()
|
---|
543 | */
|
---|
544 | int tdb_lockall_mark(struct tdb_context *tdb);
|
---|
545 |
|
---|
546 | /**
|
---|
547 | * @brief Lock entire database with write lock - unmark only.
|
---|
548 | *
|
---|
549 | * @todo Add more details.
|
---|
550 | *
|
---|
551 | * @param[in] tdb The database to mark.
|
---|
552 | *
|
---|
553 | * @return 0 on success, -1 on error with error code set.
|
---|
554 | *
|
---|
555 | * @see tdb_error()
|
---|
556 | * @see tdb_errorstr()
|
---|
557 | */
|
---|
558 | int tdb_lockall_unmark(struct tdb_context *tdb);
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * @brief Get the name of the current tdb file.
|
---|
562 | *
|
---|
563 | * This is useful for external logging functions.
|
---|
564 | *
|
---|
565 | * @param[in] tdb The database to get the name from.
|
---|
566 | *
|
---|
567 | * @return The name of the database.
|
---|
568 | */
|
---|
569 | const char *tdb_name(struct tdb_context *tdb);
|
---|
570 |
|
---|
571 | /**
|
---|
572 | * @brief Get the underlying file descriptor being used by tdb.
|
---|
573 | *
|
---|
574 | * This is useful for external routines that want to check the device/inode
|
---|
575 | * of the fd.
|
---|
576 | *
|
---|
577 | * @param[in] tdb The database to get the fd from.
|
---|
578 | *
|
---|
579 | * @return The file descriptor or -1.
|
---|
580 | */
|
---|
581 | int tdb_fd(struct tdb_context *tdb);
|
---|
582 |
|
---|
583 | /**
|
---|
584 | * @brief Get the current logging function.
|
---|
585 | *
|
---|
586 | * This is useful for external tdb routines that wish to log tdb errors.
|
---|
587 | *
|
---|
588 | * @param[in] tdb The database to get the logging function from.
|
---|
589 | *
|
---|
590 | * @return The logging function of the database.
|
---|
591 | *
|
---|
592 | * @see tdb_get_logging_private()
|
---|
593 | */
|
---|
594 | tdb_log_func tdb_log_fn(struct tdb_context *tdb);
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * @brief Get the private data of the logging function.
|
---|
598 | *
|
---|
599 | * @param[in] tdb The database to get the data from.
|
---|
600 | *
|
---|
601 | * @return The private data pointer of the logging function.
|
---|
602 | *
|
---|
603 | * @see tdb_log_fn()
|
---|
604 | */
|
---|
605 | void *tdb_get_logging_private(struct tdb_context *tdb);
|
---|
606 |
|
---|
607 | /**
|
---|
608 | * @brief Start a transaction.
|
---|
609 | *
|
---|
610 | * All operations after the transaction start can either be committed with
|
---|
611 | * tdb_transaction_commit() or cancelled with tdb_transaction_cancel().
|
---|
612 | *
|
---|
613 | * If you call tdb_transaction_start() again on the same tdb context while a
|
---|
614 | * transaction is in progress, then the same transaction buffer is re-used. The
|
---|
615 | * number of tdb_transaction_{commit,cancel} operations must match the number
|
---|
616 | * of successful tdb_transaction_start() calls.
|
---|
617 | *
|
---|
618 | * Note that transactions are by default disk synchronous, and use a recover
|
---|
619 | * area in the database to automatically recover the database on the next open
|
---|
620 | * if the system crashes during a transaction. You can disable the synchronous
|
---|
621 | * transaction recovery setup using the TDB_NOSYNC flag, which will greatly
|
---|
622 | * speed up operations at the risk of corrupting your database if the system
|
---|
623 | * crashes.
|
---|
624 | *
|
---|
625 | * Operations made within a transaction are not visible to other users of the
|
---|
626 | * database until a successful commit.
|
---|
627 | *
|
---|
628 | * @param[in] tdb The database to start the transaction.
|
---|
629 | *
|
---|
630 | * @return 0 on success, -1 on error with error code set.
|
---|
631 | *
|
---|
632 | * @see tdb_error()
|
---|
633 | * @see tdb_errorstr()
|
---|
634 | */
|
---|
635 | int tdb_transaction_start(struct tdb_context *tdb);
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * @brief Start a transaction, non-blocking.
|
---|
639 | *
|
---|
640 | * @param[in] tdb The database to start the transaction.
|
---|
641 | *
|
---|
642 | * @return 0 on success, -1 on error with error code set.
|
---|
643 | *
|
---|
644 | * @see tdb_error()
|
---|
645 | * @see tdb_errorstr()
|
---|
646 | * @see tdb_transaction_start()
|
---|
647 | */
|
---|
648 | int tdb_transaction_start_nonblock(struct tdb_context *tdb);
|
---|
649 |
|
---|
650 | /**
|
---|
651 | * @brief Prepare to commit a current transaction, for two-phase commits.
|
---|
652 | *
|
---|
653 | * Once prepared for commit, the only allowed calls are tdb_transaction_commit()
|
---|
654 | * or tdb_transaction_cancel(). Preparing allocates disk space for the pending
|
---|
655 | * updates, so a subsequent commit should succeed (barring any hardware
|
---|
656 | * failures).
|
---|
657 | *
|
---|
658 | * @param[in] tdb The database to prepare the commit.
|
---|
659 | *
|
---|
660 | * @return 0 on success, -1 on error with error code set.
|
---|
661 | *
|
---|
662 | * @see tdb_error()
|
---|
663 | * @see tdb_errorstr()
|
---|
664 | */
|
---|
665 | int tdb_transaction_prepare_commit(struct tdb_context *tdb);
|
---|
666 |
|
---|
667 | /**
|
---|
668 | * @brief Commit a current transaction.
|
---|
669 | *
|
---|
670 | * This updates the database and releases the current transaction locks.
|
---|
671 | *
|
---|
672 | * @param[in] tdb The database to commit the transaction.
|
---|
673 | *
|
---|
674 | * @return 0 on success, -1 on error with error code set.
|
---|
675 | *
|
---|
676 | * @see tdb_error()
|
---|
677 | * @see tdb_errorstr()
|
---|
678 | */
|
---|
679 | int tdb_transaction_commit(struct tdb_context *tdb);
|
---|
680 |
|
---|
681 | /**
|
---|
682 | * @brief Cancel a current transaction.
|
---|
683 | *
|
---|
684 | * This discards all write and lock operations that have been made since the
|
---|
685 | * transaction started.
|
---|
686 | *
|
---|
687 | * @param[in] tdb The tdb to cancel the transaction on.
|
---|
688 | *
|
---|
689 | * @return 0 on success, -1 on error with error code set.
|
---|
690 | *
|
---|
691 | * @see tdb_error()
|
---|
692 | * @see tdb_errorstr()
|
---|
693 | */
|
---|
694 | int tdb_transaction_cancel(struct tdb_context *tdb);
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * @brief Get the tdb sequence number.
|
---|
698 | *
|
---|
699 | * Only makes sense if the writers opened with TDB_SEQNUM set. Note that this
|
---|
700 | * sequence number will wrap quite quickly, so it should only be used for a
|
---|
701 | * 'has something changed' test, not for code that relies on the count of the
|
---|
702 | * number of changes made. If you want a counter then use a tdb record.
|
---|
703 | *
|
---|
704 | * The aim of this sequence number is to allow for a very lightweight test of a
|
---|
705 | * possible tdb change.
|
---|
706 | *
|
---|
707 | * @param[in] tdb The database to get the sequence number from.
|
---|
708 | *
|
---|
709 | * @return The sequence number or 0.
|
---|
710 | *
|
---|
711 | * @see tdb_open()
|
---|
712 | * @see tdb_enable_seqnum()
|
---|
713 | */
|
---|
714 | int tdb_get_seqnum(struct tdb_context *tdb);
|
---|
715 |
|
---|
716 | /**
|
---|
717 | * @brief Get the hash size.
|
---|
718 | *
|
---|
719 | * @param[in] tdb The database to get the hash size from.
|
---|
720 | *
|
---|
721 | * @return The hash size.
|
---|
722 | */
|
---|
723 | int tdb_hash_size(struct tdb_context *tdb);
|
---|
724 |
|
---|
725 | /**
|
---|
726 | * @brief Get the map size.
|
---|
727 | *
|
---|
728 | * @param[in] tdb The database to get the map size from.
|
---|
729 | *
|
---|
730 | * @return The map size.
|
---|
731 | */
|
---|
732 | size_t tdb_map_size(struct tdb_context *tdb);
|
---|
733 |
|
---|
734 | /**
|
---|
735 | * @brief Get the tdb flags set during open.
|
---|
736 | *
|
---|
737 | * @param[in] tdb The database to get the flags form.
|
---|
738 | *
|
---|
739 | * @return The flags set to on the database.
|
---|
740 | */
|
---|
741 | int tdb_get_flags(struct tdb_context *tdb);
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * @brief Add flags to the database.
|
---|
745 | *
|
---|
746 | * @param[in] tdb The database to add the flags.
|
---|
747 | *
|
---|
748 | * @param[in] flag The tdb flags to add.
|
---|
749 | */
|
---|
750 | void tdb_add_flags(struct tdb_context *tdb, unsigned flag);
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * @brief Remove flags from the database.
|
---|
754 | *
|
---|
755 | * @param[in] tdb The database to remove the flags.
|
---|
756 | *
|
---|
757 | * @param[in] flag The tdb flags to remove.
|
---|
758 | */
|
---|
759 | void tdb_remove_flags(struct tdb_context *tdb, unsigned flag);
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * @brief Enable sequence number handling on an open tdb.
|
---|
763 | *
|
---|
764 | * @param[in] tdb The database to enable sequence number handling.
|
---|
765 | *
|
---|
766 | * @see tdb_get_seqnum()
|
---|
767 | */
|
---|
768 | void tdb_enable_seqnum(struct tdb_context *tdb);
|
---|
769 |
|
---|
770 | /**
|
---|
771 | * @brief Increment the tdb sequence number.
|
---|
772 | *
|
---|
773 | * This only works if the tdb has been opened using the TDB_SEQNUM flag or
|
---|
774 | * enabled useing tdb_enable_seqnum().
|
---|
775 | *
|
---|
776 | * @param[in] tdb The database to increment the sequence number.
|
---|
777 | *
|
---|
778 | * @see tdb_enable_seqnum()
|
---|
779 | * @see tdb_get_seqnum()
|
---|
780 | */
|
---|
781 | void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
|
---|
782 |
|
---|
783 | /**
|
---|
784 | * @brief Create a hash of the key.
|
---|
785 | *
|
---|
786 | * @param[in] key The key to hash
|
---|
787 | *
|
---|
788 | * @return The hash.
|
---|
789 | */
|
---|
790 | unsigned int tdb_jenkins_hash(TDB_DATA *key);
|
---|
791 |
|
---|
792 | /**
|
---|
793 | * @brief Check the consistency of the database.
|
---|
794 | *
|
---|
795 | * This check the consistency of the database calling back the check function
|
---|
796 | * (if non-NULL) on each record. If some consistency check fails, or the
|
---|
797 | * supplied check function returns -1, tdb_check returns -1, otherwise 0.
|
---|
798 | *
|
---|
799 | * @note The logging function (if set) will be called with additional
|
---|
800 | * information on the corruption found.
|
---|
801 | *
|
---|
802 | * @param[in] tdb The database to check.
|
---|
803 | *
|
---|
804 | * @param[in] check The check function to use.
|
---|
805 | *
|
---|
806 | * @param[in] private_data the private data to pass to the check function.
|
---|
807 | *
|
---|
808 | * @return 0 on success, -1 on error with error code set.
|
---|
809 | *
|
---|
810 | * @see tdb_error()
|
---|
811 | * @see tdb_errorstr()
|
---|
812 | */
|
---|
813 | int tdb_check(struct tdb_context *tdb,
|
---|
814 | int (*check) (TDB_DATA key, TDB_DATA data, void *private_data),
|
---|
815 | void *private_data);
|
---|
816 |
|
---|
817 | /* @} ******************************************************************/
|
---|
818 |
|
---|
819 | /* Low level locking functions: use with care */
|
---|
820 | int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
|
---|
821 | int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key);
|
---|
822 | int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
|
---|
823 | int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
|
---|
824 | int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
|
---|
825 | int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key);
|
---|
826 | int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key);
|
---|
827 |
|
---|
828 | void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *sigptr);
|
---|
829 |
|
---|
830 | /* wipe and repack */
|
---|
831 | int tdb_wipe_all(struct tdb_context *tdb);
|
---|
832 | int tdb_repack(struct tdb_context *tdb);
|
---|
833 |
|
---|
834 | /* Debug functions. Not used in production. */
|
---|
835 | void tdb_dump_all(struct tdb_context *tdb);
|
---|
836 | int tdb_printfreelist(struct tdb_context *tdb);
|
---|
837 | int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
|
---|
838 | int tdb_freelist_size(struct tdb_context *tdb);
|
---|
839 | char *tdb_summary(struct tdb_context *tdb);
|
---|
840 |
|
---|
841 | extern TDB_DATA tdb_null;
|
---|
842 |
|
---|
843 | #ifdef __cplusplus
|
---|
844 | }
|
---|
845 | #endif
|
---|
846 |
|
---|
847 | #endif /* tdb.h */
|
---|