Back to index

openldap  2.4.31
slapindex.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Kurt Zeilenga for inclusion
00018  * in OpenLDAP Software.
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <stdio.h>
00024 
00025 #include <ac/stdlib.h>
00026 
00027 #include <ac/ctype.h>
00028 #include <ac/string.h>
00029 #include <ac/socket.h>
00030 #include <ac/unistd.h>
00031 
00032 #include "slapcommon.h"
00033 
00034 int
00035 slapindex( int argc, char **argv )
00036 {
00037        ID id;
00038        int rc = EXIT_SUCCESS;
00039        const char *progname = "slapindex";
00040        AttributeDescription *ad, **adv = NULL;
00041 
00042        slap_tool_init( progname, SLAPINDEX, argc, argv );
00043 
00044        if( !be->be_entry_open ||
00045               !be->be_entry_close ||
00046               !( be->be_entry_first || be->be_entry_first_x ) ||
00047               !be->be_entry_next  ||
00048               !be->be_entry_reindex )
00049        {
00050               fprintf( stderr, "%s: database doesn't support necessary operations.\n",
00051                      progname );
00052               exit( EXIT_FAILURE );
00053        }
00054 
00055        argc -= optind;
00056        if ( argc > 0 ) {
00057               const char *text;
00058               int i;
00059 
00060               argv = &argv[optind];
00061               adv = (AttributeDescription **)argv;
00062 
00063               for (i = 0; i < argc; i++ ) {
00064                      ad = NULL;
00065                      rc = slap_str2ad( argv[i], &ad, &text );
00066                      if ( rc != LDAP_SUCCESS ) {
00067                             fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
00068                                    argv[i], rc, ldap_err2string( rc ));
00069                             exit( EXIT_FAILURE );
00070                      }
00071                      adv[i] = ad;
00072               }
00073        }
00074 
00075        if( be->be_entry_open( be, 0 ) != 0 ) {
00076               fprintf( stderr, "%s: could not open database.\n",
00077                      progname );
00078               exit( EXIT_FAILURE );
00079        }
00080 
00081        if ( be->be_entry_first ) {
00082               id = be->be_entry_first( be );
00083 
00084        } else {
00085               assert( be->be_entry_first_x != NULL );
00086               id = be->be_entry_first_x( be, NULL, LDAP_SCOPE_DEFAULT, NULL );
00087        }
00088 
00089        for ( ; id != NOID; id = be->be_entry_next( be ) ) {
00090               int rtn;
00091 
00092               if( verbose ) {
00093                      printf("indexing id=%08lx\n", (long) id );
00094               }
00095 
00096               rtn =  be->be_entry_reindex( be, id, adv );
00097 
00098               if( rtn != LDAP_SUCCESS ) {
00099                      rc = EXIT_FAILURE;
00100                      if( continuemode ) continue;
00101                      break;
00102               }
00103        }
00104 
00105        (void) be->be_entry_close( be );
00106 
00107        if ( slap_tool_destroy())
00108               rc = EXIT_FAILURE;
00109        return( rc );
00110 }