r10253: a fairly large tdb cleanup and re-organise. Nearly all of this change
[ira/wip.git] / source / lib / tdb / include / tdb.h
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 */
29
30 #ifdef  __cplusplus
31 extern "C" {
32 #endif
33
34
35 /* flags to tdb_store() */
36 #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
49 #define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
50
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};
55
56 typedef struct TDB_DATA {
57         unsigned char *dptr;
58         size_t dsize;
59 } TDB_DATA;
60
61 #ifndef PRINTF_ATTRIBUTE
62 #define PRINTF_ATTRIBUTE(a,b)
63 #endif
64
65 /* this is the context structure that is returned from a db open */
66 typedef struct tdb_context TDB_CONTEXT;
67
68 typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
69 typedef void (*tdb_log_func)(struct tdb_context *, int , const char *, ...);
70 typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
71
72 struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
73                       int open_flags, mode_t mode);
74 struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
75                          int open_flags, mode_t mode,
76                          tdb_log_func log_fn,
77                          tdb_hash_func hash_fn);
78
79 int tdb_reopen(struct tdb_context *tdb);
80 int tdb_reopen_all(void);
81 void tdb_logging_function(struct tdb_context *tdb, tdb_log_func);
82 enum TDB_ERROR tdb_error(struct tdb_context *tdb);
83 const char *tdb_errorstr(struct tdb_context *tdb);
84 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
85 int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
86 int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
87 int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
88 int tdb_close(struct tdb_context *tdb);
89 TDB_DATA tdb_firstkey(struct tdb_context *tdb);
90 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
91 int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *);
92 int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
93 int tdb_lockall(struct tdb_context *tdb);
94 void tdb_unlockall(struct tdb_context *tdb);
95 const char *tdb_name(struct tdb_context *tdb);
96 int tdb_fd(struct tdb_context *tdb);
97 tdb_log_func tdb_log_fn(struct tdb_context *tdb);
98
99 /* Low level locking functions: use with care */
100 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
101 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
102 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
103 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
104
105 /* Debug functions. Not used in production. */
106 void tdb_dump_all(struct tdb_context *tdb);
107 int tdb_printfreelist(struct tdb_context *tdb);
108
109 extern TDB_DATA tdb_null;
110
111 #ifdef  __cplusplus
112 }
113 #endif
114
115 #endif /* tdb.h */