tdb 1.2.9
tdb.h
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
30extern "C" {
31#endif
32
33#include <signal.h>
34#include <stdbool.h>
35
36/* for old gcc releases that don't have the feature test macro __has_attribute */
37#ifndef __has_attribute
38#define __has_attribute(x) 0
39#endif
40
41#ifndef _PUBLIC_
42#if __has_attribute(visibility)
43#define _PUBLIC_ __attribute__((visibility("default")))
44#else
45#define _PUBLIC_
46#endif
47#endif
48
79#define TDB_REPLACE 1
80#define TDB_INSERT 2
81#define TDB_MODIFY 3
84#define TDB_DEFAULT 0
85#define TDB_CLEAR_IF_FIRST 1
86#define TDB_INTERNAL 2
87#define TDB_NOLOCK 4
88#define TDB_NOMMAP 8
89#define TDB_CONVERT 16
90#define TDB_BIGENDIAN 32
91#define TDB_NOSYNC 64
92#define TDB_SEQNUM 128
93#define TDB_VOLATILE 256
94#define TDB_ALLOW_NESTING 512
95#define TDB_DISALLOW_NESTING 1024
96#define TDB_INCOMPATIBLE_HASH 2048
97#define TDB_MUTEX_LOCKING 4096
102enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
103 TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
104 TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
105 TDB_ERR_NESTING};
108enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
109 TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
112typedef struct TDB_DATA {
113 unsigned char *dptr;
114 size_t dsize;
115} TDB_DATA;
116
117#ifndef PRINTF_ATTRIBUTE
118#if __has_attribute(format) || (__GNUC__ >= 3)
123#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
124#else
125#define PRINTF_ATTRIBUTE(a1, a2)
126#endif
127#endif
130typedef struct tdb_context TDB_CONTEXT;
131
132typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
133typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
134typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
135
136struct tdb_logging_context {
137 tdb_log_func log_fn;
138 void *log_private;
139};
140
175_PUBLIC_ struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
176 int open_flags, mode_t mode);
177
222_PUBLIC_ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
223 int open_flags, mode_t mode,
224 const struct tdb_logging_context *log_ctx,
225 tdb_hash_func hash_fn);
226
234_PUBLIC_ void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
235
249_PUBLIC_ int tdb_reopen(struct tdb_context *tdb);
250
264_PUBLIC_ int tdb_reopen_all(int parent_longlived);
265
273_PUBLIC_ void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
274
284_PUBLIC_ enum TDB_ERROR tdb_error(struct tdb_context *tdb);
285
293_PUBLIC_ const char *tdb_errorstr(struct tdb_context *tdb);
294
310_PUBLIC_ TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
311
335_PUBLIC_ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
336 int (*parser)(TDB_DATA key, TDB_DATA data,
337 void *private_data),
338 void *private_data);
339
349_PUBLIC_ int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
350
371_PUBLIC_ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
372
373
396_PUBLIC_ int tdb_storev(struct tdb_context *tdb, TDB_DATA key,
397 const TDB_DATA *dbufs, int num_dbufs, int flag);
398
415_PUBLIC_ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
416
427_PUBLIC_ int tdb_close(struct tdb_context *tdb);
428
439_PUBLIC_ TDB_DATA tdb_firstkey(struct tdb_context *tdb);
440
453_PUBLIC_ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
454
475_PUBLIC_ int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *private_data);
476
494_PUBLIC_ int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *private_data);
495
527_PUBLIC_ int tdb_traverse_chain(struct tdb_context *tdb,
528 unsigned chain,
529 tdb_traverse_func fn,
530 void *private_data);
531
559_PUBLIC_ int tdb_traverse_key_chain(struct tdb_context *tdb,
560 TDB_DATA key,
561 tdb_traverse_func fn,
562 void *private_data);
576_PUBLIC_ int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
577
588_PUBLIC_ int tdb_lockall(struct tdb_context *tdb);
589
603_PUBLIC_ int tdb_lockall_nonblock(struct tdb_context *tdb);
604
616_PUBLIC_ int tdb_unlockall(struct tdb_context *tdb);
617
628_PUBLIC_ int tdb_lockall_read(struct tdb_context *tdb);
629
643_PUBLIC_ int tdb_lockall_read_nonblock(struct tdb_context *tdb);
644
656_PUBLIC_ int tdb_unlockall_read(struct tdb_context *tdb);
657
670_PUBLIC_ int tdb_lockall_mark(struct tdb_context *tdb);
671
684_PUBLIC_ int tdb_lockall_unmark(struct tdb_context *tdb);
685
695_PUBLIC_ const char *tdb_name(struct tdb_context *tdb);
696
707_PUBLIC_ int tdb_fd(struct tdb_context *tdb);
708
720_PUBLIC_ tdb_log_func tdb_log_fn(struct tdb_context *tdb);
721
731_PUBLIC_ void *tdb_get_logging_private(struct tdb_context *tdb);
732
749_PUBLIC_ bool tdb_transaction_active(struct tdb_context *tdb);
750
782_PUBLIC_ int tdb_transaction_start(struct tdb_context *tdb);
783
795_PUBLIC_ int tdb_transaction_start_nonblock(struct tdb_context *tdb);
796
812_PUBLIC_ int tdb_transaction_prepare_commit(struct tdb_context *tdb);
813
826_PUBLIC_ int tdb_transaction_commit(struct tdb_context *tdb);
827
841_PUBLIC_ int tdb_transaction_cancel(struct tdb_context *tdb);
842
861_PUBLIC_ int tdb_get_seqnum(struct tdb_context *tdb);
862
870_PUBLIC_ int tdb_hash_size(struct tdb_context *tdb);
871
879_PUBLIC_ size_t tdb_map_size(struct tdb_context *tdb);
880
888_PUBLIC_ int tdb_get_flags(struct tdb_context *tdb);
889
897_PUBLIC_ void tdb_add_flags(struct tdb_context *tdb, unsigned flag);
898
906_PUBLIC_ void tdb_remove_flags(struct tdb_context *tdb, unsigned flag);
907
915_PUBLIC_ void tdb_enable_seqnum(struct tdb_context *tdb);
916
928_PUBLIC_ void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
929
937_PUBLIC_ unsigned int tdb_jenkins_hash(TDB_DATA *key);
938
960_PUBLIC_ int tdb_check(struct tdb_context *tdb,
961 int (*check) (TDB_DATA key, TDB_DATA data, void *private_data),
962 void *private_data);
963
982_PUBLIC_ int tdb_rescue(struct tdb_context *tdb,
983 void (*walk) (TDB_DATA key, TDB_DATA data, void *private_data),
984 void *private_data);
985
1005_PUBLIC_ bool tdb_runtime_check_for_robust_mutexes(void);
1006
1007/* @} ******************************************************************/
1008
1009/* Low level locking functions: use with care */
1010_PUBLIC_ int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
1011_PUBLIC_ int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key);
1012_PUBLIC_ int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
1013_PUBLIC_ int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
1014_PUBLIC_ int tdb_chainlock_read_nonblock(struct tdb_context *tdb, TDB_DATA key);
1015_PUBLIC_ int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
1016_PUBLIC_ int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key);
1017_PUBLIC_ int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key);
1018
1019_PUBLIC_ void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *sigptr);
1020
1021/* wipe and repack */
1022_PUBLIC_ int tdb_wipe_all(struct tdb_context *tdb);
1023_PUBLIC_ int tdb_repack(struct tdb_context *tdb);
1024
1025/* Debug functions. Not used in production. */
1026_PUBLIC_ void tdb_dump_all(struct tdb_context *tdb);
1027_PUBLIC_ int tdb_printfreelist(struct tdb_context *tdb);
1028_PUBLIC_ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
1029_PUBLIC_ int tdb_freelist_size(struct tdb_context *tdb);
1030_PUBLIC_ char *tdb_summary(struct tdb_context *tdb);
1031
1032_PUBLIC_ extern TDB_DATA tdb_null;
1033
1034#ifdef __cplusplus
1035}
1036#endif
1037
1038#endif /* tdb.h */
_PUBLIC_ int tdb_transaction_start_nonblock(struct tdb_context *tdb)
Start a transaction, non-blocking.
_PUBLIC_ int tdb_lockall_unmark(struct tdb_context *tdb)
Lock entire database with write lock - unmark only.
_PUBLIC_ int tdb_get_seqnum(struct tdb_context *tdb)
Get the tdb sequence number.
_PUBLIC_ int tdb_lockall_read(struct tdb_context *tdb)
Lock entire database with a read lock.
TDB_ERROR
The tdb error codes.
Definition: tdb.h:100
_PUBLIC_ bool tdb_transaction_active(struct tdb_context *tdb)
Is a transaction active?
_PUBLIC_ int tdb_transaction_commit(struct tdb_context *tdb)
Commit a current transaction.
_PUBLIC_ size_t tdb_map_size(struct tdb_context *tdb)
Get the map size.
_PUBLIC_ unsigned int tdb_jenkins_hash(TDB_DATA *key)
Create a hash of the key.
_PUBLIC_ void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx)
Set a different tdb logging function.
struct TDB_DATA TDB_DATA
The tdb data structure.
_PUBLIC_ void tdb_increment_seqnum_nonblock(struct tdb_context *tdb)
Increment the tdb sequence number.
struct tdb_context TDB_CONTEXT
This is the context structure that is returned from a db open.
Definition: tdb.h:128
_PUBLIC_ int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
Check if an entry in the database exists.
_PUBLIC_ int tdb_check(struct tdb_context *tdb, int(*check)(TDB_DATA key, TDB_DATA data, void *private_data), void *private_data)
Check the consistency of the database.
_PUBLIC_ void tdb_set_max_dead(struct tdb_context *tdb, int max_dead)
Set the maximum number of dead records per hash chain.
_PUBLIC_ int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
Delete an entry in the database given a key.
_PUBLIC_ int tdb_traverse_chain(struct tdb_context *tdb, unsigned chain, tdb_traverse_func fn, void *private_data)
Traverse a single hash chain.
_PUBLIC_ TDB_DATA tdb_firstkey(struct tdb_context *tdb)
Find the first entry in the database and return its key.
_PUBLIC_ int tdb_lockall_mark(struct tdb_context *tdb)
Lock entire database with write lock - mark only.
_PUBLIC_ int tdb_lockall_read_nonblock(struct tdb_context *tdb)
Lock entire database with a read lock.
_PUBLIC_ int tdb_traverse_key_chain(struct tdb_context *tdb, TDB_DATA key, tdb_traverse_func fn, void *private_data)
Traverse a single hash chain.
_PUBLIC_ const char * tdb_name(struct tdb_context *tdb)
Get the name of the current tdb file.
_PUBLIC_ int tdb_hash_size(struct tdb_context *tdb)
Get the hash size.
_PUBLIC_ tdb_log_func tdb_log_fn(struct tdb_context *tdb)
Get the current logging function.
_PUBLIC_ int tdb_lockall_nonblock(struct tdb_context *tdb)
Lock entire database with a write lock.
_PUBLIC_ int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *private_data)
Traverse the entire database.
_PUBLIC_ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key)
Find the next entry in the database, returning its key.
_PUBLIC_ void tdb_add_flags(struct tdb_context *tdb, unsigned flag)
Add flags to the database.
_PUBLIC_ enum TDB_ERROR tdb_error(struct tdb_context *tdb)
Get the tdb last error code.
_PUBLIC_ int tdb_get_flags(struct tdb_context *tdb)
Get the tdb flags set during open.
_PUBLIC_ void tdb_remove_flags(struct tdb_context *tdb, unsigned flag)
Remove flags from the database.
_PUBLIC_ int tdb_transaction_prepare_commit(struct tdb_context *tdb)
Prepare to commit a current transaction, for two-phase commits.
_PUBLIC_ int tdb_storev(struct tdb_context *tdb, TDB_DATA key, const TDB_DATA *dbufs, int num_dbufs, int flag)
Store an element in the database.
_PUBLIC_ struct tdb_context * tdb_open(const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode)
Open the database and creating it if necessary.
_PUBLIC_ int tdb_reopen(struct tdb_context *tdb)
Reopen a tdb.
_PUBLIC_ int tdb_reopen_all(int parent_longlived)
Reopen all tdb's.
_PUBLIC_ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
Append data to an entry.
_PUBLIC_ int tdb_rescue(struct tdb_context *tdb, void(*walk)(TDB_DATA key, TDB_DATA data, void *private_data), void *private_data)
Dump all possible records in a corrupt database.
_PUBLIC_ int tdb_lockall(struct tdb_context *tdb)
Lock entire database with a write lock.
_PUBLIC_ int tdb_close(struct tdb_context *tdb)
Close a database.
_PUBLIC_ struct tdb_context * tdb_open_ex(const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode, const struct tdb_logging_context *log_ctx, tdb_hash_func hash_fn)
Open the database and creating it if necessary.
_PUBLIC_ int tdb_unlockall_read(struct tdb_context *tdb)
Unlock entire database with read lock.
_PUBLIC_ int tdb_transaction_cancel(struct tdb_context *tdb)
Cancel a current transaction.
_PUBLIC_ int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *private_data)
Traverse the entire database.
_PUBLIC_ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key, int(*parser)(TDB_DATA key, TDB_DATA data, void *private_data), void *private_data)
Hand a record to a parser function without allocating it.
_PUBLIC_ const char * tdb_errorstr(struct tdb_context *tdb)
Get a error string for the last tdb error.
_PUBLIC_ bool tdb_runtime_check_for_robust_mutexes(void)
Check if support for TDB_MUTEX_LOCKING is available at runtime.
_PUBLIC_ int tdb_fd(struct tdb_context *tdb)
Get the underlying file descriptor being used by tdb.
_PUBLIC_ void * tdb_get_logging_private(struct tdb_context *tdb)
Get the private data of the logging function.
_PUBLIC_ TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
Fetch an entry in the database given a key.
tdb_debug_level
Debugging uses one of the following levels.
Definition: tdb.h:106
_PUBLIC_ int tdb_unlockall(struct tdb_context *tdb)
Unlock entire database with write lock.
_PUBLIC_ void tdb_enable_seqnum(struct tdb_context *tdb)
Enable sequence number handling on an open tdb.
_PUBLIC_ int tdb_transaction_start(struct tdb_context *tdb)
Start a transaction.
_PUBLIC_ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
Store an element in the database.
The tdb data structure.
Definition: tdb.h:110