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