/* ldbm.c - ldap dbm compatibility routines */
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
/* Patched for Berkeley DB version 2.0; /KSp; 98/02/23
*
#ifdef SLAPD_LDBM
#include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/errno.h>
static int ldbm_initialized = 0;
-#ifndef HAVE_BERKELEY_DB2
-/* Everything but DB2 is non-reentrant */
-
+#ifndef HAVE_BERKELEY_DB2_DB_THREAD
+/* Only DB2 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))
+#else
+#define LDBM_LOCK ((void)0)
+#define LDBM_UNLOCK ((void)0)
+#endif
+
+#ifndef HAVE_BERKELEY_DB2
+
int ldbm_initialize( void )
{
if(ldbm_initialized++) return 1;
static DB_ENV ldbm_Env_internal;
DB_ENV *ldbm_Env = NULL;
-/* Berkeley DB 2.x is reentrant */
-#define LDBM_LOCK ((void)0)
-#define LDBM_UNLOCK ((void)0)
-
int ldbm_initialize( void )
{
int err;
- int envFlags;
+ u_int32_t envFlags;
if(ldbm_initialized++) return 1;
ldbm_Env->db_errcall = ldbm_db_errcall;
ldbm_Env->db_errpfx = "==>";
- envFlags = DB_CREATE | DB_THREAD;
+ envFlags = DB_CREATE
+#ifdef HAVE_BERKELEY_DB2_DB_THREAD
+ | DB_THREAD
+#endif
+ ;
if ( ( err = db_appinit( NULL, NULL, ldbm_Env, envFlags )) ) {
char error[BUFSIZ];
DB_INFO dbinfo;
memset( &dbinfo, 0, sizeof( dbinfo ));
+
+#if defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
+ 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;
}
Datum
-#ifdef HAVE_BERKELEY_DB2
-ldbm_firstkey( LDBM ldbm, DBC **dbch )
-#else
-ldbm_firstkey( LDBM ldbm )
-#endif
+ldbm_firstkey( LDBM ldbm, LDBMCursor **dbch )
{
Datum key, data;
#ifdef HAVE_BERKELEY_DB2
- DBC *dbci;
+ LDBMCursor *dbci;
ldbm_datum_init( key );
ldbm_datum_init( data );
}
Datum
-#ifdef HAVE_BERKELEY_DB2
-ldbm_nextkey( LDBM ldbm, Datum key, DBC *dbcp )
-#else
-ldbm_nextkey( LDBM ldbm, Datum key )
-#endif
+ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
{
Datum data;
#ifdef HAVE_ST_BLKSIZE
if ( dbcachesize > 0 && stat( name, &st ) == 0 ) {
- dbcachesize = (dbcachesize / st.st_blksize);
+ dbcachesize /= st.st_blksize;
+ if( dbcachesize == 0 ) dbcachesize = 1;
gdbm_setopt( db, GDBM_CACHESIZE, &dbcachesize, sizeof(int) );
}
#else
- dbcachesize = (dbcachesize / 4096);
- gdbm_setopt( db, GDBM_CACHESIZE, &dbcachesize, sizeof(int) );
+ if ( dbcachesize > 0 ) {
+ dbcachesize /= 4096;
+ if( dbcachesize == 0 ) dbcachesize = 1;
+ gdbm_setopt( db, GDBM_CACHESIZE, &dbcachesize, sizeof(int) );
+ }
#endif
LDBM_UNLOCK;
}
Datum
-ldbm_firstkey( LDBM ldbm )
+ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
{
Datum d;
}
Datum
-ldbm_nextkey( LDBM ldbm, Datum key )
+ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
{
Datum d;
/* MMAPED DBM HASHING DATABASE */
-#include <alloca.h>
-#include <string.h>
+#include <ac/string.h>
/* #define MDBM_DEBUG */
return( db );
-}/* LDBM ldbm_open() */
+}
fflush( stdout );
#endif
-}/* void ldbm_close() */
+}
mdbm_sync( ldbm );
LDBM_UNLOCK;
-}/* void ldbm_sync() */
+}
#define MAX_MDBM_RETRY 5
#ifdef NO_NULL_KEY
k.key.dsize = key.dsize + 1;
- k.key.dptr = alloca(k.key.dsize);
+ k.key.dptr = malloc(k.key.dsize);
*(k.key.dptr) = 'l';
memcpy( (void *)(k.key.dptr + 1), key.dptr, key.dsize );
#else
/* LDBM_UNLOCK; */
+#ifdef NO_NULL_KEY
+ free(k.key.dptr);
+#endif
+
return d;
-}/* Datum ldbm_fetch() */
+}
#ifdef NO_NULL_KEY
int_key.dsize = key.dsize + 1;
- int_key.dptr = alloca( int_key.dsize );
+ int_key.dptr = malloc( int_key.dsize );
*(int_key.dptr) = 'l'; /* Must not be NULL !*/
memcpy( (void *)(int_key.dptr + 1), key.dptr, key.dsize );
#else
fflush( stdout );
#endif
- return( rc );
+#ifdef NO_NULL_KEY
+ free(int_key.dptr);
+#endif
-}/* int ldbm_store() */
+ return( rc );
+}
#ifdef NO_NULL_KEY
int_key.dsize = key.dsize + 1;
- int_key.dptr = alloca(int_key.dsize);
+ int_key.dptr = malloc(int_key.dsize);
*(int_key.dptr) = 'l';
memcpy( (void *)(int_key.dptr + 1), key.dptr, key.dsize );
#else
rc = mdbm_delete( ldbm, int_key );
/* LDBM_UNLOCK; */
+#ifdef NO_NULL_KEY
+ free(int_key.dptr);
+#endif
return( rc );
-}/* int ldbm_delete() */
+}
/* LDBM_LOCK; */
in.key.dsize = sz; /* Assume first key in one pg */
- in.key.dptr = alloca(sz);
+ in.key.dptr = malloc(sz);
in.val.dptr = NULL; /* Don't need data just key */
in.val.dsize = 0;
}
/* LDBM_UNLOCK; */
+
+ free(in.key.dptr);
return ret;
-}/* static Datum ldbm_get_next() */
+}
Datum
-ldbm_firstkey( LDBM ldbm )
+ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
{
return ldbm_get_next( ldbm, mdbm_first );
-}/* Datum ldbm_firstkey() */
+}
Datum
-ldbm_nextkey( LDBM ldbm, Datum key )
+ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
{
/* XXX:
return ldbm_get_next( ldbm, mdbm_next );
-}/* Datum ldbm_nextkey() */
+}
int
ldbm_errno( LDBM ldbm )
/* XXX: best we can do with current mdbm interface */
return( errno );
-}/* int ldbm_errno() */
+}
}
Datum
-ldbm_firstkey( LDBM ldbm )
+ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
{
Datum d;
}
Datum
-ldbm_nextkey( LDBM ldbm, Datum key )
+ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
{
Datum d;