]> git.sur5r.net Git - openldap/blobdiff - libraries/libldbm/ldbm.c
Sync with HEAD
[openldap] / libraries / libldbm / ldbm.c
index 03112e37f64b43f4778065c82e7cffb074f03333..11e7ea0cc54a4bb6f46e7e630b1a78bd0f70990a 100644 (file)
@@ -1,14 +1,31 @@
 /* ldbm.c - ldap dbm compatibility routines */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2003 The OpenLDAP Foundation.
+ * Portions Copyright 1998-2003 Kurt D. Zeilenga.
+ * Portions Copyright 1998-2001 Net Boolean Incorporated.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-
-/* 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
+/* ACKNOWLEDGEMENTS:
+ * This work was originally developed by the University of Michigan
+ * (as part of U-MICH LDAP).  Additional significant contributors
+ * include:
+ *   Gary Williams
+ *   Howard Chu
+ *   Juan Gomez
+ *   Kurt D. Zeilenga
+ *   Kurt Spanier
+ *   Mark Whitehouse
+ *   Randy Kundee
  */
 
 #include "portable.h"
@@ -91,6 +108,12 @@ static ldap_pvt_thread_mutex_t ldbm_big_mutex;
 DB_ENV *ldbm_Env = NULL;       /* real or fake, depending on db and version */
 #endif
 
+/* Let's make the version comparisons a little easier... */
+#undef DB_VERSION_X
+#ifdef HAVE_BERKELEY_DB
+#define        DB_VERSION_X    ((DB_VERSION_MAJOR<<16)|(DB_VERSION_MINOR<<8)|DB_VERSION_PATCH)
+#endif
+
 /*******************************************************************
  *                                                                 *
  *  Create some special functions to initialize Berkeley DB for    *
@@ -120,8 +143,10 @@ ldbm_db_errcall( const char *prefix, char *message )
 
 int ldbm_initialize( const char* home )
 {
+#if DB_VERSION_MAJOR < 3
        int     err;
        u_int32_t       envFlags;
+#endif
 
        if(ldbm_initialized++) return 1;
 
@@ -165,8 +190,7 @@ int ldbm_initialize( const char* home )
        if ( err ) {
 #ifdef LDAP_SYSLOG
                syslog( LOG_INFO, "ldbm_initialize(): "
-                       "FATAL error in db_appinit() : %s (%d)\n",
-                       db_strerror( err ), err );
+                       "FATAL error (%d) in db_appinit()\n", err );
 #endif
                return( 1 );
        }
@@ -218,6 +242,9 @@ DB_ENV *ldbm_initialize_env(const char *home, int dbcachesize, int *envdirok)
        DB_ENV *env = NULL;    
        int     err;
        u_int32_t       envFlags;
+#ifdef HAVE_EBCDIC
+       char n2[2048];
+#endif
 
        err = db_env_create( &env, 0 );
 
@@ -230,7 +257,8 @@ DB_ENV *ldbm_initialize_env(const char *home, int dbcachesize, int *envdirok)
                return NULL;
        }
 
-#if DB_VERSION_MINOR >= 3
+#if DB_VERSION_X >= 0x030300
+       /* This interface appeared in 3.3 */
        env->set_alloc( env, ldbm_malloc, NULL, NULL );
 #endif
 
@@ -251,7 +279,13 @@ DB_ENV *ldbm_initialize_env(const char *home, int dbcachesize, int *envdirok)
        envFlags |= DB_THREAD;
 #endif
 
-#if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 0
+#ifdef HAVE_EBCDIC
+       strncpy(n2, home, sizeof(n2)-1);
+       n2[sizeof(n2)-1] = '\0';
+       __atoe(n2);
+       home = n2;
+#endif
+#if DB_VERSION_X >= 0x030100
        err = env->open( env, home, envFlags, 0 );
 #else
        /* 3.0.x requires an extra argument */
@@ -302,6 +336,9 @@ LDBM
 ldbm_open( DB_ENV *env, char *name, int rw, int mode, int dbcachesize )
 {
        LDBM            ret = NULL;
+#ifdef HAVE_EBCDIC
+       char n2[2048];
+#endif
 
 #if DB_VERSION_MAJOR >= 3
        int err;
@@ -316,7 +353,7 @@ ldbm_open( DB_ENV *env, char *name, int rw, int mode, int dbcachesize )
                return NULL;
        }
 
-#if DB_VERSION_MINOR < 3
+#if DB_VERSION_X < 0x030300
        ret->set_malloc( ret, ldbm_malloc );
 #endif
 
@@ -324,7 +361,17 @@ ldbm_open( DB_ENV *env, char *name, int rw, int mode, int dbcachesize )
 
        /* likely should use ber_mem* routines */
 
+#ifdef HAVE_EBCDIC
+       strncpy(n2, name, sizeof(n2)-1);
+       n2[sizeof(n2)-1] = '\0';
+       __atoe(n2);
+       name = n2;
+#endif
+#if DB_VERSION_X >= 0x040111
+       err = ret->open( ret, NULL, name, NULL, DB_TYPE, rw, mode);
+#else
        err = ret->open( ret, name, NULL, DB_TYPE, rw, mode);
+#endif
 
        if ( err != 0 ) {
                int tmp = errno;
@@ -390,12 +437,10 @@ void
 ldbm_close( LDBM ldbm )
 {
        LDBM_WLOCK;
-#if DB_VERSION_MAJOR >= 3
+#if DB_VERSION_MAJOR >= 2
        ldbm->close( ldbm, 0 );
-#elif DB_VERSION_MAJOR >= 2
-       (*ldbm->close)( ldbm, 0 );
 #else
-       (*ldbm->close)( ldbm );
+       ldbm->close( ldbm );
 #endif
        LDBM_WUNLOCK;
 }
@@ -416,7 +461,7 @@ ldbm_fetch( LDBM ldbm, Datum key )
 
        LDBM_RLOCK;
 
-#if DB_VERSION_MAJOR >= 3
+#if DB_VERSION_MAJOR >= 2
        ldbm_datum_init( data );
 
        data.flags = DB_DBT_MALLOC;
@@ -426,20 +471,8 @@ ldbm_fetch( LDBM ldbm, Datum key )
                data.dptr = NULL;
                data.dsize = 0;
        }
-
-#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 );
-               data.dptr = NULL;
-               data.dsize = 0;
-       }
 #else
-
-       if ( (rc = (*ldbm->get)( ldbm, &key, &data, 0 )) == 0 ) {
+       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 );
@@ -461,19 +494,15 @@ ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
 
        LDBM_WLOCK;
 
-#if DB_VERSION_MAJOR >= 3
+#if DB_VERSION_MAJOR >= 2
        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;
 #else
-       rc = (*ldbm->put)( ldbm, &key, &data, flags & ~LDBM_SYNC );
+       rc = ldbm->put( ldbm, &key, &data, flags & ~LDBM_SYNC );
 #endif
 
        if ( flags & LDBM_SYNC )
-               (*ldbm->sync)( ldbm, 0 );
+               ldbm->sync( ldbm, 0 );
 
        LDBM_WUNLOCK;
 
@@ -487,16 +516,13 @@ ldbm_delete( LDBM ldbm, Datum key )
 
        LDBM_WLOCK;
 
-#if DB_VERSION_MAJOR >= 3
+#if DB_VERSION_MAJOR >= 2
        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
-       rc = (*ldbm->del)( ldbm, &key, 0 );
+       rc = ldbm->del( ldbm, &key, 0 );
 #endif
-       (*ldbm->sync)( ldbm, 0 );
+       ldbm->sync( ldbm, 0 );
 
        LDBM_WUNLOCK;
 
@@ -520,20 +546,17 @@ ldbm_firstkey( LDBM ldbm, LDBMCursor **dbch )
        LDBM_RLOCK;
 
        /* acquire a cursor for the DB */
-# if DB_VERSION_MAJOR >= 3
+# if DB_VERSION_X >= 0x020600
        rc = ldbm->cursor( ldbm, NULL, &dbci, 0 );
-# elif defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
-       (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6)
-       rc = (*ldbm->cursor)( ldbm, NULL, &dbci );
 # else
-       rc = (*ldbm->cursor)( ldbm, NULL, &dbci, 0 );
+       rc = ldbm->cursor( ldbm, NULL, &dbci );
 # endif
 
        if( rc ) {
                key.dptr = NULL;
        } else {
                *dbch = dbci;
-               if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
+               if ( dbci->c_get( dbci, &key, &data, DB_NEXT ) == 0 ) {
                        ldbm_datum_free( ldbm, data );
                } else {
                        key.dptr = NULL;
@@ -546,7 +569,7 @@ ldbm_firstkey( LDBM ldbm, LDBMCursor **dbch )
 #else
        LDBM_RLOCK;
 
-       rc = (*ldbm->seq)( ldbm, &key, &data, R_FIRST );
+       rc = ldbm->seq( ldbm, &key, &data, R_FIRST );
 
        if ( rc == 0 ) {
                key = ldbm_datum_dup( ldbm, key );
@@ -575,12 +598,12 @@ ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
        ldbm_datum_free( ldbm, key );
        key.flags = data.flags = DB_DBT_MALLOC;
 
-       rc = (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT );
+       rc = dbcp->c_get( dbcp, &key, &data, DB_NEXT );
        if ( rc == 0 ) {
                ldbm_datum_free( ldbm, data );
        } else
 #else
-       rc = (*ldbm->seq)( ldbm, &key, &data, R_NEXT );
+       rc = ldbm->seq( ldbm, &key, &data, R_NEXT );
 
        if ( rc == 0 ) {
                key = ldbm_datum_dup( ldbm, key );
@@ -626,6 +649,14 @@ ldbm_open( DB_ENV *env, char *name, int rw, int mode, int dbcachesize )
 #ifdef HAVE_ST_BLKSIZE
                struct stat     st;
 #endif
+#ifdef HAVE_EBCDIC
+       char n2[2048];
+
+       strncpy(n2, name, sizeof(n2)-1);
+       n2[sizeof(n2)-1] = '\0';
+       __atoe(n2);
+       name = n2;
+#endif
 
        LDBM_WLOCK;