1998-08-09 08:43:13 +08:00
|
|
|
/* ldbm.c - ldap dbm compatibility routines */
|
|
|
|
|
1998-09-03 08:50:13 +08:00
|
|
|
/* Patched for Berkeley DB version 2.0; /KSp; 98/02/23
|
|
|
|
*
|
|
|
|
* - basic implementation; 1998/02/23, /KSp
|
|
|
|
* - DB_DBT_MALLOC ; 1998/03/22, /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>
|
|
|
|
#include <stdlib.h>
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
#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"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#if defined( LDBM_USE_DBHASH ) || defined( LDBM_USE_DBBTREE )
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
* *
|
|
|
|
* use berkeley db hash or btree package *
|
|
|
|
* *
|
|
|
|
*****************************************************************/
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
/*************************************************
|
|
|
|
* *
|
|
|
|
* A malloc routine for use with DB_DBT_MALLOC *
|
|
|
|
* *
|
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
void *
|
|
|
|
ldbm_malloc( size_t size )
|
|
|
|
{
|
|
|
|
return( calloc( 1, size ));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
LDBM
|
|
|
|
ldbm_open( char *name, int rw, int mode, int dbcachesize )
|
|
|
|
{
|
1998-09-03 08:50:13 +08:00
|
|
|
LDBM ret = NULL;
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
DB_INFO dbinfo;
|
|
|
|
|
|
|
|
memset( &dbinfo, 0, sizeof( dbinfo ));
|
|
|
|
dbinfo.db_cachesize = dbcachesize;
|
|
|
|
dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
|
|
|
|
dbinfo.db_malloc = ldbm_malloc;
|
|
|
|
|
|
|
|
db_open( name, DB_TYPE, rw, mode, NULL, &dbinfo, &ret );
|
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
1998-09-03 08:50:13 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
ret = dbopen( name, rw, mode, DB_TYPE, info );
|
1998-09-03 08:50:13 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_close( LDBM ldbm )
|
|
|
|
{
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
(*ldbm->close)( ldbm, 0 );
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
(*ldbm->close)( ldbm );
|
1998-09-03 08:50:13 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_sync( LDBM ldbm )
|
|
|
|
{
|
|
|
|
(*ldbm->sync)( ldbm, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_datum_free( LDBM ldbm, Datum data )
|
|
|
|
{
|
|
|
|
free( data.dptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_datum_dup( LDBM ldbm, Datum data )
|
|
|
|
{
|
|
|
|
Datum dup;
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
memset( &dup, 0, sizeof( dup ));
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( data.dsize == 0 ) {
|
|
|
|
dup.dsize = 0;
|
|
|
|
dup.dptr = NULL;
|
|
|
|
|
|
|
|
return( dup );
|
|
|
|
}
|
|
|
|
dup.dsize = data.dsize;
|
|
|
|
if ( dup.dptr = (char *) malloc( data.dsize ) )
|
|
|
|
memcpy( dup.dptr, data.dptr, data.dsize );
|
|
|
|
|
|
|
|
return( dup );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_fetch( LDBM ldbm, Datum key )
|
|
|
|
{
|
|
|
|
Datum data;
|
|
|
|
int rc;
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
memset( &data, 0, sizeof( data ));
|
|
|
|
|
|
|
|
data.flags = DB_DBT_MALLOC;
|
|
|
|
|
|
|
|
if ( (rc = (*ldbm->get)( ldbm, NULL, &key, &data, 0 )) != 0 ) {
|
|
|
|
if ( data.dptr ) free( data.dptr );
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (rc = (*ldbm->get)( ldbm, &key, &data, 0 )) == 0 ) {
|
|
|
|
data = ldbm_datum_dup( ldbm, data );
|
|
|
|
} else {
|
1998-09-03 08:50:13 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
data.dptr = NULL;
|
|
|
|
data.dsize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
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 );
|
1998-09-03 08:50:13 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( flags & LDBM_SYNC )
|
|
|
|
(*ldbm->sync)( ldbm, 0 );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_delete( LDBM ldbm, Datum key )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
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 );
|
1998-09-03 08:50:13 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
(*ldbm->sync)( ldbm, 0 );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
ldbm_firstkey( LDBM ldbm, DBC **dbch )
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
ldbm_firstkey( LDBM ldbm )
|
1998-09-03 08:50:13 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
Datum key, data;
|
|
|
|
int rc;
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
DBC *dbci;
|
|
|
|
|
|
|
|
memset( &key, 0, sizeof( key ));
|
|
|
|
memset( &data, 0, sizeof( data ));
|
|
|
|
|
|
|
|
key.flags = data.flags = DB_DBT_MALLOC;
|
|
|
|
|
|
|
|
/* acquire a cursor for the DB */
|
|
|
|
if ( (*ldbm->cursor)( ldbm, NULL, &dbci )) {
|
|
|
|
return( key );
|
|
|
|
} else {
|
|
|
|
*dbch = dbci;
|
|
|
|
if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
|
|
|
|
if ( data.dptr ) free( data.dptr );
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_FIRST )) == 0 ) {
|
|
|
|
key = ldbm_datum_dup( ldbm, key );
|
1998-09-03 08:50:13 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
key.dptr = NULL;
|
|
|
|
key.dsize = 0;
|
|
|
|
}
|
1998-09-03 08:50:13 +08:00
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( key );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
ldbm_nextkey( LDBM ldbm, Datum key, DBC *dbcp )
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
ldbm_nextkey( LDBM ldbm, Datum key )
|
1998-09-03 08:50:13 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
Datum data;
|
|
|
|
int rc;
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
void *oldKey = key.dptr;
|
|
|
|
|
|
|
|
memset( &data, 0, sizeof( data ));
|
|
|
|
|
|
|
|
data.flags = DB_DBT_MALLOC;
|
|
|
|
|
|
|
|
if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
|
|
|
|
if ( data.dptr ) free( data.dptr );
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_NEXT )) == 0 ) {
|
|
|
|
key = ldbm_datum_dup( ldbm, key );
|
1998-09-03 08:50:13 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
key.dptr = NULL;
|
|
|
|
key.dsize = 0;
|
|
|
|
}
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_BERKELEY_DB2
|
1998-09-03 08:50:13 +08:00
|
|
|
if ( oldKey ) free( oldKey );
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( key );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_errno( LDBM ldbm )
|
|
|
|
{
|
|
|
|
return( errno );
|
|
|
|
}
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#elif defined( HAVE_GDBM )
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
* *
|
|
|
|
* use gdbm *
|
|
|
|
* *
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
LDBM
|
|
|
|
ldbm_open( char *name, int rw, int mode, int dbcachesize )
|
|
|
|
{
|
|
|
|
LDBM db;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if ( (db = gdbm_open( name, 0, rw | GDBM_FAST, mode, 0 )) == NULL ) {
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
if ( dbcachesize > 0 && stat( name, &st ) == 0 ) {
|
|
|
|
dbcachesize = (dbcachesize / st.st_blksize);
|
|
|
|
gdbm_setopt( db, GDBM_CACHESIZE, &dbcachesize, sizeof(int) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( db );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_close( LDBM ldbm )
|
|
|
|
{
|
|
|
|
gdbm_close( ldbm );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_sync( LDBM ldbm )
|
|
|
|
{
|
|
|
|
gdbm_sync( ldbm );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_datum_free( LDBM ldbm, Datum data )
|
|
|
|
{
|
|
|
|
free( data.dptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_datum_dup( LDBM ldbm, Datum data )
|
|
|
|
{
|
|
|
|
Datum dup;
|
|
|
|
|
|
|
|
if ( data.dsize == 0 ) {
|
|
|
|
dup.dsize = 0;
|
|
|
|
dup.dptr = NULL;
|
|
|
|
|
|
|
|
return( dup );
|
|
|
|
}
|
|
|
|
dup.dsize = data.dsize;
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
if ( (dup.dptr = (char *) malloc( data.dsize )) != NULL )
|
1998-10-25 10:02:31 +08:00
|
|
|
memcpy( dup.dptr, data.dptr, data.dsize );
|
|
|
|
|
|
|
|
return( dup );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_fetch( LDBM ldbm, Datum key )
|
|
|
|
{
|
|
|
|
return( gdbm_fetch( ldbm, key ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = gdbm_store( ldbm, key, data, flags & ~LDBM_SYNC );
|
|
|
|
if ( flags & LDBM_SYNC )
|
|
|
|
gdbm_sync( ldbm );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_delete( LDBM ldbm, Datum key )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = gdbm_delete( ldbm, key );
|
|
|
|
gdbm_sync( ldbm );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_firstkey( LDBM ldbm )
|
|
|
|
{
|
|
|
|
return( gdbm_firstkey( ldbm ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_nextkey( LDBM ldbm, Datum key )
|
|
|
|
{
|
|
|
|
return( gdbm_nextkey( ldbm, key ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_errno( LDBM ldbm )
|
|
|
|
{
|
|
|
|
return( (int) gdbm_errno );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#elif defined( HAVE_NDBM )
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
* *
|
|
|
|
* if no gdbm, fall back to using ndbm, the standard unix thing *
|
|
|
|
* *
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
LDBM
|
|
|
|
ldbm_open( char *name, int rw, int mode, int dbcachesize )
|
|
|
|
{
|
|
|
|
return( dbm_open( name, rw, mode ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_close( LDBM ldbm )
|
|
|
|
{
|
|
|
|
dbm_close( ldbm );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
|
|
|
ldbm_sync( LDBM ldbm )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_datum_free( LDBM ldbm, Datum data )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_datum_dup( LDBM ldbm, Datum data )
|
|
|
|
{
|
|
|
|
Datum dup;
|
|
|
|
|
|
|
|
if ( data.dsize == 0 ) {
|
|
|
|
dup.dsize = 0;
|
|
|
|
dup.dptr = NULL;
|
|
|
|
|
|
|
|
return( dup );
|
|
|
|
}
|
|
|
|
dup.dsize = data.dsize;
|
1998-11-04 21:15:18 +08:00
|
|
|
dup.dptr = (char *) malloc( data.dsize );
|
|
|
|
if ( dup.dptr )
|
1998-08-09 08:43:13 +08:00
|
|
|
memcpy( dup.dptr, data.dptr, data.dsize );
|
|
|
|
|
|
|
|
return( dup );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_fetch( LDBM ldbm, Datum key )
|
|
|
|
{
|
|
|
|
return( ldbm_datum_dup( ldbm, dbm_fetch( ldbm, key ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
|
|
|
|
{
|
|
|
|
return( dbm_store( ldbm, key, data, flags ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_delete( LDBM ldbm, Datum key )
|
|
|
|
{
|
|
|
|
return( dbm_delete( ldbm, key ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_firstkey( LDBM ldbm )
|
|
|
|
{
|
|
|
|
return( dbm_firstkey( ldbm ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_nextkey( LDBM ldbm, Datum key )
|
|
|
|
{
|
|
|
|
return( dbm_nextkey( ldbm ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_errno( LDBM ldbm )
|
|
|
|
{
|
|
|
|
return( dbm_error( ldbm ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ndbm */
|
1998-10-25 10:02:31 +08:00
|
|
|
#endif /* ldbm */
|