]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapindex.c
New indexer/filter codes (test suite works) with cheats
[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         const 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         rc = slap_str2ad( type, &desc, &text );
51
52         if( rc != LDAP_SUCCESS ) {
53                 fprintf( stderr, "%s: unrecognized attribute type: %s\n",
54                         progname, text );
55                 exit( EXIT_FAILURE );
56         }
57 #else
58         desc = type = attr_normalize( argv[argc - 1] );
59 #endif
60
61         if ( !be->be_index_attr( be, desc ) ) {
62                 fprintf( stderr, "attribute type \"%s\": no indices to generate\n",
63                         type );
64                 exit( EXIT_FAILURE );
65         }
66
67         if( be->be_entry_open( be, 0 ) != 0 ) {
68                 fprintf( stderr, "%s: could not open database.\n",
69                         progname );
70                 exit( EXIT_FAILURE );
71         }
72
73         for ( id = be->be_entry_first( be );
74                 id != NOID;
75                 id = be->be_entry_next( be ) )
76         {
77                 Entry* e = be->be_entry_get( be, id );
78
79                 if ( e == NULL ) {
80                         fprintf( stderr,
81                                 "entry id=%08lx: no data\n", (long) id );
82                         rc = EXIT_FAILURE;
83                         if( continuemode ) continue;
84                         break;
85                 }
86
87                 if( verbose ) {
88                         printf("indexing id=%08lx dn=\"%s\"\n",
89                                 id, e->e_dn );
90                 }
91
92 #ifndef SLAPD_SCHEMA_NOT_COMPAT
93                 if( strcasecmp( type, "dn" ) == 0 )
94                 {
95                         struct berval **values;
96                         struct berval bv;
97                         struct berval *bvals[2];
98
99                         bv.bv_val = e->e_ndn;
100                         bv.bv_len = strlen( bv.bv_val );
101                         bvals[0] = &bv;
102                         bvals[1] = NULL;
103
104                         values = bvals;
105
106                         if ( be->be_index_change( be,
107                                 desc, values, id, SLAP_INDEX_ADD_OP ) )
108                         {
109                                 rc = EXIT_FAILURE;
110
111                                 if( !continuemode ) {
112                                         entry_free( e );
113                                         break;
114                                 }
115                         }
116
117                 } else
118 #endif
119                 {
120                         Attribute *attr;
121                         
122 #ifdef SLAPD_SCHEMA_NOT_COMPAT
123                         for( attr = attrs_find( e->e_attrs, desc );
124                                 attr != NULL;
125                                 attr = attrs_find( attr->a_next, desc ) )
126 #else
127                         if (( attr = attr_find( e->e_attrs, type )) != NULL )
128 #endif
129                         {
130
131                                 if ( be->be_index_change( be,
132                                         desc, attr->a_vals, id, SLAP_INDEX_ADD_OP ) )
133                                 {
134                                         rc = EXIT_FAILURE;
135
136                                         if( !continuemode ) {
137                                                 entry_free( e );
138                                                 goto done;
139                                         }
140                                 }
141                         }
142                 }
143
144                 entry_free( e );
145         }
146
147 done:
148         (void) be->be_entry_close( be );
149
150         slap_tool_destroy();
151
152         return( rc );
153 }