2000-12-06 03:05:15 +03:00
# ifndef __TDB_H__
# define __TDB_H__
1999-12-21 06:04:37 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1999-12-21 06:04:37 +03:00
2004-03-28 06:19:58 +04:00
trivial database library
1999-12-21 06:04:37 +03:00
2004-03-28 06:19:58 +04:00
Copyright ( C ) Andrew Tridgell 1999 - 2004
* * NOTE ! The following LGPL license applies to the tdb
* * library . This does NOT imply that all of Samba is released
* * under the LGPL
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 2 of the License , or ( at your option ) any later version .
This library is distributed in the hope that it will be useful ,
1999-12-21 06:04:37 +03:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
2004-03-28 06:19:58 +04:00
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Lesser General Public License for more details .
1999-12-21 06:04:37 +03:00
2004-03-28 06:19:58 +04:00
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , write to the Free Software
Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
1999-12-21 06:04:37 +03:00
*/
2000-12-06 03:05:15 +03:00
# ifdef __cplusplus
extern " C " {
# endif
1999-12-21 06:04:37 +03:00
2004-09-07 17:51:03 +04:00
# ifndef PRINTF_ATTRIBUTE
2004-09-08 06:48:27 +04:00
/** Use gcc attribute to check printf fns. a1 is the 1-based index of
* the parameter containing the format , and a2 the index of the first
* argument . Note that some gcc 2. x versions don ' t handle this
* properly * */
2004-11-16 00:21:50 +03:00
# if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
2004-09-07 17:51:03 +04:00
# define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
2004-09-08 06:48:27 +04:00
# else
# define PRINTF_ATTRIBUTE(a1, a2)
# endif
2004-09-07 17:51:03 +04:00
# endif
2000-01-07 06:01:55 +03:00
2000-12-06 03:05:15 +03:00
/* flags to tdb_store() */
# define TDB_REPLACE 1
# define TDB_INSERT 2
# define TDB_MODIFY 3
/* flags for tdb_open() */
# define TDB_DEFAULT 0 /* just a readability place holder */
# define TDB_CLEAR_IF_FIRST 1
# define TDB_INTERNAL 2 /* don't store on disk */
# define TDB_NOLOCK 4 /* don't do any locking */
# define TDB_NOMMAP 8 /* don't use mmap */
# define TDB_CONVERT 16 /* convert endian (internal use) */
2002-07-15 14:35:28 +04:00
# define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
2000-12-06 03:05:15 +03:00
# define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
/* error codes */
enum TDB_ERROR { TDB_SUCCESS = 0 , TDB_ERR_CORRUPT , TDB_ERR_IO , TDB_ERR_LOCK ,
2004-03-28 06:19:58 +04:00
TDB_ERR_OOM , TDB_ERR_EXISTS , TDB_ERR_NOLOCK , TDB_ERR_LOCK_TIMEOUT ,
TDB_ERR_NOEXIST } ;
2000-12-06 03:05:15 +03:00
# ifndef u32
# define u32 unsigned
# endif
1999-12-21 06:04:37 +03:00
typedef struct {
char * dptr ;
size_t dsize ;
} TDB_DATA ;
2000-12-06 03:05:15 +03:00
typedef u32 tdb_len ;
typedef u32 tdb_off ;
/* this is stored at the front of every database */
struct tdb_header {
char magic_food [ 32 ] ; /* for /etc/magic */
u32 version ; /* version of the code */
u32 hash_size ; /* number of hash entries */
tdb_off rwlocks ;
tdb_off reserved [ 31 ] ;
} ;
2000-06-15 19:30:37 +04:00
struct tdb_lock_type {
2000-12-06 03:05:15 +03:00
u32 count ;
u32 ltype ;
} ;
struct tdb_traverse_lock {
struct tdb_traverse_lock * next ;
u32 off ;
u32 hash ;
2000-06-15 19:30:37 +04:00
} ;
1999-12-21 06:04:37 +03:00
/* this is the context structure that is returned from a db open */
2000-12-06 03:05:15 +03:00
typedef struct tdb_context {
1999-12-21 06:04:37 +03:00
char * name ; /* the name of the database */
void * map_ptr ; /* where it is currently mapped */
int fd ; /* open file descriptor for the database */
tdb_len map_size ; /* how much space has been mapped */
1999-12-21 07:54:30 +03:00
int read_only ; /* opened read-only */
2000-12-06 03:05:15 +03:00
struct tdb_lock_type * locked ; /* array of chain locks */
enum TDB_ERROR ecode ; /* error code for last tdb error */
1999-12-21 06:04:37 +03:00
struct tdb_header header ; /* a cached copy of the header */
2000-12-06 03:05:15 +03:00
u32 flags ; /* the flags passed to tdb_open */
struct tdb_traverse_lock travlocks ; /* current traversal locks */
struct tdb_context * next ; /* all tdbs to avoid multiple opens */
dev_t device ; /* uniquely identifies this tdb */
ino_t inode ; /* uniquely identifies this tdb */
2004-09-07 17:51:03 +04:00
void ( * log_fn ) ( struct tdb_context * tdb , int level , const char * , . . . ) PRINTF_ATTRIBUTE ( 3 , 4 ) ; /* logging function */
2004-08-25 02:48:49 +04:00
u32 ( * hash_fn ) ( TDB_DATA * key ) ;
2001-09-19 09:43:15 +04:00
int open_flags ; /* flags used in the open - needed by reopen */
1999-12-21 06:04:37 +03:00
} TDB_CONTEXT ;
2000-04-19 00:41:04 +04:00
typedef int ( * tdb_traverse_func ) ( TDB_CONTEXT * , TDB_DATA , TDB_DATA , void * ) ;
2001-12-04 16:21:15 +03:00
typedef void ( * tdb_log_func ) ( TDB_CONTEXT * , int , const char * , . . . ) ;
2004-08-25 02:48:49 +04:00
typedef u32 ( * tdb_hash_func ) ( TDB_DATA * key ) ;
2001-12-04 14:41:12 +03:00
2001-12-11 11:31:58 +03:00
TDB_CONTEXT * tdb_open ( const char * name , int hash_size , int tdb_flags ,
2001-12-04 14:25:44 +03:00
int open_flags , mode_t mode ) ;
2001-12-11 11:31:58 +03:00
TDB_CONTEXT * tdb_open_ex ( const char * name , int hash_size , int tdb_flags ,
2001-12-04 16:21:15 +03:00
int open_flags , mode_t mode ,
2004-08-25 05:04:02 +04:00
tdb_log_func log_fn ,
tdb_hash_func hash_fn ) ;
2001-12-04 16:21:15 +03:00
int tdb_reopen ( TDB_CONTEXT * tdb ) ;
int tdb_reopen_all ( void ) ;
void tdb_logging_function ( TDB_CONTEXT * tdb , tdb_log_func ) ;
2000-12-06 03:05:15 +03:00
enum TDB_ERROR tdb_error ( TDB_CONTEXT * tdb ) ;
const char * tdb_errorstr ( TDB_CONTEXT * tdb ) ;
1999-12-21 06:04:37 +03:00
TDB_DATA tdb_fetch ( TDB_CONTEXT * tdb , TDB_DATA key ) ;
int tdb_delete ( TDB_CONTEXT * tdb , TDB_DATA key ) ;
int tdb_store ( TDB_CONTEXT * tdb , TDB_DATA key , TDB_DATA dbuf , int flag ) ;
2003-01-11 05:31:22 +03:00
int tdb_append ( TDB_CONTEXT * tdb , TDB_DATA key , TDB_DATA new_dbuf ) ;
1999-12-21 06:04:37 +03:00
int tdb_close ( TDB_CONTEXT * tdb ) ;
TDB_DATA tdb_firstkey ( TDB_CONTEXT * tdb ) ;
TDB_DATA tdb_nextkey ( TDB_CONTEXT * tdb , TDB_DATA key ) ;
2004-03-28 06:19:58 +04:00
int tdb_traverse ( TDB_CONTEXT * tdb , tdb_traverse_func fn , void * ) ;
1999-12-21 06:04:37 +03:00
int tdb_exists ( TDB_CONTEXT * tdb , TDB_DATA key ) ;
2000-12-06 03:05:15 +03:00
int tdb_lockall ( TDB_CONTEXT * tdb ) ;
void tdb_unlockall ( TDB_CONTEXT * tdb ) ;
/* Low level locking functions: use with care */
2002-09-25 19:19:00 +04:00
void tdb_set_lock_alarm ( sig_atomic_t * palarm ) ;
2000-12-06 03:05:15 +03:00
int tdb_chainlock ( TDB_CONTEXT * tdb , TDB_DATA key ) ;
2002-04-08 02:02:09 +04:00
int tdb_chainunlock ( TDB_CONTEXT * tdb , TDB_DATA key ) ;
2001-12-04 16:21:15 +03:00
/* Debug functions. Not used in production. */
void tdb_dump_all ( TDB_CONTEXT * tdb ) ;
2002-04-08 02:02:09 +04:00
int tdb_printfreelist ( TDB_CONTEXT * tdb ) ;
2001-12-04 16:21:15 +03:00
2000-12-06 03:05:15 +03:00
extern TDB_DATA tdb_null ;
2002-01-03 02:28:55 +03:00
2000-12-06 03:05:15 +03:00
# ifdef __cplusplus
}
1999-12-21 06:04:37 +03:00
# endif
2000-12-06 03:05:15 +03:00
# endif /* tdb.h */