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, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #ifdef  __cplusplus
30 extern "C" {
31 #endif
32 
33 
34 /* flags to tdb_store() */
35 #define TDB_REPLACE 1
36 #define TDB_INSERT 2
37 #define TDB_MODIFY 3
38 
39 /* flags for tdb_open() */
40 #define TDB_DEFAULT 0 /* just a readability place holder */
41 #define TDB_CLEAR_IF_FIRST 1
42 #define TDB_INTERNAL 2 /* don't store on disk */
43 #define TDB_NOLOCK   4 /* don't do any locking */
44 #define TDB_NOMMAP   8 /* don't use mmap */
45 #define TDB_CONVERT 16 /* convert endian (internal use) */
46 #define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
47 
48 #define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
49 
50 /* error codes */
51 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
52 		TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
53 		TDB_ERR_NOEXIST};
54 
55 #ifndef uint32_t
56 #define uint32_t unsigned
57 #endif
58 
59 typedef struct TDB_DATA {
60 	char *dptr;
61 	size_t dsize;
62 } TDB_DATA;
63 
64 typedef uint32_t tdb_len;
65 typedef uint32_t tdb_off;
66 
67 /* this is stored at the front of every database */
68 struct tdb_header {
69 	char magic_food[32]; /* for /etc/magic */
70 	uint32_t version; /* version of the code */
71 	uint32_t hash_size; /* number of hash entries */
72 	tdb_off rwlocks;
73 	tdb_off reserved[31];
74 };
75 
76 struct tdb_lock_type {
77 	uint32_t count;
78 	uint32_t ltype;
79 };
80 
81 struct tdb_traverse_lock {
82 	struct tdb_traverse_lock *next;
83 	uint32_t off;
84 	uint32_t hash;
85 };
86 
87 #ifndef PRINTF_ATTRIBUTE
88 #define PRINTF_ATTRIBUTE(a,b)
89 #endif
90 
91 /* this is the context structure that is returned from a db open */
92 typedef struct tdb_context {
93 	char *name; /* the name of the database */
94 	void *map_ptr; /* where it is currently mapped */
95 	int fd; /* open file descriptor for the database */
96 	tdb_len map_size; /* how much space has been mapped */
97 	int read_only; /* opened read-only */
98 	struct tdb_lock_type *locked; /* array of chain locks */
99 	enum TDB_ERROR ecode; /* error code for last tdb error */
100 	struct tdb_header header; /* a cached copy of the header */
101 	uint32_t flags; /* the flags passed to tdb_open */
102 	struct tdb_traverse_lock travlocks; /* current traversal locks */
103 	struct tdb_context *next; /* all tdbs to avoid multiple opens */
104 	dev_t device;	/* uniquely identifies this tdb */
105 	ino_t inode;	/* uniquely identifies this tdb */
106 	void (*log_fn)(struct tdb_context *tdb, int level, const char *, ...) PRINTF_ATTRIBUTE(3,4); /* logging function */
107 	uint32_t (*hash_fn)(TDB_DATA *key);
108 	int open_flags; /* flags used in the open - needed by reopen */
109 } TDB_CONTEXT;
110 
111 typedef int (*tdb_traverse_func)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *);
112 typedef void (*tdb_log_func)(TDB_CONTEXT *, int , const char *, ...);
113 typedef uint32_t (*tdb_hash_func)(TDB_DATA *key);
114 
115 TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
116 			 int open_flags, mode_t mode,
117 			 tdb_log_func log_fn,
118 			 tdb_hash_func hash_fn);
119 
120 enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb);
121 const char *tdb_errorstr(TDB_CONTEXT *tdb);
122 TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key);
123 int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key);
124 int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
125 int tdb_close(TDB_CONTEXT *tdb);
126 TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb);
127 TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key);
128 int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *);
129 
130 #ifdef  __cplusplus
131 }
132 #endif
133 
134 #endif /* tdb.h */
135