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 31extern "C" { 32#endif 33 34#ifndef PRINTF_ATTRIBUTE 35/** Use gcc attribute to check printf fns. a1 is the 1-based index of 36 * the parameter containing the format, and a2 the index of the first 37 * argument. Note that some gcc 2.x versions don't handle this 38 * properly **/ 39#if (__GNUC__ >= 3) 40#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2))) 41#else 42#define PRINTF_ATTRIBUTE(a1, a2) 43#endif 44#endif 45 46/* flags to tdb_store() */ 47#define TDB_REPLACE 1 48#define TDB_INSERT 2 49#define TDB_MODIFY 3 50 51/* flags for tdb_open() */ 52#define TDB_DEFAULT 0 /* just a readability place holder */ 53#define TDB_CLEAR_IF_FIRST 1 54#define TDB_INTERNAL 2 /* don't store on disk */ 55#define TDB_NOLOCK 4 /* don't do any locking */ 56#define TDB_NOMMAP 8 /* don't use mmap */ 57#define TDB_CONVERT 16 /* convert endian (internal use) */ 58#define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */ 59 60#define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret) 61 62/* error codes */ 63enum 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}; 66 67#ifndef u32 68#define u32 unsigned 69#endif 70 71typedef struct { 72 char *dptr; 73 size_t dsize; 74} TDB_DATA; 75 76typedef u32 tdb_len; 77typedef u32 tdb_off; 78 79/* this is stored at the front of every database */ 80struct tdb_header { 81 char magic_food[32]; /* for /etc/magic */ 82 u32 version; /* version of the code */ 83 u32 hash_size; /* number of hash entries */ 84 tdb_off rwlocks; 85 tdb_off reserved[31]; 86}; 87 88struct tdb_lock_type { 89 u32 count; 90 u32 ltype; 91}; 92 93struct tdb_traverse_lock { 94 struct tdb_traverse_lock *next; 95 u32 off; 96 u32 hash; 97}; 98 99/* this is the context structure that is returned from a db open */ 100typedef struct tdb_context { 101 char *name; /* the name of the database */ 102 void *map_ptr; /* where it is currently mapped */ 103 int fd; /* open file descriptor for the database */ 104 tdb_len map_size; /* how much space has been mapped */ 105 int read_only; /* opened read-only */ 106 struct tdb_lock_type *locked; /* array of chain locks */ 107 enum TDB_ERROR ecode; /* error code for last tdb error */ 108 struct tdb_header header; /* a cached copy of the header */ 109 u32 flags; /* the flags passed to tdb_open */ 110 struct tdb_traverse_lock travlocks; /* current traversal locks */ 111 struct tdb_context *next; /* all tdbs to avoid multiple opens */ 112 dev_t device; /* uniquely identifies this tdb */ 113 ino_t inode; /* uniquely identifies this tdb */ 114 void (*log_fn)(struct tdb_context *tdb, int level, const char *, ...) PRINTF_ATTRIBUTE(3,4); /* logging function */ 115 u32 (*hash_fn)(TDB_DATA *key); 116 int open_flags; /* flags used in the open - needed by reopen */ 117} TDB_CONTEXT; 118 119typedef int (*tdb_traverse_func)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *); 120typedef void (*tdb_log_func)(TDB_CONTEXT *, int , const char *, ...); 121typedef u32 (*tdb_hash_func)(TDB_DATA *key); 122 123TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags, 124 int open_flags, mode_t mode); 125TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags, 126 int open_flags, mode_t mode, 127 tdb_log_func log_fn, 128 tdb_hash_func hash_fn); 129 130int tdb_reopen(TDB_CONTEXT *tdb); 131int tdb_reopen_all(void); 132void tdb_logging_function(TDB_CONTEXT *tdb, tdb_log_func); 133enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb); 134const char *tdb_errorstr(TDB_CONTEXT *tdb); 135TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key); 136int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key); 137int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag); 138int tdb_append(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf); 139int tdb_close(TDB_CONTEXT *tdb); 140TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb); 141TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key); 142int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *); 143int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key); 144int tdb_lockkeys(TDB_CONTEXT *tdb, u32 number, TDB_DATA keys[]); 145void tdb_unlockkeys(TDB_CONTEXT *tdb); 146int tdb_lockall(TDB_CONTEXT *tdb); 147void tdb_unlockall(TDB_CONTEXT *tdb); 148 149/* Low level locking functions: use with care */ 150void tdb_set_lock_alarm(sig_atomic_t *palarm); 151int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key); 152int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key); 153 154/* Debug functions. Not used in production. */ 155void tdb_dump_all(TDB_CONTEXT *tdb); 156int tdb_printfreelist(TDB_CONTEXT *tdb); 157 158extern TDB_DATA tdb_null; 159 160#ifdef __cplusplus 161} 162#endif 163 164#endif /* tdb.h */ 165