]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/tools/ldbmtest.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[openldap] / servers / slapd / tools / ldbmtest.c
index ddda1aae12854dcc5ff4200ce4d89327dd8aefff..1897c0245b83721dc97f7148e26e8ccee9de4cae 100644 (file)
@@ -1,9 +1,14 @@
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdlib.h>
 #include <limits.h>
 
+#include <ac/stdlib.h>
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/ctype.h>
 #include <ac/unistd.h>
 #include <ac/wait.h>
 
+#ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
+#endif
+#ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
+#endif
 #include <sys/stat.h>
 
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
 
-#include "ldapconfig.h"
+#ifdef HAVE_IO_H
+#include <io.h>
+#endif
+
 #include "../slap.h"
 #include "../back-ldbm/back-ldbm.h"
 
-#define EDITOR "/usr/ucb/vi"
-
-static struct dbcache  *openchoice(char c, int mode, int verbose, char **fname);
+static DBCache *openchoice(char c, int mode, int verbose, char **fname);
 static void            print_entry(FILE *fp, char c, Datum *key, char *klabel, Datum *data, char *dlabel);
-static void            free_and_close(struct dbcache *dbc, Datum key, Datum data);
+static void            free_and_close(DBCache *dbc, Datum key, Datum data);
 static void            edit_entry(char c, Datum *data);
 static void            get_keydata(FILE *fp, char c, Datum *key, Datum *data);
 
-struct dbcache *dbc;
-LDBM           dbp;
-char           *tailorfile;
-Backend                *be = NULL;
-int            ldap_debug;
-int            ldap_syslog;
-int            ldap_syslog_level;
-long           num_entries_sent;
-long           num_bytes_sent;
-int            active_threads;
-char           *default_referral;
-time_t         currenttime;
-pthread_t      listener_tid;
-pthread_mutex_t        num_sent_mutex;
-pthread_mutex_t        entry2str_mutex;
-pthread_mutex_t        active_threads_mutex;
-pthread_mutex_t        new_conn_mutex;
-pthread_mutex_t        currenttime_mutex;
-pthread_mutex_t        replog_mutex;
-pthread_mutex_t        ops_mutex;
-pthread_mutex_t        regex_mutex;
+static DBCache *dbc;
+static LDBM           dbp;
+static Backend         *be = NULL;
 
 int
 main( int argc, char **argv )
@@ -59,19 +51,23 @@ main( int argc, char **argv )
        char            buf[256];
        Datum           savekey, key, data, last;
        char            *fname;
-       ID              id;
-       IDList          *idl;
+       ID              id, cursor;
+       ID_BLOCK                *idl;
        Backend         *tbe;
        int             i;
+       char            *tailorfile;
+
+       LDBMCursor      *cursorp;
+
+       fprintf(stderr,
+               "ldbmtest not updated to support new index formats!\n" );
+       exit( EXIT_FAILURE );
 
-#ifdef HAVE_BERKELEY_DB2
-       DBC     *cursorp;
 
        ldbm_datum_init( savekey );
        ldbm_datum_init( key );
        ldbm_datum_init( data );
        ldbm_datum_init( last );
-#endif
 
        tailorfile = SLAPD_DEFAULT_CONFIGFILE;
        while ( (i = getopt( argc, argv, "d:f:" )) != EOF ) {
@@ -88,8 +84,7 @@ main( int argc, char **argv )
                        fprintf( stderr,
                            "usage: %s [-d level] [-f slapdconfigfile]\n",
                            argv[0] );
-                       exit( -1 );
-                       break;
+                       exit( EXIT_FAILURE );
                }
        }
 
@@ -97,8 +92,9 @@ main( int argc, char **argv )
         * initialize stuff and figure out which backend we're dealing with
         */
 
-       init();
-       read_config( tailorfile, &be, NULL );
+       slap_init(SLAP_TOOL_MODE, "ldbmtest");
+       read_config( tailorfile );
+       slap_startup( NULL );
 
        while ( 1 ) {
                printf( "dbtest: " );
@@ -121,7 +117,7 @@ main( int argc, char **argv )
                        }
                        break;
 
-               case 'l':       /* lookup somethig in an index */
+               case 'l':       /* lookup something in an index */
                        if ( (dbc = openchoice( buf[1], LDBM_READER, 1, NULL ))
                            == NULL ) {
                                continue;
@@ -144,7 +140,7 @@ main( int argc, char **argv )
                        get_keydata( stdin, buf[1], &key, NULL );
                        if ( (idl = idl_fetch( be, dbc, key )) != NULL ) {
                                data.dptr = (char *) idl;
-                               data.dsize = (idl->b_nmax + 1) * sizeof(ID);
+                               data.dsize = (ID_BLOCK_NMAX(idl) + 1) * sizeof(ID);
                                print_entry( stdout, buf[1], &key, "key: ",
                                    &data, "data:\n" );
                        }
@@ -160,15 +156,9 @@ main( int argc, char **argv )
                        }
 
                        savekey.dptr = NULL;
-#ifdef HAVE_BERKELEY_DB2
                        for ( key = ldbm_firstkey( dbc->dbc_db, &cursorp );
                            key.dptr != NULL;
                            key = ldbm_nextkey( dbc->dbc_db, key, cursorp ) )
-#else
-                       for ( key = ldbm_firstkey( dbc->dbc_db );
-                           key.dptr != NULL;
-                           key = ldbm_nextkey( dbc->dbc_db, key ) )
-#endif
                        {
                                if ( savekey.dptr != NULL )
                                        ldbm_datum_free( dbc->dbc_db, savekey );
@@ -206,7 +196,7 @@ main( int argc, char **argv )
                                if ( ldbm_errno( dbc->dbc_db ) == 0 ) {
                                        perror( "ldbm_delete" );
                                } else {
-                                       fprintf( stderr, "db_errno %d",
+                                       fprintf( stderr, "db_errno=%d",
                                            ldbm_errno( dbc->dbc_db ) );
                                }
                        }
@@ -215,6 +205,7 @@ main( int argc, char **argv )
                        free_and_close( dbc, key, data );
                        break;
 
+#ifndef HAVE_WINSOCK
                case 'e':       /* edit an entry */
                        if ( (dbc = openchoice( buf[1], LDBM_WRITER, 1, NULL ))
                            == NULL ) {
@@ -228,7 +219,7 @@ main( int argc, char **argv )
                                if ( ldbm_errno( dbc->dbc_db ) == 0 ) {
                                        perror( "ldbm_fetch" );
                                } else {
-                                       fprintf( stderr, "db_errno %d\n",
+                                       fprintf( stderr, "db_errno=%d\n",
                                            ldbm_errno( dbc->dbc_db ) );
                                }
                                free_and_close( dbc, key, data );
@@ -246,13 +237,14 @@ main( int argc, char **argv )
                                if ( ldbm_errno( dbc->dbc_db ) == 0 ) {
                                        perror( "ldbm_store" );
                                } else {
-                                       fprintf( stderr, "db_errno %d\n",
+                                       fprintf( stderr, "db_errno=%d\n",
                                            ldbm_errno( dbc->dbc_db ) );
                                }
                        }
 
                        free_and_close( dbc, key, data );
                        break;
+#endif
 
                case 'a':       /* add an entry */
                        if ( (dbc = openchoice( buf[1], LDBM_WRITER, 1, NULL ))
@@ -267,7 +259,7 @@ main( int argc, char **argv )
                                if ( ldbm_errno( dbc->dbc_db ) == 0 ) {
                                        perror( "ldbm_store" );
                                } else {
-                                       fprintf( stderr, "db_errno %d\n",
+                                       fprintf( stderr, "db_errno=%d\n",
                                            ldbm_errno( dbc->dbc_db ) );
                                }
                        }
@@ -283,9 +275,9 @@ main( int argc, char **argv )
 
                        get_keydata( stdin, buf[1], &key, &data );
 
-                       idl = (IDList *) data.dptr;
-                       for ( id = idl_firstid( idl ); id != NOID;
-                           id = idl_nextid( idl, id ) ) {
+                       idl = (ID_BLOCK *) data.dptr;
+                       for ( id = idl_firstid( idl, &cursor ); id != NOID;
+                           id = idl_nextid( idl, &cursor ) ) {
                                if ( idl_insert_key( be, dbc, key, id )
                                    != 0 ) {
                                        fprintf( stderr,
@@ -302,11 +294,11 @@ main( int argc, char **argv )
                        printf( "suffix: " );
                        fflush( stdout );
                        if ( fgets( buf, sizeof(buf), stdin ) == NULL ) {
-                               exit( 0 );
+                               exit( EXIT_SUCCESS );
                        } else {
                                buf[strlen( buf ) - 1] = '\0';
                        }
-                       (void) dn_normalize( buf );
+                       (void) dn_normalize_case( buf );
                        if ( (tbe = select_backend( buf )) == NULL ) {
                                fprintf( stderr, "unknown suffix \"%s\"\n",
                                    buf );
@@ -332,14 +324,9 @@ main( int argc, char **argv )
 
                        last.dptr = NULL;
 
-#ifdef HAVE_BERKELEY_DB2
                        for ( key = ldbm_firstkey( dbp, &cursorp );
                                key.dptr != NULL;
                                key = ldbm_nextkey( dbp, last, cursorp ) )
-#else
-                       for ( key = ldbm_firstkey( dbp ); key.dptr != NULL;
-                           key = ldbm_nextkey( dbp, last ) )
-#endif
                        {
                                if ( last.dptr != NULL ) {
                                        ldbm_datum_free( dbp, last );
@@ -365,7 +352,6 @@ main( int argc, char **argv )
                        printf( "          b    => change default backend\n" );
                        printf( "          B    => print default backend\n" );
                        printf( "where <c> is a char selecting the index:\n" );
-                       printf( "          c => id2children\n" );
                        printf( "          d => dn2id\n" );
                        printf( "          e => id2entry\n" );
                        printf( "          f => arbitrary file\n" );
@@ -374,11 +360,14 @@ main( int argc, char **argv )
                }
        }
 
-       return( 0 );
+       slap_shutdown( NULL );
+       slap_destroy();
+
+       return( EXIT_SUCCESS );
 }
 
 static void
-free_and_close( struct dbcache *dbc, Datum key, Datum data )
+free_and_close( DBCache *dbc, Datum key, Datum data )
 {
        ldbm_cache_really_close( be, dbc );
        if ( key.dptr != NULL )
@@ -406,10 +395,10 @@ static void
 get_idlist( FILE *fp, Datum *data )
 {
        char    buf[20];
-       int     i, j, fd, tty;
-       IDList  *p;
-       int     psize, pmax;
-       int     nmax, nids;
+       int     i, fd, tty;
+       ID_BLOCK        *p;
+       unsigned int    psize, pmax;
+       unsigned int    nmax, nids;
 
        fd = fileno( fp );
        tty = isatty( fd );
@@ -432,7 +421,7 @@ get_idlist( FILE *fp, Datum *data )
 
                if ( psize + sizeof(ID) > pmax ) {
                        pmax += BUFSIZ;
-                       p = (IDList *) myrealloc( (char *) p, pmax );
+                       p = (ID_BLOCK *) myrealloc( (char *) p, pmax );
                }
 
                if ( strncmp( buf, "nids=0", 6 ) == 0 ) {
@@ -440,7 +429,7 @@ get_idlist( FILE *fp, Datum *data )
                        continue;
                }
 
-               p->b_ids[i++] = atol( buf );
+               ID_BLOCK_ID(p,i++) = atol( buf );
                psize += sizeof(ID);
        }
        if ( nmax == 0 ) {
@@ -449,7 +438,7 @@ get_idlist( FILE *fp, Datum *data )
                        printf( "%d IDs entered.  Max number of ids? [%d] ", i,
                            i );
                        if ( fgets( buf, sizeof(buf), fp ) != NULL &&
-                           isdigit( buf[0] ) ) {
+                            isdigit( (unsigned char) buf[0] ) ) {
                                nmax = atol( buf );
                        }
                } else {
@@ -457,19 +446,19 @@ get_idlist( FILE *fp, Datum *data )
                }
        }
        if ( i > 0 ) {
-               p->b_nmax = nmax;
+               ID_BLOCK_NMAX(p) = nmax;
                if ( nids != 0 ) {
-                       p->b_nids = 0;
-                       p->b_ids[i] = NOID;
+                       ID_BLOCK_NIDS(p) = 0;
+                       ID_BLOCK_ID(p,i) = NOID;
                } else {
-                       p->b_nids = i;
+                       ID_BLOCK_NIDS(p) = i;
                }
 
-               qsort( (void *) p->b_ids, i, sizeof(ID), dnid_cmp );
+               qsort( (void *) &ID_BLOCK_ID(p, 0), i, sizeof(ID), dnid_cmp );
        }
 
        data->dptr = (char *) p;
-       data->dsize = (nmax + 2) * sizeof(ID);
+       data->dsize = (nmax + ID_BLOCK_IDS_OFFSET) * sizeof(ID);
 }
 
 static void
@@ -477,7 +466,7 @@ get_entry( FILE *fp, Datum *data )
 {
        char    buf[BUFSIZ];
        char    *p;
-       int     pmax, psize, len;
+       unsigned int    pmax, psize, len;
        int     fd;
 
        fd = fileno( fp );
@@ -506,6 +495,7 @@ get_entry( FILE *fp, Datum *data )
        data->dsize = psize + 1;
 }
 
+#ifndef HAVE_WINSOCK
 static void
 edit_entry( char c, Datum *data )
 {
@@ -542,20 +532,21 @@ edit_entry( char c, Datum *data )
                char    *editor;
 
                if ( (editor = getenv( "EDITOR" )) == NULL ) {
-                       editor = EDITOR;
+                       editor = LDAP_EDITOR;
                }
                execl( editor, editor, tmpname, NULL );
                perror( "execl" );
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
 
        fclose( fp );
  
 #ifdef HAVE_WAITPID
-       if ( waitpid( (pid_t) -1, NULL, WAIT_FLAGS ) < 0 ) {
+       if ( waitpid( (pid_t) -1, NULL, WAIT_FLAGS ) < 0 )
 #else
-       if ( wait3( &status, WAIT_FLAGS, 0 ) < 0 ) {
+       if ( wait3( (pid_t) -1, &status, WAIT_FLAGS, 0 ) < 0 )
 #endif
+       {
                perror( "wait" );
                return;
        }
@@ -571,22 +562,23 @@ edit_entry( char c, Datum *data )
        fclose( fp );
        unlink( tmpname );
 }
+#endif
 
-static struct dbcache *
+static DBCache *
 openfile( char *name, int namesiz, int mode, int verbose, char c )
 {
-       struct dbcache  *dbc;
+       DBCache *dbc;
 
        if ( name == NULL || *name == '\0' ) {
                if ( c == 'f' ) {
                        printf( "  file: " );
                        if ( fgets( name, namesiz, stdin ) == NULL )
-                               exit( 0 );
+                               exit( EXIT_SUCCESS );
                        name[strlen( name ) - 1] = '\0';
                } else {
                        printf( "  attr: " );
                        if ( fgets( name, namesiz, stdin ) == NULL )
-                               exit( 0 );
+                               exit( EXIT_SUCCESS );
                        name[strlen( name ) - 1] = '\0';
                }
        }
@@ -601,15 +593,12 @@ openfile( char *name, int namesiz, int mode, int verbose, char c )
        return( dbc );
 }
 
-static struct dbcache *
+static DBCache *
 openchoice( char c, int mode, int verbose, char **fname )
 {
        static char     name[MAXPATHLEN];
 
        switch ( c ) {
-       case 'c':       /* id2children */
-               sprintf( name, "id2children" );
-               break;
        case 'd':       /* dn2id */
                sprintf( name, "dn2id" );
                break;
@@ -625,7 +614,7 @@ openchoice( char c, int mode, int verbose, char **fname )
                }
                break;
        default:
-               printf( "specify one of [fdeci] to select file\n" );
+               printf( "specify one of [fdei] to select file\n" );
                return( NULL );
                break;
        }
@@ -647,8 +636,8 @@ print_entry(
 )
 {
        ID      id;
-       IDList  *idl;
-       int     i;
+       ID_BLOCK        *idl;
+       unsigned int    i;
        char    msg[2];
 
        if ( data != NULL && data->dptr == NULL ) {
@@ -658,7 +647,7 @@ print_entry(
                if ( ldbm_errno( dbp ) == 0 )
                        perror( msg );
                else
-                       fprintf( stderr, "%s: db_errno %d\n", msg,
+                       fprintf( stderr, "%s: db_errno=%d\n", msg,
                            ldbm_errno( dbp ) );
                return;
        }
@@ -677,7 +666,7 @@ print_entry(
        case 'e':       /* id2entry - key is dnid, data is entry */
                if ( key != NULL ) {
                        SAFEMEMCPY( (char *) &id, key->dptr, sizeof(ID) );
-                       fprintf( fp, "%s %lu\n", klabel, id );
+                       fprintf( fp, "%s %ld\n", klabel, id );
                }
                if ( data != NULL ) {
                        if ( dlabel ) {
@@ -688,28 +677,26 @@ print_entry(
                }
                break;
 
-       case 'c':
        case 'i':       /* index - key is string, data is dnid[] */
                if ( key != NULL )
                        fprintf( fp, "%s%s (len %d)\n", klabel, key->dptr,
                            key->dsize );
                if ( data != NULL ) {
-                       idl = (IDList *) data->dptr;
+                       idl = (ID_BLOCK *) data->dptr;
 
                        if ( dlabel )
                                fprintf( fp, "%s\tnmax=%ld\n\tncur=%ld\n", dlabel,
-                                   idl->b_nmax, idl->b_nids );
+                                   ID_BLOCK_NMAX(idl), ID_BLOCK_NIDS(idl) );
 
-                       if ( INDIRECT_BLOCK( idl ) ) {
-                               for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
-                                       fprintf( fp, "\t%ld\n", idl->b_ids[i] );
+                       if ( ID_BLOCK_INDIRECT( idl ) ) {
+                               for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
+                                       fprintf( fp, "\t%ld\n", ID_BLOCK_ID(idl, i) );
                                }
-                       } else if ( ALLIDS( idl ) ) {
-                               fprintf( fp, "\tALLIDS (1..%ld)\n",
-                                   idl->b_nids - 1 );
+                       } else if ( ID_BLOCK_ALLIDS( idl ) ) {
+                               fprintf( fp, "\tALLIDS\n" );
                        } else {
-                               for ( i = 0; i < idl->b_nids; i++ ) {
-                                       fprintf( fp, "\t%ld\n", idl->b_ids[i] );
+                               for ( i = 0; i < ID_BLOCK_NIDS(idl); i++ ) {
+                                       fprintf( fp, "\t%ld\n", ID_BLOCK_ID(idl,i) );
                                }
                        }
                }
@@ -725,7 +712,7 @@ print_entry(
                break;
 
        default:
-               fprintf( stderr, "specify [deci] to select a file\n" );
+               fprintf( stderr, "specify [dei] to select a file\n" );
                break;
        }
 }
@@ -746,7 +733,7 @@ get_keydata( FILE *fp, char c, Datum *key, Datum *data )
                        if ( tty )
                                printf( "  dn: " );
                        if ( fgets( kbuf, sizeof(kbuf), fp ) == NULL ) {
-                               exit( 0 );
+                               exit( EXIT_SUCCESS );
                        }
                        kbuf[strlen( kbuf ) - 1] = '\0';
                        key->dptr = strdup( kbuf );
@@ -757,7 +744,7 @@ get_keydata( FILE *fp, char c, Datum *key, Datum *data )
                        if ( tty )
                                printf( "  dnid: " );
                        if ( fgets( dbuf, sizeof(dbuf), fp ) == NULL ) {
-                               exit( 0 );
+                               exit( EXIT_SUCCESS );
                        }
                        n = atol( dbuf );
                        data->dptr = (char *) malloc( sizeof(n) );
@@ -771,7 +758,7 @@ get_keydata( FILE *fp, char c, Datum *key, Datum *data )
                        if ( tty )
                                printf( "  dnid: " );
                        if ( fgets( kbuf, sizeof(kbuf), fp ) == NULL ) {
-                               exit( 0 );
+                               exit( EXIT_SUCCESS );
                        }
                        n = atol( kbuf );
                        key->dptr = (char *) malloc( sizeof(n) );
@@ -784,13 +771,12 @@ get_keydata( FILE *fp, char c, Datum *key, Datum *data )
                }
                break;
 
-       case 'c':       /* id2children - key is string dnid, data is dnid[] */
        case 'i':       /* index - key is string, data is dnid[] */
                if ( key != NULL ) {
                        if ( tty )
                                printf( "  key: " );
                        if ( fgets( kbuf, sizeof(kbuf), fp ) == NULL ) {
-                               exit( 0 );
+                               exit( EXIT_SUCCESS );
                        }
                        kbuf[strlen( kbuf ) - 1] = '\0';
                        key->dptr = strdup( kbuf );
@@ -803,7 +789,7 @@ get_keydata( FILE *fp, char c, Datum *key, Datum *data )
                break;
 
        default:
-               fprintf(stderr, "specify [deci] to select file type\n");
+               fprintf(stderr, "specify [dei] to select file type\n");
                break;
        }
 }