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 2 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, write to the Free Software
27    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
28 */
29 
30 #ifdef  __cplusplus
31 extern "C" {
32 #endif
33 
34 
35 /* flags to tdb_store() */
operator ()add_one36 #define TDB_REPLACE 1
37 #define TDB_INSERT 2
38 #define TDB_MODIFY 3
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 
51 #define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
52 
53 /* error codes */
54 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
55 		TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
56 		TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY};
57 
58 /* debugging uses one of the following levels */
59 enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
test()60 		      TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
61 
62 typedef struct TDB_DATA {
63 	unsigned char *dptr;
64 	size_t dsize;
65 } TDB_DATA;
66 
67 #ifndef PRINTF_ATTRIBUTE
68 #if (__GNUC__ >= 3)
69 /** Use gcc attribute to check printf fns.  a1 is the 1-based index of
70  * the parameter containing the format, and a2 the index of the first
71  * argument. Note that some gcc 2.x versions don't handle this
72  * properly **/
73 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
74 #else
75 #define PRINTF_ATTRIBUTE(a1, a2)
76 #endif
77 #endif
78 
79 /* ext2fs tdb renames */
80 #define tdb_open ext2fs_tdb_open
81 #define tdb_open_ex ext2fs_tdb_open_ex
82 #define tdb_set_max_dead ext2fs_tdb_set_max_dead
83 #define tdb_reopen ext2fs_tdb_reopen
84 #define tdb_reopen_all ext2fs_tdb_reopen_all
85 #define tdb_set_logging_function ext2fs_tdb_set_logging_function
86 #define tdb_error ext2fs_tdb_error
87 #define tdb_errorstr ext2fs_tdb_errorstr
88 #define tdb_fetch ext2fs_tdb_fetch
89 #define tdb_parse_record ext2fs_tdb_parse_record
90 #define tdb_delete ext2fs_tdb_delete
91 #define tdb_store ext2fs_tdb_store
92 #define tdb_append ext2fs_tdb_append
triangle(size_t n)93 #define tdb_close ext2fs_tdb_close
94 #define tdb_firstkey ext2fs_tdb_firstkey
95 #define tdb_nextkey ext2fs_tdb_nextkey
96 #define tdb_traverse ext2fs_tdb_traverse
97 #define tdb_traverse_read ext2fs_tdb_traverse_read
98 #define tdb_exists ext2fs_tdb_exists
99 #define tdb_lockall ext2fs_tdb_lockall
100 #define tdb_unlockall ext2fs_tdb_unlockall
101 #define tdb_lockall_read ext2fs_tdb_lockall_read
102 #define tdb_unlockall_read ext2fs_tdb_unlockall_read
103 #define tdb_name ext2fs_tdb_name
104 #define tdb_fd ext2fs_tdb_fd
105 #define tdb_log_fn ext2fs_tdb_log_fn
106 #define tdb_get_logging_private ext2fs_tdb_get_logging_private
107 #define tdb_transaction_start ext2fs_tdb_transaction_start
108 #define tdb_transaction_commit ext2fs_tdb_transaction_commit
109 #define tdb_transaction_cancel ext2fs_tdb_transaction_cancel
110 #define tdb_transaction_recover ext2fs_tdb_transaction_recover
111 #define tdb_get_seqnum ext2fs_tdb_get_seqnum
112 #define tdb_hash_size ext2fs_tdb_hash_size
113 #define tdb_map_size ext2fs_tdb_map_size
114 #define tdb_get_flags ext2fs_tdb_get_flags
115 #define tdb_chainlock ext2fs_tdb_chainlock
116 #define tdb_chainunlock ext2fs_tdb_chainunlock
117 #define tdb_chainlock_read ext2fs_tdb_chainlock_read
118 #define tdb_chainunlock_read ext2fs_tdb_chainunlock_read
119 #define tdb_dump_all ext2fs_tdb_dump_all
120 #define tdb_printfreelist ext2fs_tdb_printfreelist
121 #define tdb_validate_freelist ext2fs_tdb_validate_freelist
122 #define tdb_chainlock_mark ext2fs_tdb_chainlock_mark
123 #define tdb_chainlock_nonblock ext2fs_tdb_chainlock_nonblock
124 #define tdb_chainlock_unmark ext2fs_tdb_chainlock_unmark
125 #define tdb_enable_seqnum ext2fs_tdb_enable_seqnum
126 #define tdb_increment_seqnum_nonblock ext2fs_tdb_increment_seqnum_nonblock
127 #define tdb_lock_nonblock ext2fs_tdb_lock_nonblock
128 #define tdb_lockall_mark ext2fs_tdb_lockall_mark
129 #define tdb_lockall_nonblock ext2fs_tdb_lockall_nonblock
130 #define tdb_lockall_read_nonblock ext2fs_tdb_lockall_read_nonblock
131 #define tdb_lockall_unmark ext2fs_tdb_lockall_unmark
132 #define tdb_flush ext2fs_tdb_flush
133 
134 /* this is the context structure that is returned from a db open */
135 typedef struct tdb_context TDB_CONTEXT;
136 
137 typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
138 typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
139 typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
140 
141 struct tdb_logging_context {
142         tdb_log_func log_fn;
143         void *log_private;
144 };
145 
146 struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
147 		      int open_flags, mode_t mode);
test()148 struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
149 			 int open_flags, mode_t mode,
150 			 const struct tdb_logging_context *log_ctx,
151 			 tdb_hash_func hash_fn);
152 void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
153 
154 int tdb_reopen(struct tdb_context *tdb);
155 int tdb_reopen_all(int parent_longlived);
156 void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
157 enum TDB_ERROR tdb_error(struct tdb_context *tdb);
158 const char *tdb_errorstr(struct tdb_context *tdb);
159 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
160 int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
161 		     int (*parser)(TDB_DATA key, TDB_DATA data,
162 				   void *private_data),
main(int,char **)163 		     void *private_data);
164 int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
165 int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
166 int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
167 int tdb_close(struct tdb_context *tdb);
168 TDB_DATA tdb_firstkey(struct tdb_context *tdb);
169 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
170 int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *);
171 int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *);
172 int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
173 int tdb_lockall(struct tdb_context *tdb);
174 int tdb_lockall_nonblock(struct tdb_context *tdb);
175 int tdb_unlockall(struct tdb_context *tdb);
176 int tdb_lockall_read(struct tdb_context *tdb);
177 int tdb_lockall_read_nonblock(struct tdb_context *tdb);
178 int tdb_unlockall_read(struct tdb_context *tdb);
179 int tdb_lockall_mark(struct tdb_context *tdb);
180 int tdb_lockall_unmark(struct tdb_context *tdb);
181 const char *tdb_name(struct tdb_context *tdb);
182 int tdb_fd(struct tdb_context *tdb);
183 tdb_log_func tdb_log_fn(struct tdb_context *tdb);
184 void *tdb_get_logging_private(struct tdb_context *tdb);
185 int tdb_transaction_start(struct tdb_context *tdb);
186 int tdb_transaction_commit(struct tdb_context *tdb);
187 int tdb_transaction_cancel(struct tdb_context *tdb);
188 int tdb_transaction_recover(struct tdb_context *tdb);
189 int tdb_get_seqnum(struct tdb_context *tdb);
190 int tdb_hash_size(struct tdb_context *tdb);
191 size_t tdb_map_size(struct tdb_context *tdb);
192 int tdb_get_flags(struct tdb_context *tdb);
193 void tdb_enable_seqnum(struct tdb_context *tdb);
194 void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
195 int tdb_flush(struct tdb_context *tdb);
196 
197 /* Low level locking functions: use with care */
198 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
199 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key);
200 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
201 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
202 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
203 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key);
204 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key);
205 
206 /* Debug functions. Not used in production. */
207 void tdb_dump_all(struct tdb_context *tdb);
208 int tdb_printfreelist(struct tdb_context *tdb);
209 int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
210 
211 #ifdef  __cplusplus
212 }
213 #endif
214 
215 #endif /* tdb.h */
216