]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb2.h
7032cd5006675be511be121aff8522a778e321f0
[ccan] / ccan / tdb2 / tdb2.h
1 #ifndef CCAN_TDB2_H
2 #define CCAN_TDB2_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 3 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 #ifndef _SAMBA_BUILD_
34 /* For mode_t */
35 #include <sys/types.h>
36 /* For O_* flags. */
37 #include <sys/stat.h>
38 /* For sig_atomic_t. */
39 #include <signal.h>
40 /* For uint64_t */
41 #include <stdint.h>
42 #endif
43
44 /* flags to tdb_store() */
45 #define TDB_REPLACE 1           /* Unused */
46 #define TDB_INSERT 2            /* Don't overwrite an existing entry */
47 #define TDB_MODIFY 3            /* Don't create an existing entry    */
48
49 /* flags for tdb_open() */
50 #define TDB_DEFAULT 0 /* just a readability place holder */
51 #define TDB_CLEAR_IF_FIRST 1
52 #define TDB_INTERNAL 2 /* don't store on disk */
53 #define TDB_NOLOCK   4 /* don't do any locking */
54 #define TDB_NOMMAP   8 /* don't use mmap */
55 #define TDB_CONVERT 16 /* convert endian */
56 #define TDB_NOSYNC   64 /* don't use synchronous transactions */
57 #define TDB_SEQNUM   128 /* maintain a sequence number */
58 #define TDB_VOLATILE   256 /* Activate the per-hashchain freelist, default 5 */
59 #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
60 #define TDB_DISALLOW_NESTING 1024 /* Disallow transactions to nest */
61
62 /* error codes */
63 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, 
64                 TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
65                 TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
66                 TDB_ERR_NESTING};
67
68 /* debugging uses one of the following levels */
69 enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR, 
70                       TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
71
72 typedef struct tdb_data {
73         unsigned char *dptr;
74         size_t dsize;
75 } TDB_DATA;
76
77 #ifndef PRINTF_ATTRIBUTE
78 #if (__GNUC__ >= 3)
79 /** Use gcc attribute to check printf fns.  a1 is the 1-based index of
80  * the parameter containing the format, and a2 the index of the first
81  * argument. Note that some gcc 2.x versions don't handle this
82  * properly **/
83 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
84 #else
85 #define PRINTF_ATTRIBUTE(a1, a2)
86 #endif
87 #endif
88
89 struct tdb_context;
90
91 /* FIXME: Make typesafe */
92 typedef void (*tdb_logfn_t)(struct tdb_context *, enum tdb_debug_level, void *priv, const char *, ...) PRINTF_ATTRIBUTE(4, 5);
93 typedef uint64_t (*tdb_hashfn_t)(const void *key, size_t len, uint64_t seed,
94                                  void *priv);
95
96 enum tdb_attribute_type {
97         TDB_ATTRIBUTE_LOG = 0,
98         TDB_ATTRIBUTE_HASH = 1
99 };
100
101 struct tdb_attribute_base {
102         enum tdb_attribute_type attr;
103         union tdb_attribute *next;
104 };
105
106 struct tdb_attribute_log {
107         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
108         tdb_logfn_t log_fn;
109         void *log_private;
110 };
111
112 struct tdb_attribute_hash {
113         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
114         tdb_hashfn_t hash_fn;
115         void *hash_private;
116 };
117
118 union tdb_attribute {
119         struct tdb_attribute_base base;
120         struct tdb_attribute_log log;
121         struct tdb_attribute_hash hash;
122 };
123                 
124 struct tdb_context *tdb_open(const char *name, int tdb_flags,
125                              int open_flags, mode_t mode,
126                              union tdb_attribute *attributes);
127
128 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key);
129 int tdb_delete(struct tdb_context *tdb, struct tdb_data key);
130 int tdb_store(struct tdb_context *tdb, struct tdb_data key, struct tdb_data dbuf, int flag);
131 int tdb_close(struct tdb_context *tdb);
132 int tdb_check(struct tdb_context *tdb,
133               int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
134               void *private_data);
135
136 enum TDB_ERROR tdb_error(struct tdb_context *tdb);
137
138 extern struct tdb_data tdb_null;
139
140 #ifdef  __cplusplus
141 }
142 #endif
143
144 #endif /* tdb2.h */