openldap/libraries/libldbm/ldbm.c

1151 lines
20 KiB
C
Raw Normal View History

1998-08-09 08:43:13 +08:00
/* ldbm.c - ldap dbm compatibility routines */
/* $OpenLDAP$ */
1999-08-05 07:57:27 +08:00
/*
2000-05-13 10:36:07 +08:00
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
1999-08-05 07:57:27 +08:00
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
1998-08-09 08:43:13 +08:00
/* Patched for Berkeley DB version 2.0; /KSp; 98/02/23
*
* - DB version 2.6.4b ; 1998/12/28, /KSp
* - DB_DBT_MALLOC ; 1998/03/22, /KSp
* - basic implementation; 1998/02/23, /KSp
*/
1998-10-25 10:02:31 +08:00
#include "portable.h"
1998-08-09 08:43:13 +08:00
1998-10-25 10:02:31 +08:00
#ifdef SLAPD_LDBM
1998-08-09 08:43:13 +08:00
1998-10-25 10:02:31 +08:00
#include <stdio.h>
1999-06-03 08:37:44 +08:00
#include <ac/stdlib.h>
#include <ac/string.h>
1998-10-25 10:02:31 +08:00
#include <ac/errno.h>
1998-08-09 08:43:13 +08:00
1998-10-25 10:02:31 +08:00
#include "ldbm.h"
#include "ldap_pvt_thread.h"
1998-08-09 08:43:13 +08:00
void
ldbm_datum_free( LDBM ldbm, Datum data )
{
if ( data.dptr ) {
free( data.dptr );
2000-06-11 06:39:30 +08:00
memset( &data, '\0', sizeof( Datum ));
data.dptr = NULL;
}
}
1998-08-09 08:43:13 +08:00
Datum
ldbm_datum_dup( LDBM ldbm, Datum data )
{
Datum dup;
ldbm_datum_init( dup );
if ( data.dsize == 0 ) {
dup.dsize = 0;
dup.dptr = NULL;
return( dup );
}
dup.dsize = data.dsize;
if ( (dup.dptr = (char *) malloc( data.dsize )) != NULL )
AC_MEMCPY( dup.dptr, data.dptr, data.dsize );
return( dup );
}
static int ldbm_initialized = 0;
2000-05-26 13:47:02 +08:00
#ifdef HAVE_BERKELEY_DB_THREAD
#define LDBM_LOCK ((void)0)
#define LDBM_UNLOCK ((void)0)
#else
/* Only DB2 or DB3 with DB_THREAD is thread-free */
static ldap_pvt_thread_mutex_t ldbm_big_mutex;
#define LDBM_LOCK (ldap_pvt_thread_mutex_lock(&ldbm_big_mutex))
#define LDBM_UNLOCK (ldap_pvt_thread_mutex_unlock(&ldbm_big_mutex))
#endif
/*******************************************************************
* *
* Create some special functions to initialize Berkeley DB for *
* versions greater than 2. *
* *
*******************************************************************/
#if defined( HAVE_BERKELEY_DB ) && (DB_VERSION_MAJOR >= 2)
Vienna Bulk Commit This commit includes many changes. All changes compile under NT but have not been tested under UNIX. A Summary of changes (likely incomplete): NT changes: Removed lint. Clean up configuration support for "Debug", "Release", "SDebug", and "SRelease" configurations. Share output directories for clients, libraries, and slapd. (maybe they should be combined further and moved to build/{,S}{Debug,Release}). Enable threading when _MT is defined. Enable debuging when _DEBUG is defined. Disable setting of NDEBUG under Release/SRelease. Asserts are disabled in <ac/assert.h> when LDAP_DEBUG is not defined. Added 'build/main.dsp' Master project. Removed non-slapd projects from slapd.dsp (see main.dsp). Removed replaced many uses of _WIN32 macro with feature based macros. ldap_cdefs.h changes #define LDAP_CONST const (see below) #define LDAP_F(type) LDAP_F_PRE type LDAP_F_POST To allow specifiers to be added before and after the type declaration. (For DLL handling) LBER/LDAP changes Namespace changes: s/lber_/ber_/ for here and there. s/NAME_ERROR/LDAP_NAME_ERROR/g Deleted NULLMSG and other NULL* macros for namespace reasons. "const" libraries. Installed headers (ie: lber.h, ldap.h) use LDAP_CONST macro. Normally set to 'const' when __STDC__. Can be set externally to enable/disable 'constification' of external interface. Internal interface always uses 'const'. Did not fix warnings in -lldif (in lieu of new LDIF parser). Added _ext API implementations (excepting search and bind). Need to implement ldap_int_get_controls() for reponses with controls. Added numberous assert() checks. LDAP_R _MT defines HAVE_NT_THREADS Added numberous assert() checks. Changed ldap_pthread_t back to unsigned long. Used cast to HANDLE in _join(). LDBM Replaced _WIN32 with HAVE_SYSLOG ud Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). slapd Made connection sockbuf field a pointer to a sockbuf. This removed slap.h dependency on lber-int.h. lber-int.h now only included by those files needing to mess with the sockbuf. Used ber_* functions/macros to access sockbuf internals whenever possible. Added version string if MKVERSION is not defined. (MKVERSION needs to be set under UNIX). Removed FD_SET unsigned lint slapd/tools Used EXEEXT to added ".exe" to routines. Need to define EXEEXT under UNIX. ldappasswd Added ldappasswd.dsp. Ported to NT. Used getpid() to seed rand(). nt_debug Minor cleanup. Added "portable.h" include and used <ac/*.h> where appropriate. Added const to char* format argument.
1999-05-19 09:12:33 +08:00
void *
ldbm_malloc( size_t size )
{
return( calloc( 1, size ));
}
#ifdef LDAP_SYSLOG
#include <ac/syslog.h>
#endif
static void
ldbm_db_errcall( const char *prefix, char *message )
{
#ifdef LDAP_SYSLOG
2000-09-20 08:27:47 +08:00
syslog( LOG_INFO, "ldbm: %s %s", prefix, message );
1999-04-02 00:06:08 +08:00
#endif
}
/* a dbEnv for BERKELEYv2 */
DB_ENV *ldbm_Env = NULL;
int ldbm_initialize( const char* home )
{
int err;
1999-09-07 02:45:39 +08:00
u_int32_t envFlags;
if(ldbm_initialized++) return 1;
2000-05-26 13:47:02 +08:00
#if DB_VERSION_MAJOR < 3
ldbm_Env = calloc( 1, sizeof( DB_ENV ));
if( ldbm_Env == NULL ) return 1;
ldbm_Env->db_errcall = ldbm_db_errcall;
ldbm_Env->db_errpfx = "==>";
2000-05-26 13:47:02 +08:00
#else
ldbm_Env = NULL;
#endif
2000-05-26 13:47:02 +08:00
envFlags =
2000-09-12 01:49:25 +08:00
#if defined( DB_PRIVATE )
DB_PRIVATE |
#endif
#if defined( HAVE_BERKELEY_DB_THREAD )
2000-05-26 13:47:02 +08:00
DB_THREAD |
#endif
2000-05-26 13:47:02 +08:00
DB_CREATE;
#if DB_VERSION_MAJOR >= 3
2000-05-26 13:47:02 +08:00
err = db_env_create( &ldbm_Env, 0 );
#else
envFlags |= DB_USE_ENVIRON;
err = db_appinit( home, NULL, ldbm_Env, envFlags );
#endif
2000-05-26 13:47:02 +08:00
if ( err ) {
char error[BUFSIZ];
sprintf( error, "%s (%d)\n", STRERROR( err ), err );
#ifdef LDAP_SYSLOG
syslog( LOG_INFO,
#if DB_VERSION_MAJOR >= 3
"ldbm_initialize(): FATAL error in db_env_create() : %s\n",
#else
"ldbm_initialize(): FATAL error in db_appinit() : %s\n",
#endif
error );
1999-04-02 00:06:08 +08:00
#endif
return( 1 );
}
2000-05-26 13:47:02 +08:00
#if DB_VERSION_MAJOR > 2
2000-05-26 13:47:02 +08:00
ldbm_Env->set_errcall( ldbm_Env, ldbm_db_errcall );
ldbm_Env->set_errpfx( ldbm_Env, "==>" );
envFlags |= DB_INIT_MPOOL | DB_USE_ENVIRON;
#if (DB_VERSION_MAJOR > 3) || (DB_VERSION_MINOR >= 1)
err = ldbm_Env->open( ldbm_Env, home, envFlags, 0 );
#else
err = ldbm_Env->open( ldbm_Env, home, NULL, envFlags, 0 );
#endif
if ( err != 0 )
{
char error[BUFSIZ];
sprintf( error, "%s (%d)\n", STRERROR( err ), err );
#ifdef LDAP_SYSLOG
syslog( LOG_INFO,
"ldbm_initialize(): FATAL error in dbEnv->open() : %s\n",
error );
#endif
2000-05-26 13:47:02 +08:00
ldbm_Env->close( ldbm_Env, 0 );
return( 1 );
}
#endif
return 0;
}
int ldbm_shutdown( void )
{
if( !ldbm_initialized ) return 1;
#if DB_VERSION_MAJOR >= 3
ldbm_Env->close( ldbm_Env, 0 );
#else
db_appexit( ldbm_Env );
#endif
return 0;
}
#else /* some DB other than Berkeley V2 or greater */
int ldbm_initialize( const char * home )
{
if(ldbm_initialized++) return 1;
ldap_pvt_thread_mutex_init( &ldbm_big_mutex );
return 0;
}
int ldbm_shutdown( void )
{
if( !ldbm_initialized ) return 1;
ldap_pvt_thread_mutex_destroy( &ldbm_big_mutex );
return 0;
}
#endif /* HAVE_BERKELEY_DB */
#if defined( LDBM_USE_DBHASH ) || defined( LDBM_USE_DBBTREE )
/*****************************************************************
* *
* use berkeley db hash or btree package *
* *
*****************************************************************/
LDBM
ldbm_open( char *name, int rw, int mode, int dbcachesize )
{
LDBM ret = NULL;
#if DB_VERSION_MAJOR >= 3
int err;
LDBM_LOCK;
err = db_create( &ret, ldbm_Env, 0 );
if ( err != 0 )
{
char error[BUFSIZ];
sprintf( error, "%s (%d)\n", STRERROR( err ), err );
(void)ret->close(ret, 0);
return NULL;
}
ret->set_pagesize( ret, DEFAULT_DB_PAGE_SIZE );
ret->set_malloc( ret, ldbm_malloc );
ret->set_cachesize( ret, 0, dbcachesize, 0 );
err = ret->open( ret, name, NULL, DB_TYPE, rw, mode);
LDBM_UNLOCK;
if ( err != 0 )
{
char error[BUFSIZ];
sprintf( error, "%s (%d)\n", STRERROR( err ), err );
(void)ret->close(ret, 0);
return NULL;
}
#elif DB_VERSION_MAJOR >= 2
DB_INFO dbinfo;
2000-06-11 06:39:30 +08:00
memset( &dbinfo, '\0', sizeof( dbinfo ));
#if DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR == 4
/*
* BerkeleyDB 2.4 do not allow db_cachesize
* to be specified if an DB_ENV is.
*/
#else
/* set db_cachesize of MPOOL is NOT being used. */
if (( ldbm_Env == NULL ) || ( ldbm_Env->mp_info == NULL ))
dbinfo.db_cachesize = dbcachesize;
#endif
dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
dbinfo.db_malloc = ldbm_malloc;
LDBM_LOCK;
(void) db_open( name, DB_TYPE, rw, mode, ldbm_Env, &dbinfo, &ret );
LDBM_UNLOCK;
#else
1998-08-09 08:43:13 +08:00
void *info;
BTREEINFO binfo;
HASHINFO hinfo;
if ( DB_TYPE == DB_HASH ) {
memset( (char *) &hinfo, '\0', sizeof(hinfo) );
hinfo.cachesize = dbcachesize;
info = &hinfo;
} else if ( DB_TYPE == DB_BTREE ) {
memset( (char *) &binfo, '\0', sizeof(binfo) );
binfo.cachesize = dbcachesize;
info = &binfo;
} else {
info = NULL;
}
LDBM_LOCK;
1998-08-09 08:43:13 +08:00
ret = dbopen( name, rw, mode, DB_TYPE, info );
LDBM_UNLOCK;
#endif
1998-08-09 08:43:13 +08:00
return( ret );
}
void
ldbm_close( LDBM ldbm )
{
LDBM_LOCK;
#if DB_VERSION_MAJOR >= 3
ldbm->close( ldbm, 0 );
#elif DB_VERSION_MAJOR >= 2
(*ldbm->close)( ldbm, 0 );
#else
1998-08-09 08:43:13 +08:00
(*ldbm->close)( ldbm );
#endif
LDBM_UNLOCK;
1998-08-09 08:43:13 +08:00
}
void
ldbm_sync( LDBM ldbm )
{
LDBM_LOCK;
1998-08-09 08:43:13 +08:00
(*ldbm->sync)( ldbm, 0 );
LDBM_UNLOCK;
1998-08-09 08:43:13 +08:00
}
Datum
ldbm_fetch( LDBM ldbm, Datum key )
{
Datum data;
int rc;
LDBM_LOCK;
#if DB_VERSION_MAJOR >= 3
ldbm_datum_init( data );
data.flags = DB_DBT_MALLOC;
if ( (rc = ldbm->get( ldbm, NULL, &key, &data, 0 )) != 0 ) {
ldbm_datum_free( ldbm, data );
#elif DB_VERSION_MAJOR >= 2
ldbm_datum_init( data );
data.flags = DB_DBT_MALLOC;
if ( (rc = (*ldbm->get)( ldbm, NULL, &key, &data, 0 )) != 0 ) {
ldbm_datum_free( ldbm, data );
#else
1998-08-09 08:43:13 +08:00
if ( (rc = (*ldbm->get)( ldbm, &key, &data, 0 )) == 0 ) {
/* Berkeley DB 1.85 don't malloc the data for us */
/* duplicate it for to ensure reentrancy */
1998-08-09 08:43:13 +08:00
data = ldbm_datum_dup( ldbm, data );
} else {
#endif
1998-08-09 08:43:13 +08:00
data.dptr = NULL;
data.dsize = 0;
}
LDBM_UNLOCK;
1998-08-09 08:43:13 +08:00
return( data );
}
int
ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
{
int rc;
LDBM_LOCK;
#if DB_VERSION_MAJOR >= 3
rc = ldbm->put( ldbm, NULL, &key, &data, flags & ~LDBM_SYNC );
if ( rc != 0 )
{
char error[BUFSIZ];
sprintf( error, "%s (%d)\n", STRERROR( rc ), rc );
}
rc = (-1) * rc;
#elif DB_VERSION_MAJOR >= 2
rc = (*ldbm->put)( ldbm, NULL, &key, &data, flags & ~LDBM_SYNC );
rc = (-1 ) * rc;
#else
1998-08-09 08:43:13 +08:00
rc = (*ldbm->put)( ldbm, &key, &data, flags & ~LDBM_SYNC );
#endif
1998-08-09 08:43:13 +08:00
if ( flags & LDBM_SYNC )
(*ldbm->sync)( ldbm, 0 );
LDBM_UNLOCK;
1998-08-09 08:43:13 +08:00
return( rc );
}
int
ldbm_delete( LDBM ldbm, Datum key )
{
int rc;
LDBM_LOCK;
#if DB_VERSION_MAJOR >= 3
rc = ldbm->del( ldbm, NULL, &key, 0 );
rc = (-1 ) * rc;
#elif DB_VERSION_MAJOR >= 2
rc = (*ldbm->del)( ldbm, NULL, &key, 0 );
rc = (-1 ) * rc;
#else
1998-08-09 08:43:13 +08:00
rc = (*ldbm->del)( ldbm, &key, 0 );
#endif
1998-08-09 08:43:13 +08:00
(*ldbm->sync)( ldbm, 0 );
LDBM_UNLOCK;
1998-08-09 08:43:13 +08:00
return( rc );
}
Datum
ldbm_firstkey( LDBM ldbm, LDBMCursor **dbch )
1998-08-09 08:43:13 +08:00
{
Datum key, data;
#if DB_VERSION_MAJOR >= 2
LDBMCursor *dbci;
ldbm_datum_init( key );
ldbm_datum_init( data );
key.flags = data.flags = DB_DBT_MALLOC;
LDBM_LOCK;
/* acquire a cursor for the DB */
# if DB_VERSION_MAJOR >= 3
if ( ldbm->cursor( ldbm, NULL, &dbci, 0 ) )
# elif defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
(DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6)
if ( (*ldbm->cursor)( ldbm, NULL, &dbci ))
# else
if ( (*ldbm->cursor)( ldbm, NULL, &dbci, 0 ))
# endif
{
key.dptr = NULL;
return( key );
} else {
*dbch = dbci;
if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
ldbm_datum_free( ldbm, data );
}
else {
#else
int rc;
LDBM_LOCK;
1998-08-09 08:43:13 +08:00
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_FIRST )) == 0 ) {
key = ldbm_datum_dup( ldbm, key );
}
else {
#endif
1998-08-09 08:43:13 +08:00
key.dptr = NULL;
key.dsize = 0;
}
#if DB_VERSION_MAJOR >= 2
}
#endif
LDBM_UNLOCK;
1998-08-09 08:43:13 +08:00
return( key );
}
Datum
ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
1998-08-09 08:43:13 +08:00
{
Datum data;
#if DB_VERSION_MAJOR >= 2
ldbm_datum_init( data );
ldbm_datum_free( ldbm, key );
key.flags = data.flags = DB_DBT_MALLOC;
LDBM_LOCK;
if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
ldbm_datum_free( ldbm, data );
}
else {
#else
int rc;
LDBM_LOCK;
1998-08-09 08:43:13 +08:00
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_NEXT )) == 0 ) {
key = ldbm_datum_dup( ldbm, key );
}
else {
#endif
1998-08-09 08:43:13 +08:00
key.dptr = NULL;
key.dsize = 0;
}
LDBM_UNLOCK;
1998-08-09 08:43:13 +08:00
return( key );
}
int
ldbm_errno( LDBM ldbm )
{
return( errno );
}
/******************************************************************
* *
* END Berkeley section *
* *
******************************************************************/
1998-10-25 10:02:31 +08:00
#elif defined( HAVE_GDBM )
#ifdef HAVE_ST_BLKSIZE
1998-10-25 10:02:31 +08:00
#include <sys/stat.h>
#endif
1998-10-25 10:02:31 +08:00
/*****************************************************************
* *
* use gdbm *
1998-10-25 10:02:31 +08:00
* *
*****************************************************************/
LDBM
ldbm_open( char *name, int rw, int mode, int dbcachesize )
{
LDBM db;
1999-04-20 01:56:48 +08:00
#ifdef HAVE_ST_BLKSIZE
struct stat st;
#endif
1998-10-25 10:02:31 +08:00
LDBM_LOCK;
1998-10-25 10:02:31 +08:00
if ( (db = gdbm_open( name, 0, rw | GDBM_FAST, mode, 0 )) == NULL ) {
LDBM_UNLOCK;
1998-10-25 10:02:31 +08:00
return( NULL );
}
#ifdef HAVE_ST_BLKSIZE
1998-10-25 10:02:31 +08:00
if ( dbcachesize > 0 && stat( name, &st ) == 0 ) {
dbcachesize /= st.st_blksize;
if( dbcachesize == 0 ) dbcachesize = 1;
1998-10-25 10:02:31 +08:00
gdbm_setopt( db, GDBM_CACHESIZE, &dbcachesize, sizeof(int) );
}
#else
if ( dbcachesize > 0 ) {
dbcachesize /= 4096;
if( dbcachesize == 0 ) dbcachesize = 1;
gdbm_setopt( db, GDBM_CACHESIZE, &dbcachesize, sizeof(int) );
}
#endif
1998-10-25 10:02:31 +08:00
LDBM_UNLOCK;
1998-10-25 10:02:31 +08:00
return( db );
}
void
ldbm_close( LDBM ldbm )
{
LDBM_LOCK;
1998-10-25 10:02:31 +08:00
gdbm_close( ldbm );
LDBM_UNLOCK;
1998-10-25 10:02:31 +08:00
}
void
ldbm_sync( LDBM ldbm )
{
LDBM_LOCK;
1998-10-25 10:02:31 +08:00
gdbm_sync( ldbm );
LDBM_UNLOCK;
1998-10-25 10:02:31 +08:00
}
Datum
ldbm_fetch( LDBM ldbm, Datum key )
{
Datum d;
LDBM_LOCK;
d = gdbm_fetch( ldbm, key );
LDBM_UNLOCK;
return d;
1998-10-25 10:02:31 +08:00
}
int
ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
{
int rc;
LDBM_LOCK;
1998-10-25 10:02:31 +08:00
rc = gdbm_store( ldbm, key, data, flags & ~LDBM_SYNC );
if ( flags & LDBM_SYNC )
gdbm_sync( ldbm );
LDBM_UNLOCK;
1998-10-25 10:02:31 +08:00
return( rc );
}
int
ldbm_delete( LDBM ldbm, Datum key )
{
int rc;
LDBM_LOCK;
1998-10-25 10:02:31 +08:00
rc = gdbm_delete( ldbm, key );
gdbm_sync( ldbm );
LDBM_UNLOCK;
1998-10-25 10:02:31 +08:00
return( rc );
}
Datum
ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
1998-10-25 10:02:31 +08:00
{
Datum d;
LDBM_LOCK;
d = gdbm_firstkey( ldbm );
LDBM_UNLOCK;
2000-02-11 10:05:14 +08:00
if ( d.dptr != NULL ) {
*dbcp = (Datum *) malloc( sizeof( Datum ) );
**dbcp = ldbm_datum_dup( ldbm, d );
}
return d;
1998-10-25 10:02:31 +08:00
}
Datum
ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
1998-10-25 10:02:31 +08:00
{
Datum d;
LDBM_LOCK;
2000-02-11 10:05:14 +08:00
d = gdbm_nextkey( ldbm, *dbcp );
LDBM_UNLOCK;
2000-02-11 10:05:14 +08:00
ldbm_datum_free( ldbm, *dbcp );
if ( d.dptr != NULL ) {
*dbcp = ldbm_datum_dup( ldbm, d );
} else {
free( dbcp );
}
return d;
1998-10-25 10:02:31 +08:00
}
int
ldbm_errno( LDBM ldbm )
{
int err;
LDBM_LOCK;
err = gdbm_errno;
LDBM_UNLOCK;
return( err );
1998-10-25 10:02:31 +08:00
}
1998-08-09 08:43:13 +08:00
1999-03-30 12:39:08 +08:00
#elif HAVE_MDBM
/* MMAPED DBM HASHING DATABASE */
1999-06-03 08:37:44 +08:00
#include <ac/string.h>
1999-03-30 12:39:08 +08:00
/* #define MDBM_DEBUG */
#ifdef MDBM_DEBUG
#include <stdio.h>
#endif
#define NO_NULL_KEY
/* #define MDBM_CHAIN */
#ifdef MDBM_CHAIN
/* Use chaining */
#define mdbm_store mdbm_chain_store
#define mdbm_fetch mdbm_chain_fetch
#define mdbm_delete mdbm_chain_delete
#define mdbm_first mdbm_chain_first
#define mdbm_next mdbm_chain_next
#endif
#define MDBM_PG_SZ (4*1024)
/*****************************************************************
* *
* use mdbm *
* *
*****************************************************************/
LDBM
ldbm_open( char *name, int rw, int mode, int dbcachesize )
{
LDBM db;
#ifdef MDBM_DEBUG
fprintf( stdout,
"==>(mdbm)ldbm_open(name=%s,rw=%x,mode=%x,cachesize=%d)\n",
name ? name : "NULL", rw, mode, dbcachesize );
fflush( stdout );
#endif
LDBM_LOCK; /* We need locking here, this is the only non-thread
* safe function we have.
*/
if ( (db = mdbm_open( name, rw, mode, MDBM_PG_SZ )) == NULL ) {
LDBM_UNLOCK;
#ifdef MDBM_DEBUG
fprintf( stdout, "<==(mdbm)ldbm_open(db=NULL)\n" );
fflush( stdout );
#endif
return( NULL );
}
#ifdef MDBM_CHAIN
(void)mdbm_set_chain(db);
#endif
LDBM_UNLOCK;
#ifdef MDBM_DEBUG
fprintf( stdout, "<==(mdbm)ldbm_open(db=%p)\n", db );
fflush( stdout );
#endif
return( db );
}
1999-03-30 12:39:08 +08:00
void
ldbm_close( LDBM ldbm )
{
/* Open and close are not reentrant so we need to use locks here */
#ifdef MDBM_DEBUG
fprintf( stdout,
"==>(mdbm)ldbm_close(db=%p)\n", ldbm );
fflush( stdout );
#endif
LDBM_LOCK;
mdbm_close( ldbm );
LDBM_UNLOCK;
#ifdef MDBM_DEBUG
fprintf( stdout, "<==(mdbm)ldbm_close()\n" );
fflush( stdout );
#endif
}
1999-03-30 12:39:08 +08:00
void
ldbm_sync( LDBM ldbm )
{
/* XXX: Not sure if this is re-entrant need to check code, if so
* you can leave LOCKS out.
*/
LDBM_LOCK;
mdbm_sync( ldbm );
LDBM_UNLOCK;
}
1999-03-30 12:39:08 +08:00
#define MAX_MDBM_RETRY 5
Datum
ldbm_fetch( LDBM ldbm, Datum key )
{
Datum d;
kvpair k;
int retry = 0;
/* This hack is needed because MDBM does not take keys
* which begin with NULL when working in the chaining
* mode.
*/
/* LDBM_LOCK; */
#ifdef NO_NULL_KEY
k.key.dsize = key.dsize + 1;
1999-06-12 02:56:28 +08:00
k.key.dptr = malloc(k.key.dsize);
1999-03-30 12:39:08 +08:00
*(k.key.dptr) = 'l';
AC_MEMCPY( (void *)(k.key.dptr + 1), key.dptr, key.dsize );
1999-03-30 12:39:08 +08:00
#else
k.key = key;
#endif
k.val.dptr = NULL;
k.val.dsize = 0;
do {
d = mdbm_fetch( ldbm, k );
if ( d.dsize > 0 ) {
if ( k.val.dptr != NULL ) {
free( k.val.dptr );
}
if ( (k.val.dptr = malloc( d.dsize )) != NULL ) {
k.val.dsize = d.dsize;
d = mdbm_fetch( ldbm, k );
} else {
d.dsize = 0;
break;
}
}/* if ( d.dsize > 0 ) */
} while ((d.dsize > k.val.dsize) && (++retry < MAX_MDBM_RETRY));
/* LDBM_UNLOCK; */
1999-06-12 02:56:28 +08:00
#ifdef NO_NULL_KEY
free(k.key.dptr);
#endif
1999-03-30 12:39:08 +08:00
return d;
}
1999-03-30 12:39:08 +08:00
int
ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
{
int rc;
Datum int_key; /* Internal key */
#ifdef MDBM_DEBUG
fprintf( stdout,
"==>(mdbm)ldbm_store(db=%p, key(dptr=%p,sz=%d), data(dptr=%p,sz=%d), flags=%x)\n",
ldbm, key.dptr, key.dsize, data.dptr, data.dsize, flags );
fflush( stdout );
#endif
/* LDBM_LOCK; */
#ifdef NO_NULL_KEY
int_key.dsize = key.dsize + 1;
1999-06-12 02:56:28 +08:00
int_key.dptr = malloc( int_key.dsize );
1999-03-30 12:39:08 +08:00
*(int_key.dptr) = 'l'; /* Must not be NULL !*/
AC_MEMCPY( (void *)(int_key.dptr + 1), key.dptr, key.dsize );
1999-03-30 12:39:08 +08:00
#else
int_key = key;
#endif
rc = mdbm_store( ldbm, int_key, data, flags );
if ( flags & LDBM_SYNC ) {
mdbm_sync( ldbm );
}
/* LDBM_UNLOCK; */
#ifdef MDBM_DEBUG
fprintf( stdout, "<==(mdbm)ldbm_store(rc=%d)\n", rc );
fflush( stdout );
#endif
1999-06-12 02:56:28 +08:00
#ifdef NO_NULL_KEY
free(int_key.dptr);
#endif
1999-03-30 12:39:08 +08:00
return( rc );
}
1999-03-30 12:39:08 +08:00
int
ldbm_delete( LDBM ldbm, Datum key )
{
int rc;
Datum int_key;
/* LDBM_LOCK; */
#ifdef NO_NULL_KEY
int_key.dsize = key.dsize + 1;
1999-06-12 02:56:28 +08:00
int_key.dptr = malloc(int_key.dsize);
1999-03-30 12:39:08 +08:00
*(int_key.dptr) = 'l';
AC_MEMCPY( (void *)(int_key.dptr + 1), key.dptr, key.dsize );
1999-03-30 12:39:08 +08:00
#else
int_key = key;
#endif
rc = mdbm_delete( ldbm, int_key );
/* LDBM_UNLOCK; */
1999-06-12 02:56:28 +08:00
#ifdef NO_NULL_KEY
free(int_key.dptr);
#endif
1999-03-30 12:39:08 +08:00
return( rc );
}
1999-03-30 12:39:08 +08:00
static Datum
ldbm_get_next( LDBM ldbm, kvpair (*fptr)(MDBM *, kvpair) )
{
kvpair out;
kvpair in;
Datum ret;
size_t sz = MDBM_PAGE_SIZE(ldbm);
#ifdef NO_NULL_KEY
int delta = 1;
#else
int delta = 0;
#endif
/* LDBM_LOCK; */
in.key.dsize = sz; /* Assume first key in one pg */
1999-06-12 02:56:28 +08:00
in.key.dptr = malloc(sz);
1999-03-30 12:39:08 +08:00
in.val.dptr = NULL; /* Don't need data just key */
in.val.dsize = 0;
ret.dptr = NULL;
ret.dsize = NULL;
out = fptr( ldbm, in );
if (out.key.dsize > 0) {
ret.dsize = out.key.dsize - delta;
if ((ret.dptr = (char *)malloc(ret.dsize)) == NULL) {
ret.dsize = 0;
ret.dptr = NULL;
} else {
AC_MEMCPY(ret.dptr, (void *)(out.key.dptr + delta),
1999-03-30 12:39:08 +08:00
ret.dsize );
}
}
/* LDBM_UNLOCK; */
1999-06-12 02:56:28 +08:00
free(in.key.dptr);
1999-03-30 12:39:08 +08:00
return ret;
}
1999-03-30 12:39:08 +08:00
Datum
ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
1999-03-30 12:39:08 +08:00
{
return ldbm_get_next( ldbm, mdbm_first );
}
1999-03-30 12:39:08 +08:00
Datum
ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
1999-03-30 12:39:08 +08:00
{
/* XXX:
* don't know if this will affect the LDAP server opertaion
* but mdbm cannot take and input key.
*/
return ldbm_get_next( ldbm, mdbm_next );
}
1999-03-30 12:39:08 +08:00
int
ldbm_errno( LDBM ldbm )
{
/* XXX: best we can do with current mdbm interface */
return( errno );
}
1999-03-30 12:39:08 +08:00
1998-10-25 10:02:31 +08:00
#elif defined( HAVE_NDBM )
1998-08-09 08:43:13 +08:00
/*****************************************************************
* *
1999-03-30 12:39:08 +08:00
* if no gdbm or mdbm, fall back to using ndbm, the standard unix thing *
1998-08-09 08:43:13 +08:00
* *
*****************************************************************/
/* ARGSUSED */
LDBM
ldbm_open( char *name, int rw, int mode, int dbcachesize )
{
LDBM ldbm;
LDBM_LOCK;
ldbm = dbm_open( name, rw, mode );
LDBM_UNLOCK;
return( ldbm );
1998-08-09 08:43:13 +08:00
}
void
ldbm_close( LDBM ldbm )
{
LDBM_LOCK;
1998-08-09 08:43:13 +08:00
dbm_close( ldbm );
LDBM_UNLOCK;
1998-08-09 08:43:13 +08:00
}
/* ARGSUSED */
void
ldbm_sync( LDBM ldbm )
{
return;
}
Datum
ldbm_fetch( LDBM ldbm, Datum key )
{
Datum d;
LDBM_LOCK;
d = ldbm_datum_dup( ldbm, dbm_fetch( ldbm, key ) );
LDBM_UNLOCK;
return d;
1998-08-09 08:43:13 +08:00
}
int
ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
{
int rc;
LDBM_LOCK;
rc = dbm_store( ldbm, key, data, flags );
LDBM_UNLOCK;
return rc;
1998-08-09 08:43:13 +08:00
}
int
ldbm_delete( LDBM ldbm, Datum key )
{
int rc;
LDBM_LOCK;
rc = dbm_delete( ldbm, key );
LDBM_UNLOCK;
return rc;
1998-08-09 08:43:13 +08:00
}
Datum
ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
1998-08-09 08:43:13 +08:00
{
Datum d;
LDBM_LOCK;
d = dbm_firstkey( ldbm );
LDBM_UNLOCK;
return d;
1998-08-09 08:43:13 +08:00
}
Datum
ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
1998-08-09 08:43:13 +08:00
{
Datum d;
LDBM_LOCK;
d = dbm_nextkey( ldbm );
LDBM_UNLOCK;
return d;
1998-08-09 08:43:13 +08:00
}
int
ldbm_errno( LDBM ldbm )
{
int err;
LDBM_LOCK;
err = dbm_error( ldbm );
LDBM_UNLOCK;
return err;
1998-08-09 08:43:13 +08:00
}
#endif /* ndbm */
1998-10-25 10:02:31 +08:00
#endif /* ldbm */