xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/tdb.h (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker #ifndef __TDB_H__
2*6a54128fSAndroid Build Coastguard Worker #define __TDB_H__
3*6a54128fSAndroid Build Coastguard Worker 
4*6a54128fSAndroid Build Coastguard Worker /*
5*6a54128fSAndroid Build Coastguard Worker    Unix SMB/CIFS implementation.
6*6a54128fSAndroid Build Coastguard Worker 
7*6a54128fSAndroid Build Coastguard Worker    trivial database library
8*6a54128fSAndroid Build Coastguard Worker 
9*6a54128fSAndroid Build Coastguard Worker    Copyright (C) Andrew Tridgell 1999-2004
10*6a54128fSAndroid Build Coastguard Worker 
11*6a54128fSAndroid Build Coastguard Worker      ** NOTE! The following LGPL license applies to the tdb
12*6a54128fSAndroid Build Coastguard Worker      ** library. This does NOT imply that all of Samba is released
13*6a54128fSAndroid Build Coastguard Worker      ** under the LGPL
14*6a54128fSAndroid Build Coastguard Worker 
15*6a54128fSAndroid Build Coastguard Worker    This library is free software; you can redistribute it and/or
16*6a54128fSAndroid Build Coastguard Worker    modify it under the terms of the GNU Lesser General Public
17*6a54128fSAndroid Build Coastguard Worker    License as published by the Free Software Foundation; either
18*6a54128fSAndroid Build Coastguard Worker    version 2 of the License, or (at your option) any later version.
19*6a54128fSAndroid Build Coastguard Worker 
20*6a54128fSAndroid Build Coastguard Worker    This library is distributed in the hope that it will be useful,
21*6a54128fSAndroid Build Coastguard Worker    but WITHOUT ANY WARRANTY; without even the implied warranty of
22*6a54128fSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23*6a54128fSAndroid Build Coastguard Worker    Lesser General Public License for more details.
24*6a54128fSAndroid Build Coastguard Worker 
25*6a54128fSAndroid Build Coastguard Worker    You should have received a copy of the GNU Lesser General Public
26*6a54128fSAndroid Build Coastguard Worker    License along with this library; if not, write to the Free Software
27*6a54128fSAndroid Build Coastguard Worker    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
28*6a54128fSAndroid Build Coastguard Worker */
29*6a54128fSAndroid Build Coastguard Worker 
30*6a54128fSAndroid Build Coastguard Worker #ifdef  __cplusplus
31*6a54128fSAndroid Build Coastguard Worker extern "C" {
32*6a54128fSAndroid Build Coastguard Worker #endif
33*6a54128fSAndroid Build Coastguard Worker 
34*6a54128fSAndroid Build Coastguard Worker 
35*6a54128fSAndroid Build Coastguard Worker /* flags to tdb_store() */
36*6a54128fSAndroid Build Coastguard Worker #define TDB_REPLACE 1
37*6a54128fSAndroid Build Coastguard Worker #define TDB_INSERT 2
38*6a54128fSAndroid Build Coastguard Worker #define TDB_MODIFY 3
39*6a54128fSAndroid Build Coastguard Worker 
40*6a54128fSAndroid Build Coastguard Worker /* flags for tdb_open() */
41*6a54128fSAndroid Build Coastguard Worker #define TDB_DEFAULT 0 /* just a readability place holder */
42*6a54128fSAndroid Build Coastguard Worker #define TDB_CLEAR_IF_FIRST 1
43*6a54128fSAndroid Build Coastguard Worker #define TDB_INTERNAL 2 /* don't store on disk */
44*6a54128fSAndroid Build Coastguard Worker #define TDB_NOLOCK   4 /* don't do any locking */
45*6a54128fSAndroid Build Coastguard Worker #define TDB_NOMMAP   8 /* don't use mmap */
46*6a54128fSAndroid Build Coastguard Worker #define TDB_CONVERT 16 /* convert endian (internal use) */
47*6a54128fSAndroid Build Coastguard Worker #define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
48*6a54128fSAndroid Build Coastguard Worker #define TDB_NOSYNC   64 /* don't use synchronous transactions */
49*6a54128fSAndroid Build Coastguard Worker #define TDB_SEQNUM   128 /* maintain a sequence number */
50*6a54128fSAndroid Build Coastguard Worker 
51*6a54128fSAndroid Build Coastguard Worker #define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
52*6a54128fSAndroid Build Coastguard Worker 
53*6a54128fSAndroid Build Coastguard Worker /* error codes */
54*6a54128fSAndroid Build Coastguard Worker enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
55*6a54128fSAndroid Build Coastguard Worker 		TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
56*6a54128fSAndroid Build Coastguard Worker 		TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY};
57*6a54128fSAndroid Build Coastguard Worker 
58*6a54128fSAndroid Build Coastguard Worker /* debugging uses one of the following levels */
59*6a54128fSAndroid Build Coastguard Worker enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
60*6a54128fSAndroid Build Coastguard Worker 		      TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
61*6a54128fSAndroid Build Coastguard Worker 
62*6a54128fSAndroid Build Coastguard Worker typedef struct TDB_DATA {
63*6a54128fSAndroid Build Coastguard Worker 	unsigned char *dptr;
64*6a54128fSAndroid Build Coastguard Worker 	size_t dsize;
65*6a54128fSAndroid Build Coastguard Worker } TDB_DATA;
66*6a54128fSAndroid Build Coastguard Worker 
67*6a54128fSAndroid Build Coastguard Worker #ifndef PRINTF_ATTRIBUTE
68*6a54128fSAndroid Build Coastguard Worker #if (__GNUC__ >= 3)
69*6a54128fSAndroid Build Coastguard Worker /** Use gcc attribute to check printf fns.  a1 is the 1-based index of
70*6a54128fSAndroid Build Coastguard Worker  * the parameter containing the format, and a2 the index of the first
71*6a54128fSAndroid Build Coastguard Worker  * argument. Note that some gcc 2.x versions don't handle this
72*6a54128fSAndroid Build Coastguard Worker  * properly **/
73*6a54128fSAndroid Build Coastguard Worker #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
74*6a54128fSAndroid Build Coastguard Worker #else
75*6a54128fSAndroid Build Coastguard Worker #define PRINTF_ATTRIBUTE(a1, a2)
76*6a54128fSAndroid Build Coastguard Worker #endif
77*6a54128fSAndroid Build Coastguard Worker #endif
78*6a54128fSAndroid Build Coastguard Worker 
79*6a54128fSAndroid Build Coastguard Worker /* ext2fs tdb renames */
80*6a54128fSAndroid Build Coastguard Worker #define tdb_open ext2fs_tdb_open
81*6a54128fSAndroid Build Coastguard Worker #define tdb_open_ex ext2fs_tdb_open_ex
82*6a54128fSAndroid Build Coastguard Worker #define tdb_set_max_dead ext2fs_tdb_set_max_dead
83*6a54128fSAndroid Build Coastguard Worker #define tdb_reopen ext2fs_tdb_reopen
84*6a54128fSAndroid Build Coastguard Worker #define tdb_reopen_all ext2fs_tdb_reopen_all
85*6a54128fSAndroid Build Coastguard Worker #define tdb_set_logging_function ext2fs_tdb_set_logging_function
86*6a54128fSAndroid Build Coastguard Worker #define tdb_error ext2fs_tdb_error
87*6a54128fSAndroid Build Coastguard Worker #define tdb_errorstr ext2fs_tdb_errorstr
88*6a54128fSAndroid Build Coastguard Worker #define tdb_fetch ext2fs_tdb_fetch
89*6a54128fSAndroid Build Coastguard Worker #define tdb_parse_record ext2fs_tdb_parse_record
90*6a54128fSAndroid Build Coastguard Worker #define tdb_delete ext2fs_tdb_delete
91*6a54128fSAndroid Build Coastguard Worker #define tdb_store ext2fs_tdb_store
92*6a54128fSAndroid Build Coastguard Worker #define tdb_append ext2fs_tdb_append
93*6a54128fSAndroid Build Coastguard Worker #define tdb_close ext2fs_tdb_close
94*6a54128fSAndroid Build Coastguard Worker #define tdb_firstkey ext2fs_tdb_firstkey
95*6a54128fSAndroid Build Coastguard Worker #define tdb_nextkey ext2fs_tdb_nextkey
96*6a54128fSAndroid Build Coastguard Worker #define tdb_traverse ext2fs_tdb_traverse
97*6a54128fSAndroid Build Coastguard Worker #define tdb_traverse_read ext2fs_tdb_traverse_read
98*6a54128fSAndroid Build Coastguard Worker #define tdb_exists ext2fs_tdb_exists
99*6a54128fSAndroid Build Coastguard Worker #define tdb_lockall ext2fs_tdb_lockall
100*6a54128fSAndroid Build Coastguard Worker #define tdb_unlockall ext2fs_tdb_unlockall
101*6a54128fSAndroid Build Coastguard Worker #define tdb_lockall_read ext2fs_tdb_lockall_read
102*6a54128fSAndroid Build Coastguard Worker #define tdb_unlockall_read ext2fs_tdb_unlockall_read
103*6a54128fSAndroid Build Coastguard Worker #define tdb_name ext2fs_tdb_name
104*6a54128fSAndroid Build Coastguard Worker #define tdb_fd ext2fs_tdb_fd
105*6a54128fSAndroid Build Coastguard Worker #define tdb_log_fn ext2fs_tdb_log_fn
106*6a54128fSAndroid Build Coastguard Worker #define tdb_get_logging_private ext2fs_tdb_get_logging_private
107*6a54128fSAndroid Build Coastguard Worker #define tdb_transaction_start ext2fs_tdb_transaction_start
108*6a54128fSAndroid Build Coastguard Worker #define tdb_transaction_commit ext2fs_tdb_transaction_commit
109*6a54128fSAndroid Build Coastguard Worker #define tdb_transaction_cancel ext2fs_tdb_transaction_cancel
110*6a54128fSAndroid Build Coastguard Worker #define tdb_transaction_recover ext2fs_tdb_transaction_recover
111*6a54128fSAndroid Build Coastguard Worker #define tdb_get_seqnum ext2fs_tdb_get_seqnum
112*6a54128fSAndroid Build Coastguard Worker #define tdb_hash_size ext2fs_tdb_hash_size
113*6a54128fSAndroid Build Coastguard Worker #define tdb_map_size ext2fs_tdb_map_size
114*6a54128fSAndroid Build Coastguard Worker #define tdb_get_flags ext2fs_tdb_get_flags
115*6a54128fSAndroid Build Coastguard Worker #define tdb_chainlock ext2fs_tdb_chainlock
116*6a54128fSAndroid Build Coastguard Worker #define tdb_chainunlock ext2fs_tdb_chainunlock
117*6a54128fSAndroid Build Coastguard Worker #define tdb_chainlock_read ext2fs_tdb_chainlock_read
118*6a54128fSAndroid Build Coastguard Worker #define tdb_chainunlock_read ext2fs_tdb_chainunlock_read
119*6a54128fSAndroid Build Coastguard Worker #define tdb_dump_all ext2fs_tdb_dump_all
120*6a54128fSAndroid Build Coastguard Worker #define tdb_printfreelist ext2fs_tdb_printfreelist
121*6a54128fSAndroid Build Coastguard Worker #define tdb_validate_freelist ext2fs_tdb_validate_freelist
122*6a54128fSAndroid Build Coastguard Worker #define tdb_chainlock_mark ext2fs_tdb_chainlock_mark
123*6a54128fSAndroid Build Coastguard Worker #define tdb_chainlock_nonblock ext2fs_tdb_chainlock_nonblock
124*6a54128fSAndroid Build Coastguard Worker #define tdb_chainlock_unmark ext2fs_tdb_chainlock_unmark
125*6a54128fSAndroid Build Coastguard Worker #define tdb_enable_seqnum ext2fs_tdb_enable_seqnum
126*6a54128fSAndroid Build Coastguard Worker #define tdb_increment_seqnum_nonblock ext2fs_tdb_increment_seqnum_nonblock
127*6a54128fSAndroid Build Coastguard Worker #define tdb_lock_nonblock ext2fs_tdb_lock_nonblock
128*6a54128fSAndroid Build Coastguard Worker #define tdb_lockall_mark ext2fs_tdb_lockall_mark
129*6a54128fSAndroid Build Coastguard Worker #define tdb_lockall_nonblock ext2fs_tdb_lockall_nonblock
130*6a54128fSAndroid Build Coastguard Worker #define tdb_lockall_read_nonblock ext2fs_tdb_lockall_read_nonblock
131*6a54128fSAndroid Build Coastguard Worker #define tdb_lockall_unmark ext2fs_tdb_lockall_unmark
132*6a54128fSAndroid Build Coastguard Worker #define tdb_flush ext2fs_tdb_flush
133*6a54128fSAndroid Build Coastguard Worker 
134*6a54128fSAndroid Build Coastguard Worker /* this is the context structure that is returned from a db open */
135*6a54128fSAndroid Build Coastguard Worker typedef struct tdb_context TDB_CONTEXT;
136*6a54128fSAndroid Build Coastguard Worker 
137*6a54128fSAndroid Build Coastguard Worker typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
138*6a54128fSAndroid Build Coastguard Worker typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
139*6a54128fSAndroid Build Coastguard Worker typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
140*6a54128fSAndroid Build Coastguard Worker 
141*6a54128fSAndroid Build Coastguard Worker struct tdb_logging_context {
142*6a54128fSAndroid Build Coastguard Worker         tdb_log_func log_fn;
143*6a54128fSAndroid Build Coastguard Worker         void *log_private;
144*6a54128fSAndroid Build Coastguard Worker };
145*6a54128fSAndroid Build Coastguard Worker 
146*6a54128fSAndroid Build Coastguard Worker struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
147*6a54128fSAndroid Build Coastguard Worker 		      int open_flags, mode_t mode);
148*6a54128fSAndroid Build Coastguard Worker struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
149*6a54128fSAndroid Build Coastguard Worker 			 int open_flags, mode_t mode,
150*6a54128fSAndroid Build Coastguard Worker 			 const struct tdb_logging_context *log_ctx,
151*6a54128fSAndroid Build Coastguard Worker 			 tdb_hash_func hash_fn);
152*6a54128fSAndroid Build Coastguard Worker void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
153*6a54128fSAndroid Build Coastguard Worker 
154*6a54128fSAndroid Build Coastguard Worker int tdb_reopen(struct tdb_context *tdb);
155*6a54128fSAndroid Build Coastguard Worker int tdb_reopen_all(int parent_longlived);
156*6a54128fSAndroid Build Coastguard Worker void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
157*6a54128fSAndroid Build Coastguard Worker enum TDB_ERROR tdb_error(struct tdb_context *tdb);
158*6a54128fSAndroid Build Coastguard Worker const char *tdb_errorstr(struct tdb_context *tdb);
159*6a54128fSAndroid Build Coastguard Worker TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
160*6a54128fSAndroid Build Coastguard Worker int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
161*6a54128fSAndroid Build Coastguard Worker 		     int (*parser)(TDB_DATA key, TDB_DATA data,
162*6a54128fSAndroid Build Coastguard Worker 				   void *private_data),
163*6a54128fSAndroid Build Coastguard Worker 		     void *private_data);
164*6a54128fSAndroid Build Coastguard Worker int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
165*6a54128fSAndroid Build Coastguard Worker int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
166*6a54128fSAndroid Build Coastguard Worker int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
167*6a54128fSAndroid Build Coastguard Worker int tdb_close(struct tdb_context *tdb);
168*6a54128fSAndroid Build Coastguard Worker TDB_DATA tdb_firstkey(struct tdb_context *tdb);
169*6a54128fSAndroid Build Coastguard Worker TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
170*6a54128fSAndroid Build Coastguard Worker int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *);
171*6a54128fSAndroid Build Coastguard Worker int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *);
172*6a54128fSAndroid Build Coastguard Worker int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
173*6a54128fSAndroid Build Coastguard Worker int tdb_lockall(struct tdb_context *tdb);
174*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_nonblock(struct tdb_context *tdb);
175*6a54128fSAndroid Build Coastguard Worker int tdb_unlockall(struct tdb_context *tdb);
176*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_read(struct tdb_context *tdb);
177*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_read_nonblock(struct tdb_context *tdb);
178*6a54128fSAndroid Build Coastguard Worker int tdb_unlockall_read(struct tdb_context *tdb);
179*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_mark(struct tdb_context *tdb);
180*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_unmark(struct tdb_context *tdb);
181*6a54128fSAndroid Build Coastguard Worker const char *tdb_name(struct tdb_context *tdb);
182*6a54128fSAndroid Build Coastguard Worker int tdb_fd(struct tdb_context *tdb);
183*6a54128fSAndroid Build Coastguard Worker tdb_log_func tdb_log_fn(struct tdb_context *tdb);
184*6a54128fSAndroid Build Coastguard Worker void *tdb_get_logging_private(struct tdb_context *tdb);
185*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_start(struct tdb_context *tdb);
186*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_commit(struct tdb_context *tdb);
187*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_cancel(struct tdb_context *tdb);
188*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_recover(struct tdb_context *tdb);
189*6a54128fSAndroid Build Coastguard Worker int tdb_get_seqnum(struct tdb_context *tdb);
190*6a54128fSAndroid Build Coastguard Worker int tdb_hash_size(struct tdb_context *tdb);
191*6a54128fSAndroid Build Coastguard Worker size_t tdb_map_size(struct tdb_context *tdb);
192*6a54128fSAndroid Build Coastguard Worker int tdb_get_flags(struct tdb_context *tdb);
193*6a54128fSAndroid Build Coastguard Worker void tdb_enable_seqnum(struct tdb_context *tdb);
194*6a54128fSAndroid Build Coastguard Worker void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
195*6a54128fSAndroid Build Coastguard Worker int tdb_flush(struct tdb_context *tdb);
196*6a54128fSAndroid Build Coastguard Worker 
197*6a54128fSAndroid Build Coastguard Worker /* Low level locking functions: use with care */
198*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
199*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key);
200*6a54128fSAndroid Build Coastguard Worker int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
201*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
202*6a54128fSAndroid Build Coastguard Worker int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
203*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key);
204*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key);
205*6a54128fSAndroid Build Coastguard Worker 
206*6a54128fSAndroid Build Coastguard Worker /* Debug functions. Not used in production. */
207*6a54128fSAndroid Build Coastguard Worker void tdb_dump_all(struct tdb_context *tdb);
208*6a54128fSAndroid Build Coastguard Worker int tdb_printfreelist(struct tdb_context *tdb);
209*6a54128fSAndroid Build Coastguard Worker int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
210*6a54128fSAndroid Build Coastguard Worker 
211*6a54128fSAndroid Build Coastguard Worker #ifdef  __cplusplus
212*6a54128fSAndroid Build Coastguard Worker }
213*6a54128fSAndroid Build Coastguard Worker #endif
214*6a54128fSAndroid Build Coastguard Worker 
215*6a54128fSAndroid Build Coastguard Worker #endif /* tdb.h */
216