1/* $OpenLDAP$ */
2/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1998-2011 The OpenLDAP Foundation.
5 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
15 */
16/* ACKNOWLEDGEMENTS:
17 * This work was initially developed by Kurt Zeilenga for inclusion
18 * in OpenLDAP Software.
19 */
20
21#include "portable.h"
22
23#include <stdio.h>
24
25#include <ac/stdlib.h>
26
27#include <ac/ctype.h>
28#include <ac/string.h>
29#include <ac/socket.h>
30#include <ac/unistd.h>
31
32#include "slapcommon.h"
33
34int
35slapindex( int argc, char **argv )
36{
37	ID id;
38	int rc = EXIT_SUCCESS;
39	const char *progname = "slapindex";
40	AttributeDescription *ad, **adv = NULL;
41
42	slap_tool_init( progname, SLAPINDEX, argc, argv );
43
44	if( !be->be_entry_open ||
45		!be->be_entry_close ||
46		!( be->be_entry_first || be->be_entry_first_x ) ||
47		!be->be_entry_next  ||
48		!be->be_entry_reindex )
49	{
50		fprintf( stderr, "%s: database doesn't support necessary operations.\n",
51			progname );
52		exit( EXIT_FAILURE );
53	}
54
55	argc -= optind;
56	if ( argc > 0 ) {
57		const char *text;
58		int i;
59
60		argv = &argv[optind];
61		adv = (AttributeDescription **)argv;
62
63		for (i = 0; i < argc; i++ ) {
64			ad = NULL;
65			rc = slap_str2ad( argv[i], &ad, &text );
66			if ( rc != LDAP_SUCCESS ) {
67				fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
68					argv[i], rc, ldap_err2string( rc ));
69				exit( EXIT_FAILURE );
70			}
71			adv[i] = ad;
72		}
73	}
74
75	if( be->be_entry_open( be, 0 ) != 0 ) {
76		fprintf( stderr, "%s: could not open database.\n",
77			progname );
78		exit( EXIT_FAILURE );
79	}
80
81	if ( be->be_entry_first ) {
82		id = be->be_entry_first( be );
83
84	} else {
85		assert( be->be_entry_first_x != NULL );
86		id = be->be_entry_first_x( be, NULL, LDAP_SCOPE_DEFAULT, NULL );
87	}
88
89	for ( ; id != NOID; id = be->be_entry_next( be ) ) {
90		int rtn;
91
92		if( verbose ) {
93			printf("indexing id=%08lx\n", (long) id );
94		}
95
96		rtn =  be->be_entry_reindex( be, id, adv );
97
98		if( rtn != LDAP_SUCCESS ) {
99			rc = EXIT_FAILURE;
100			if( continuemode ) continue;
101			break;
102		}
103	}
104
105	(void) be->be_entry_close( be );
106
107	if ( slap_tool_destroy())
108		rc = EXIT_FAILURE;
109	return( rc );
110}
111