]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapindex.c
Y2k copyright update
[openldap] / servers / slapd / tools / slapindex.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
24         AttributeDescription *desc;
25         char *text;
26 #else
27         char *desc;
28 #endif
29         ID id;
30         int rc = EXIT_SUCCESS;
31
32         slap_tool_init( "slapindex", SLAPINDEX, argc, argv );
33
34         if( !be->be_entry_open &&
35                 !be->be_entry_close &&
36                 !be->be_entry_first &&
37                 !be->be_entry_next &&
38                 !be->be_entry_get &&
39                 !be->be_index_attr &&
40                 !be->be_index_change )
41         {
42                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
43                         progname );
44                 exit( EXIT_FAILURE );
45         }
46
47 #ifdef SLAPD_SCHEMA_NOT_COMPAT
48         type = argv[argc - 1];
49
50         if( strcasecmp( type, "dn" ) == 0 ) {
51                 desc = NULL;
52
53         } else {
54                 rc = slap_str2ad( type, &desc, &text );
55
56                 if( rc != LDAP_SUCCESS ) {
57                         fprintf( stderr, "%s: unrecognized attribute type: %s\n",
58                                 progname, text );
59                         exit( EXIT_FAILURE );
60                 }
61         }
62 #else
63         desc = type = attr_normalize( argv[argc - 1] );
64 #endif
65
66         if ( !be->be_index_attr( be, desc ) ) {
67                 fprintf( stderr, "attribute type \"%s\": no indices to generate\n",
68                         type );
69                 exit( EXIT_FAILURE );
70         }
71
72         if( be->be_entry_open( be, 0 ) != 0 ) {
73                 fprintf( stderr, "%s: could not open database.\n",
74                         progname );
75                 exit( EXIT_FAILURE );
76         }
77
78         for ( id = be->be_entry_first( be );
79                 id != NOID;
80                 id = be->be_entry_next( be ) )
81         {
82                 struct berval **values;
83                 Entry* e = be->be_entry_get( be, id );
84                 struct berval bv;
85                 struct berval *bvals[2];
86
87                 if ( e == NULL ) {
88                         fprintf( stderr,
89                                 "entry id=%08lx: no data\n", (long) id );
90                         rc = EXIT_FAILURE;
91                         if( continuemode ) continue;
92                         break;
93                 }
94
95                 if( verbose ) {
96                         printf("indexing id=%08lx dn=\"%s\"\n",
97                                 id, e->e_dn );
98                 }
99
100 #ifdef SLAPD_SCHEMA_NOT_COMPAT
101                 if( desc == NULL )
102 #else
103                 if( strcasecmp( type, "dn" ) == 0 )
104 #endif
105                 {
106                         bv.bv_val = e->e_ndn;
107                         bv.bv_len = strlen( bv.bv_val );
108                         bvals[0] = &bv;
109                         bvals[1] = NULL;
110
111                         values = bvals;
112
113                         if ( be->be_index_change( be,
114                                 desc, values, id, SLAP_INDEX_ADD_OP ) )
115                         {
116                                 rc = EXIT_FAILURE;
117
118                                 if( !continuemode ) {
119                                         entry_free( e );
120                                         break;
121                                 }
122                         }
123
124                 } else {
125                         Attribute *attr;
126                         
127 #ifdef SLAPD_SCHEMA_NOT_COMPAT
128                         for( attr = attrs_find( e->e_attrs, desc );
129                                 attr != NULL;
130                                 attr = attrs_find( attr, desc ) )
131 #else
132                         if (( attr = attr_find( e->e_attrs, type )) != NULL )
133 #endif
134                         {
135
136                                 if ( be->be_index_change( be,
137                                         desc, attr->a_vals, id, SLAP_INDEX_ADD_OP ) )
138                                 {
139                                         rc = EXIT_FAILURE;
140
141                                         if( !continuemode ) {
142                                                 entry_free( e );
143                                                 goto done;
144                                         }
145                                 }
146                         }
147                 }
148
149                 entry_free( e );
150         }
151
152 done:
153         (void) be->be_entry_close( be );
154
155         slap_tool_destroy();
156
157         return( rc );
158 }