]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapindex.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[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         type = attr_normalize( argv[argc - 1] );
42
43         if ( !be->be_index_attr( be, type ) ) {
44                 fprintf( stderr, "attribute type \"%s\": no indices to generate\n",
45                         type );
46                 exit( EXIT_FAILURE );
47         }
48
49         if( be->be_entry_open( be, 0 ) != 0 ) {
50                 fprintf( stderr, "%s: could not open database.\n",
51                         progname );
52                 exit( EXIT_FAILURE );
53         }
54
55         for ( id = be->be_entry_first( be );
56                 id != NOID;
57                 id = be->be_entry_next( be ) )
58         {
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                         bv.bv_val = e->e_ndn;
79                         bv.bv_len = strlen( bv.bv_val );
80                         bvals[0] = &bv;
81                         bvals[1] = NULL;
82
83                         values = bvals;
84
85                 } else {
86                         Attribute *attr = attr_find( e->e_attrs, type );
87
88                         if( attr == NULL ) {
89                                 entry_free( e );
90                                 continue;
91                         }
92
93                         values = attr->a_vals;
94                 }
95
96                 if ( be->be_index_change( be,
97                         type, values, 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 }