| 1 |
#ifndef PPP_TDB_H |
| 2 |
#define PPP_TDB_H |
| 3 |
|
| 4 |
#include "pppdconf.h" |
| 5 |
|
| 6 |
/* |
| 7 |
Unix SMB/CIFS implementation. |
| 8 |
|
| 9 |
trivial database library |
| 10 |
|
| 11 |
Copyright (C) Andrew Tridgell 1999-2004 |
| 12 |
|
| 13 |
** NOTE! The following LGPL license applies to the tdb |
| 14 |
** library. This does NOT imply that all of Samba is released |
| 15 |
** under the LGPL |
| 16 |
|
| 17 |
This library is free software; you can redistribute it and/or |
| 18 |
modify it under the terms of the GNU Lesser General Public |
| 19 |
License as published by the Free Software Foundation; either |
| 20 |
version 2 of the License, or (at your option) any later version. |
| 21 |
|
| 22 |
This library is distributed in the hope that it will be useful, |
| 23 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 |
Lesser General Public License for more details. |
| 26 |
|
| 27 |
You should have received a copy of the GNU Lesser General Public |
| 28 |
License along with this library; if not, write to the Free Software |
| 29 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 30 |
*/ |
| 31 |
|
| 32 |
#ifdef __cplusplus |
| 33 |
extern "C" { |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifndef PRINTF_ATTRIBUTE |
| 37 |
/** Use gcc attribute to check printf fns. a1 is the 1-based index of |
| 38 |
* the parameter containing the format, and a2 the index of the first |
| 39 |
* argument. Note that some gcc 2.x versions don't handle this |
| 40 |
* properly **/ |
| 41 |
#if (__GNUC__ >= 3) |
| 42 |
#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2))) |
| 43 |
#else |
| 44 |
#define PRINTF_ATTRIBUTE(a1, a2) |
| 45 |
#endif |
| 46 |
#endif |
| 47 |
|
| 48 |
/* flags to tdb_store() */ |
| 49 |
#define TDB_REPLACE 1 |
| 50 |
#define TDB_INSERT 2 |
| 51 |
#define TDB_MODIFY 3 |
| 52 |
|
| 53 |
/* flags for tdb_open() */ |
| 54 |
#define TDB_DEFAULT 0 /* just a readability place holder */ |
| 55 |
#define TDB_CLEAR_IF_FIRST 1 |
| 56 |
#define TDB_INTERNAL 2 /* don't store on disk */ |
| 57 |
#define TDB_NOLOCK 4 /* don't do any locking */ |
| 58 |
#define TDB_NOMMAP 8 /* don't use mmap */ |
| 59 |
#define TDB_CONVERT 16 /* convert endian (internal use) */ |
| 60 |
#define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */ |
| 61 |
|
| 62 |
#define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret) |
| 63 |
|
| 64 |
/* error codes */ |
| 65 |
enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, |
| 66 |
TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT, |
| 67 |
TDB_ERR_NOEXIST}; |
| 68 |
|
| 69 |
#ifndef u32 |
| 70 |
#define u32 unsigned |
| 71 |
#endif |
| 72 |
|
| 73 |
typedef struct { |
| 74 |
char *dptr; |
| 75 |
size_t dsize; |
| 76 |
} TDB_DATA; |
| 77 |
|
| 78 |
typedef u32 tdb_len; |
| 79 |
typedef u32 tdb_off; |
| 80 |
|
| 81 |
/* this is stored at the front of every database */ |
| 82 |
struct tdb_header { |
| 83 |
char magic_food[32]; /* for /etc/magic */ |
| 84 |
u32 version; /* version of the code */ |
| 85 |
u32 hash_size; /* number of hash entries */ |
| 86 |
tdb_off rwlocks; |
| 87 |
tdb_off reserved[31]; |
| 88 |
}; |
| 89 |
|
| 90 |
struct tdb_lock_type { |
| 91 |
u32 count; |
| 92 |
u32 ltype; |
| 93 |
}; |
| 94 |
|
| 95 |
struct tdb_traverse_lock { |
| 96 |
struct tdb_traverse_lock *next; |
| 97 |
u32 off; |
| 98 |
u32 hash; |
| 99 |
}; |
| 100 |
|
| 101 |
/* this is the context structure that is returned from a db open */ |
| 102 |
typedef struct tdb_context { |
| 103 |
char *name; /* the name of the database */ |
| 104 |
void *map_ptr; /* where it is currently mapped */ |
| 105 |
int fd; /* open file descriptor for the database */ |
| 106 |
tdb_len map_size; /* how much space has been mapped */ |
| 107 |
int read_only; /* opened read-only */ |
| 108 |
struct tdb_lock_type *locked; /* array of chain locks */ |
| 109 |
enum TDB_ERROR ecode; /* error code for last tdb error */ |
| 110 |
struct tdb_header header; /* a cached copy of the header */ |
| 111 |
u32 flags; /* the flags passed to tdb_open */ |
| 112 |
struct tdb_traverse_lock travlocks; /* current traversal locks */ |
| 113 |
struct tdb_context *next; /* all tdbs to avoid multiple opens */ |
| 114 |
dev_t device; /* uniquely identifies this tdb */ |
| 115 |
ino_t inode; /* uniquely identifies this tdb */ |
| 116 |
void (*log_fn)(struct tdb_context *tdb, int level, const char *, ...) PRINTF_ATTRIBUTE(3,4); /* logging function */ |
| 117 |
u32 (*hash_fn)(TDB_DATA *key); |
| 118 |
int open_flags; /* flags used in the open - needed by reopen */ |
| 119 |
} TDB_CONTEXT; |
| 120 |
|
| 121 |
typedef int (*tdb_traverse_func)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *); |
| 122 |
typedef void (*tdb_log_func)(TDB_CONTEXT *, int , const char *, ...); |
| 123 |
typedef u32 (*tdb_hash_func)(TDB_DATA *key); |
| 124 |
|
| 125 |
TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags, |
| 126 |
int open_flags, mode_t mode); |
| 127 |
TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags, |
| 128 |
int open_flags, mode_t mode, |
| 129 |
tdb_log_func log_fn, |
| 130 |
tdb_hash_func hash_fn); |
| 131 |
|
| 132 |
enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb); |
| 133 |
const char *tdb_errorstr(TDB_CONTEXT *tdb); |
| 134 |
TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key); |
| 135 |
int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key); |
| 136 |
int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag); |
| 137 |
int tdb_close(TDB_CONTEXT *tdb); |
| 138 |
int tdb_lockkeys(TDB_CONTEXT *tdb, u32 number, TDB_DATA keys[]); |
| 139 |
void tdb_unlockkeys(TDB_CONTEXT *tdb); |
| 140 |
|
| 141 |
/* Low level locking functions: use with care */ |
| 142 |
int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key); |
| 143 |
int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key); |
| 144 |
|
| 145 |
extern TDB_DATA tdb_null; |
| 146 |
|
| 147 |
#ifdef __cplusplus |
| 148 |
} |
| 149 |
#endif |
| 150 |
|
| 151 |
#endif /* PPP_TBD_H */ |