]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapindex.c
Yet another round of SLAPD_SCHEMA_NOT_COMPAT changes...
[openldap] / servers / slapd / tools / slapindex.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/stdlib.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <ac/unistd.h>
16
17 #include "slapcommon.h"
18
19 int
20 main( int argc, char **argv )
21 {
22         char            *type;
23         ID id;
24         int rc = EXIT_SUCCESS;
25
26         slap_tool_init( "slapindex", SLAPINDEX, argc, argv );
27
28         if( !be->be_entry_open &&
29                 !be->be_entry_close &&
30                 !be->be_entry_first &&
31                 !be->be_entry_next &&
32                 !be->be_entry_get &&
33                 !be->be_index_attr &&
34                 !be->be_index_change )
35         {
36                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
37                         progname );
38                 exit( EXIT_FAILURE );
39         }
40
41 #ifdef SLAPD_SCHEMA_NOT_COMPAT
42         type = argv[argc - 1];
43 #else
44         type = attr_normalize( argv[argc - 1] );
45 #endif
46
47         if ( !be->be_index_attr( be, type ) ) {
48                 fprintf( stderr, "attribute type \"%s\": no indices to generate\n",
49                         type );
50                 exit( EXIT_FAILURE );
51         }
52
53         if( be->be_entry_open( be, 0 ) != 0 ) {
54                 fprintf( stderr, "%s: could not open database.\n",
55                         progname );
56                 exit( EXIT_FAILURE );
57         }
58
59         for ( id = be->be_entry_first( be );
60                 id != NOID;
61                 id = be->be_entry_next( be ) )
62         {
63                 struct berval **values;
64                 Entry* e = be->be_entry_get( be, id );
65                 struct berval bv;
66                 struct berval *bvals[2];
67
68                 if ( e == NULL ) {
69                         fprintf( stderr,
70                                 "entry id=%08lx: no data\n", (long) id );
71                         rc = EXIT_FAILURE;
72                         if( continuemode ) continue;
73                         break;
74                 }
75
76                 if( verbose ) {
77                         printf("indexing id=%08lx dn=\"%s\"\n",
78                                 id, e->e_dn );
79                 }
80
81                 if( strcasecmp( type, "dn" ) == 0 ) {
82                         bv.bv_val = e->e_ndn;
83                         bv.bv_len = strlen( bv.bv_val );
84                         bvals[0] = &bv;
85                         bvals[1] = NULL;
86
87                         values = bvals;
88
89                 } else {
90                         Attribute *attr = attr_find( e->e_attrs, type );
91
92                         if( attr == NULL ) {
93                                 entry_free( e );
94                                 continue;
95                         }
96
97                         values = attr->a_vals;
98                 }
99
100                 if ( be->be_index_change( be,
101                         type, values, id, SLAP_INDEX_ADD_OP ) )
102                 {
103                         rc = EXIT_FAILURE;
104
105                         if( !continuemode ) {
106                                 entry_free( e );
107                                 break;
108                         }
109                 }
110
111                 entry_free( e );
112         }
113
114         (void) be->be_entry_close( be );
115
116         slap_tool_destroy();
117
118         return( rc );
119 }