]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapindex.c
Add CSRIMALLOC support
[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         if( !be->be_entry_open &&
28                 !be->be_entry_close &&
29                 !be->be_entry_first &&
30                 !be->be_entry_next &&
31                 !be->be_entry_get &&
32                 !be->be_index_attr &&
33                 !be->be_index_change )
34         {
35                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
36                         progname );
37                 exit( EXIT_FAILURE );
38         }
39
40         type = attr_normalize( argv[argc - 1] );
41
42         if ( !be->be_index_attr( be, type ) ) {
43                 fprintf( stderr, "attribute type \"%s\": no indices to generate\n",
44                         type );
45                 exit( EXIT_FAILURE );
46         }
47
48         if( be->be_entry_open( be, 0 ) != 0 ) {
49                 fprintf( stderr, "%s: could not open database.\n",
50                         progname );
51                 exit( EXIT_FAILURE );
52         }
53
54         for ( id = be->be_entry_first( be );
55                 id != NOID;
56                 id = be->be_entry_next( be ) )
57         {
58                 Attribute *attr;
59                 struct berval **values;
60                 Entry* e = be->be_entry_get( be, id );
61                 struct berval bv;
62                 struct berval *bvals[2];
63
64                 if ( e == NULL ) {
65                         fprintf( stderr,
66                                 "entry id=%08lx: no data\n", (long) id );
67                         rc = EXIT_FAILURE;
68                         if( continuemode ) continue;
69                         break;
70                 }
71
72                 if( verbose ) {
73                         printf("indexing id=%08lx dn=\"%s\"\n",
74                                 id, e->e_dn );
75                 }
76
77                 if( strcasecmp( type, "dn" ) == 0 ) {
78                         attr = attr_find( e->e_attrs, type );
79
80                         if( attr == NULL ) {
81                                 entry_free( e );
82                                 continue;
83                         }
84
85                         values = attr->a_vals;
86
87                 } else {
88                         bv.bv_val = e->e_ndn;
89                         bv.bv_len = strlen( bv.bv_val );
90                         bvals[0] = &bv;
91                         bvals[1] = NULL;
92
93                         values = bvals;
94                 }
95
96                 if ( be->be_index_change( be,
97                         type, attr->a_vals, id, SLAP_INDEX_ADD_OP ) )
98                 {
99                         rc = EXIT_FAILURE;
100
101                         if( !continuemode ) {
102                                 entry_free( e );
103                                 break;
104                         }
105                 }
106
107                 entry_free( e );
108         }
109
110         (void) be->be_entry_close( be );
111
112         slap_tool_destroy();
113
114         return( rc );
115 }