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
35/* flags to tdb_store() */
36#define TDB_REPLACE 1		/* Unused */
37#define TDB_INSERT 2 		/* Don't overwrite an existing entry */
38#define TDB_MODIFY 3		/* Don't create an existing entry    */
39
40/* flags for tdb_open() */
41#define TDB_DEFAULT 0 /* just a readability place holder */
42#define TDB_CLEAR_IF_FIRST 1
43#define TDB_INTERNAL 2 /* don't store on disk */
44#define TDB_NOLOCK   4 /* don't do any locking */
45#define TDB_NOMMAP   8 /* don't use mmap */
46#define TDB_CONVERT 16 /* convert endian (internal use) */
47#define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
48#define TDB_NOSYNC   64 /* don't use synchronous transactions */
49#define TDB_SEQNUM   128 /* maintain a sequence number */
50#define TDB_VOLATILE   256 /* Activate the per-hashchain freelist, default 5 */
51#define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
52#define TDB_DISALLOW_NESTING 1024 /* Disallow transactions to nest */
53
54/* error codes */
55enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
56		TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
57		TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
58		TDB_ERR_NESTING};
59
60/* debugging uses one of the following levels */
61enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
62		      TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
63
64typedef struct TDB_DATA {
65	unsigned char *dptr;
66	size_t dsize;
67} TDB_DATA;
68
69#ifndef PRINTF_ATTRIBUTE
70#if (__GNUC__ >= 3)
71/** Use gcc attribute to check printf fns.  a1 is the 1-based index of
72 * the parameter containing the format, and a2 the index of the first
73 * argument. Note that some gcc 2.x versions don't handle this
74 * properly **/
75#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
76#else
77#define PRINTF_ATTRIBUTE(a1, a2)
78#endif
79#endif
80
81/* this is the context structure that is returned from a db open */
82typedef struct tdb_context TDB_CONTEXT;
83
84typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
85typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
86typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
87
88struct tdb_logging_context {
89        tdb_log_func log_fn;
90        void *log_private;
91};
92
93struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
94		      int open_flags, mode_t mode);
95struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
96			 int open_flags, mode_t mode,
97			 const struct tdb_logging_context *log_ctx,
98			 tdb_hash_func hash_fn);
99void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
100
101int tdb_reopen(struct tdb_context *tdb);
102int tdb_reopen_all(int parent_longlived);
103void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
104enum TDB_ERROR tdb_error(struct tdb_context *tdb);
105const char *tdb_errorstr(struct tdb_context *tdb);
106TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
107int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
108		     int (*parser)(TDB_DATA key, TDB_DATA data,
109				   void *private_data),
110		     void *private_data);
111int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
112int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
113int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
114int tdb_close(struct tdb_context *tdb);
115TDB_DATA tdb_firstkey(struct tdb_context *tdb);
116TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
117int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *);
118int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *);
119int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
120int tdb_lockall(struct tdb_context *tdb);
121int tdb_lockall_nonblock(struct tdb_context *tdb);
122int tdb_unlockall(struct tdb_context *tdb);
123int tdb_lockall_read(struct tdb_context *tdb);
124int tdb_lockall_read_nonblock(struct tdb_context *tdb);
125int tdb_unlockall_read(struct tdb_context *tdb);
126int tdb_lockall_mark(struct tdb_context *tdb);
127int tdb_lockall_unmark(struct tdb_context *tdb);
128const char *tdb_name(struct tdb_context *tdb);
129int tdb_fd(struct tdb_context *tdb);
130tdb_log_func tdb_log_fn(struct tdb_context *tdb);
131void *tdb_get_logging_private(struct tdb_context *tdb);
132int tdb_transaction_start(struct tdb_context *tdb);
133int tdb_transaction_prepare_commit(struct tdb_context *tdb);
134int tdb_transaction_commit(struct tdb_context *tdb);
135int tdb_transaction_cancel(struct tdb_context *tdb);
136int tdb_transaction_recover(struct tdb_context *tdb);
137int tdb_get_seqnum(struct tdb_context *tdb);
138int tdb_hash_size(struct tdb_context *tdb);
139size_t tdb_map_size(struct tdb_context *tdb);
140int tdb_get_flags(struct tdb_context *tdb);
141void tdb_add_flags(struct tdb_context *tdb, unsigned flag);
142void tdb_remove_flags(struct tdb_context *tdb, unsigned flag);
143void tdb_enable_seqnum(struct tdb_context *tdb);
144void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
145int tdb_check(struct tdb_context *tdb,
146	      int (*check) (TDB_DATA key, TDB_DATA data, void *private_data),
147	      void *private_data);
148
149/* Low level locking functions: use with care */
150int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
151int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key);
152int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
153int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
154int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
155int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key);
156int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key);
157
158void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *sigptr);
159
160/* wipe and repack */
161int tdb_wipe_all(struct tdb_context *tdb);
162int tdb_repack(struct tdb_context *tdb);
163
164/* Debug functions. Not used in production. */
165void tdb_dump_all(struct tdb_context *tdb);
166int tdb_printfreelist(struct tdb_context *tdb);
167int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
168int tdb_freelist_size(struct tdb_context *tdb);
169
170extern TDB_DATA tdb_null;
171
172#ifdef  __cplusplus
173}
174#endif
175
176#endif /* tdb.h */
177