r299: updating release notes
[Samba/gebeck_regimport.git] / source3 / tdb / tdb.h
blobeb120a8cecd4e67ce906a22b6bd5a8d01a4ff331
1 #ifndef __TDB_H__
2 #define __TDB_H__
4 /*
5 Unix SMB/CIFS implementation.
7 trivial database library
9 Copyright (C) Andrew Tridgell 1999-2004
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
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
35 /* flags to tdb_store() */
36 #define TDB_REPLACE 1
37 #define TDB_INSERT 2
38 #define TDB_MODIFY 3
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) */
49 #define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
51 /* error codes */
52 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
53 TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
54 TDB_ERR_NOEXIST};
56 #ifndef u32
57 #define u32 unsigned
58 #endif
60 typedef struct {
61 char *dptr;
62 size_t dsize;
63 } TDB_DATA;
65 typedef u32 tdb_len;
66 typedef u32 tdb_off;
68 /* this is stored at the front of every database */
69 struct tdb_header {
70 char magic_food[32]; /* for /etc/magic */
71 u32 version; /* version of the code */
72 u32 hash_size; /* number of hash entries */
73 tdb_off rwlocks;
74 tdb_off reserved[31];
77 struct tdb_lock_type {
78 u32 count;
79 u32 ltype;
82 struct tdb_traverse_lock {
83 struct tdb_traverse_lock *next;
84 u32 off;
85 u32 hash;
88 /* this is the context structure that is returned from a db open */
89 typedef struct tdb_context {
90 char *name; /* the name of the database */
91 void *map_ptr; /* where it is currently mapped */
92 int fd; /* open file descriptor for the database */
93 tdb_len map_size; /* how much space has been mapped */
94 int read_only; /* opened read-only */
95 struct tdb_lock_type *locked; /* array of chain locks */
96 enum TDB_ERROR ecode; /* error code for last tdb error */
97 struct tdb_header header; /* a cached copy of the header */
98 u32 flags; /* the flags passed to tdb_open */
99 u32 *lockedkeys; /* array of locked keys: first is #keys */
100 struct tdb_traverse_lock travlocks; /* current traversal locks */
101 struct tdb_context *next; /* all tdbs to avoid multiple opens */
102 dev_t device; /* uniquely identifies this tdb */
103 ino_t inode; /* uniquely identifies this tdb */
104 void (*log_fn)(struct tdb_context *tdb, int level, const char *, ...); /* logging function */
105 int open_flags; /* flags used in the open - needed by reopen */
106 } TDB_CONTEXT;
108 typedef int (*tdb_traverse_func)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *);
109 typedef void (*tdb_log_func)(TDB_CONTEXT *, int , const char *, ...);
111 TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags,
112 int open_flags, mode_t mode);
113 TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
114 int open_flags, mode_t mode,
115 tdb_log_func log_fn);
117 int tdb_reopen(TDB_CONTEXT *tdb);
118 int tdb_reopen_all(void);
119 void tdb_logging_function(TDB_CONTEXT *tdb, tdb_log_func);
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_append(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf);
126 int tdb_close(TDB_CONTEXT *tdb);
127 TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb);
128 TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key);
129 int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *);
130 int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key);
131 int tdb_lockkeys(TDB_CONTEXT *tdb, u32 number, TDB_DATA keys[]);
132 void tdb_unlockkeys(TDB_CONTEXT *tdb);
133 int tdb_lockall(TDB_CONTEXT *tdb);
134 void tdb_unlockall(TDB_CONTEXT *tdb);
136 /* Low level locking functions: use with care */
137 void tdb_set_lock_alarm(sig_atomic_t *palarm);
138 int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key);
139 int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key);
141 /* Debug functions. Not used in production. */
142 void tdb_dump_all(TDB_CONTEXT *tdb);
143 int tdb_printfreelist(TDB_CONTEXT *tdb);
145 extern TDB_DATA tdb_null;
147 #ifdef __cplusplus
149 #endif
151 #endif /* tdb.h */