return( dup );
}
dup.dsize = data.dsize;
- if ( (dup.dptr = (char *) malloc( data.dsize )) != NULL )
+
+ if ( (dup.dptr = (char *) malloc( data.dsize )) != NULL ) {
AC_MEMCPY( dup.dptr, data.dptr, data.dsize );
+ }
return( dup );
}
static int ldbm_initialized = 0;
-#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
-
-
/*******************************************************************
* *
}
/* a dbEnv for BERKELEYv2 */
-DB_ENV *ldbm_Env = NULL;
+DB_ENV *ldbm_Env = NULL;
int ldbm_initialize( const char* home )
{
- int err;
+ int err;
u_int32_t envFlags;
if(ldbm_initialized++) return 1;
+ ldap_pvt_thread_mutex_init( &ldbm_big_mutex );
+
#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 = "==>";
+ ldbm_Env->db_errcall = ldbm_db_errcall;
+ ldbm_Env->db_errpfx = "==>";
#else
ldbm_Env = NULL;
#endif
envFlags =
-#if defined( DB_PRIVATE )
+#ifdef DB_PRIVATE
DB_PRIVATE |
#endif
-#if defined( HAVE_BERKELEY_DB_THREAD )
+#ifdef HAVE_BERKELEY_DB_THREAD
DB_THREAD |
#endif
DB_CREATE;
#endif
if ( err ) {
+#ifdef LDAP_SYSLOG
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",
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 );
+ err = ldbm_Env->open( ldbm_Env, home, envFlags, 0 );
#else
- err = ldbm_Env->open( ldbm_Env, home, NULL, envFlags, 0 );
+ 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 );
+ if ( err != 0 ) {
#ifdef LDAP_SYSLOG
- syslog( LOG_INFO,
- "ldbm_initialize(): FATAL error in dbEnv->open() : %s\n",
- error );
+ char error[BUFSIZ];
+
+ sprintf( error, "%s (%d)\n", STRERROR( err ), err );
+ syslog( LOG_INFO,
+ "ldbm_initialize(): FATAL error in dbEnv->open() : %s\n",
+ error );
#endif
- ldbm_Env->close( ldbm_Env, 0 );
- return( 1 );
+ ldbm_Env->close( ldbm_Env, 0 );
+ return( 1 );
}
#endif
db_appexit( ldbm_Env );
#endif
+ ldap_pvt_thread_mutex_destroy( &ldbm_big_mutex );
+
return 0;
}
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];
+ int err;
- sprintf( error, "%s (%d)\n", STRERROR( err ), err );
+ LDBM_LOCK;
- (void)ret->close(ret, 0);
- return NULL;
- }
+ err = db_create( &ret, ldbm_Env, 0 );
+ if ( err != 0 ) {
+ (void)ret->close(ret, 0);
+ return NULL;
+ }
- ret->set_pagesize( ret, DEFAULT_DB_PAGE_SIZE );
- ret->set_malloc( ret, ldbm_malloc );
+ ret->set_pagesize( ret, DEFAULT_DB_PAGE_SIZE );
+ ret->set_malloc( ret, ldbm_malloc );
- err = ret->open( ret, name, NULL, DB_TYPE, rw, mode);
- LDBM_UNLOCK;
- if ( err != 0 )
- {
- char error[BUFSIZ];
+ err = ret->open( ret, name, NULL, DB_TYPE, rw, mode);
- sprintf( error, "%s (%d)\n", STRERROR( err ), err );
+ LDBM_UNLOCK;
- (void)ret->close(ret, 0);
- return NULL;
- }
+ if ( err != 0 ) {
+ (void)ret->close(ret, 0);
+ return NULL;
+ }
#elif DB_VERSION_MAJOR >= 2
DB_INFO dbinfo;
*/
#else
/* set db_cachesize of MPOOL is NOT being used. */
- if (( ldbm_Env == NULL ) || ( ldbm_Env->mp_info == NULL ))
+ 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;
+ 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 );
+ (void) db_open( name, DB_TYPE, rw, mode, ldbm_Env, &dbinfo, &ret );
LDBM_UNLOCK;
#else
LDBM_LOCK;
ret = dbopen( name, rw, mode, DB_TYPE, info );
LDBM_UNLOCK;
-
#endif
return( ret );
{
LDBM_LOCK;
#if DB_VERSION_MAJOR >= 3
- ldbm->close( ldbm, 0 );
+ ldbm->close( ldbm, 0 );
#elif DB_VERSION_MAJOR >= 2
(*ldbm->close)( ldbm, 0 );
#else
int rc;
LDBM_LOCK;
+
#if DB_VERSION_MAJOR >= 3
- ldbm_datum_init( data );
+ ldbm_datum_init( data );
- data.flags = DB_DBT_MALLOC;
+ data.flags = DB_DBT_MALLOC;
- if ( (rc = ldbm->get( ldbm, NULL, &key, &data, 0 )) != 0 ) {
- ldbm_datum_free( ldbm, data );
+ if ( (rc = ldbm->get( ldbm, NULL, &key, &data, 0 )) != 0 ) {
+ ldbm_datum_free( ldbm, data );
+ data.dptr = NULL;
+ data.dsize = 0;
+ }
#elif DB_VERSION_MAJOR >= 2
ldbm_datum_init( data );
if ( (rc = (*ldbm->get)( ldbm, NULL, &key, &data, 0 )) != 0 ) {
ldbm_datum_free( ldbm, data );
+ data.dptr = NULL;
+ data.dsize = 0;
+ }
#else
+
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 */
data = ldbm_datum_dup( ldbm, data );
} else {
-#endif
data.dptr = NULL;
data.dsize = 0;
}
+#endif
LDBM_UNLOCK;
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;
+ rc = ldbm->put( ldbm, NULL, &key, &data, flags & ~LDBM_SYNC );
+ rc = (-1) * rc;
#elif DB_VERSION_MAJOR >= 2
rc = (*ldbm->put)( ldbm, NULL, &key, &data, flags & ~LDBM_SYNC );
- rc = (-1 ) * rc;
+ rc = (-1) * rc;
#else
rc = (*ldbm->put)( ldbm, &key, &data, flags & ~LDBM_SYNC );
#endif
#if DB_VERSION_MAJOR >= 3
rc = ldbm->del( ldbm, NULL, &key, 0 );
- rc = (-1 ) * rc;
+ rc = (-1) * rc;
#elif DB_VERSION_MAJOR >= 2
rc = (*ldbm->del)( ldbm, NULL, &key, 0 );
- rc = (-1 ) * rc;
+ rc = (-1) * rc;
#else
rc = (*ldbm->del)( ldbm, &key, 0 );
#endif
/* acquire a cursor for the DB */
# if DB_VERSION_MAJOR >= 3
- if ( ldbm->cursor( ldbm, NULL, &dbci, 0 ) )
+ if ( ldbm->cursor( ldbm, NULL, &dbci, 0 ) )
# elif defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
- (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6)
+ (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6)
if ( (*ldbm->cursor)( ldbm, NULL, &dbci ))
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;
-
+ LDBM_UNLOCK;
}
if ( d.dsize > 0 ) {
if ( k.val.dptr != NULL ) {
-
- free( k.val.dptr );
-
+ free( k.val.dptr );
}
if ( (k.val.dptr = malloc( d.dsize )) != NULL ) {
#endif
return( rc );
-
}
static Datum
ldbm_get_next( LDBM ldbm, kvpair (*fptr)(MDBM *, kvpair) )
{
-
kvpair out;
kvpair in;
Datum ret;
out = fptr( ldbm, in );
if (out.key.dsize > 0) {
+ ret.dsize = out.key.dsize - delta;
- 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),
- ret.dsize );
+ if ((ret.dptr = (char *)malloc(ret.dsize)) == NULL) {
+ ret.dsize = 0;
+ ret.dptr = NULL;
+ } else {
+ AC_MEMCPY(ret.dptr, (void *)(out.key.dptr + delta),
+ ret.dsize );
}
-
}
/* LDBM_UNLOCK; */
free(in.key.dptr);
-
return ret;
-
}
-
-
-
Datum
ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
{
-
return ldbm_get_next( ldbm, mdbm_first );
-
}
-
-
-
Datum
ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
{
-
/* XXX:
- * don't know if this will affect the LDAP server opertaion
+ * don't know if this will affect the LDAP server operation
* but mdbm cannot take and input key.
*/
return ldbm_get_next( ldbm, mdbm_next );
-
}
int
{
/* XXX: best we can do with current mdbm interface */
return( errno );
-
}