]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2index-bdb2.c
s/exit(1)/exit(EXIT_FAILURE)/
[openldap] / servers / slapd / tools / ldif2index-bdb2.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/stdlib.h>
6
7 #include <ac/ctype.h>
8 #include <ac/string.h>
9 #include <ac/socket.h>
10 #include <ac/unistd.h>
11
12 #include "../slap.h"
13 #include "../back-bdb2/back-bdb2.h"
14
15 #include "ldap_defaults.h"
16 #include "ldif.h"
17
18 #define MAXARGS                 100
19
20 static void
21 usage( char *name )
22 {
23         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber] attr\n", name );
24         exit( EXIT_FAILURE );
25 }
26
27 int
28 main( int argc, char **argv )
29 {
30         int             i, stop;
31         char            *tailorfile, *inputfile;
32         char            *linep, *buf, *attr;
33         char            line[BUFSIZ];
34         int             lineno, elineno;
35         int             lmax, lcur, indexmask, syntaxmask;
36         int             dbnum;
37         unsigned long   id;
38         Backend         *be = NULL;
39         struct ldbminfo *li;
40         struct berval   bv;
41         struct berval   *vals[2];
42
43         inputfile = NULL;
44         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
45         dbnum = -1;
46         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
47                 switch ( i ) {
48                 case 'd':       /* turn on debugging */
49                         ldap_debug = atoi( optarg );
50                         break;
51
52                 case 'f':       /* specify a tailor file */
53                         tailorfile = strdup( optarg );
54                         break;
55
56                 case 'i':       /* input file */
57                         inputfile = strdup( optarg );
58                         break;
59
60                 case 'n':       /* which config file db to index */
61                         dbnum = atoi( optarg ) - 1;
62                         break;
63
64                 default:
65                         usage( argv[0] );
66                         break;
67                 }
68         }
69         attr = attr_normalize( argv[argc - 1] );
70         if ( inputfile == NULL ) {
71                 usage( argv[0] );
72         } else {
73                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
74                         perror( inputfile );
75                         exit( EXIT_FAILURE );
76                 }
77         }
78
79         slap_init(SLAP_TOOL_MODE, ch_strdup(argv[0]));
80         read_config( tailorfile );
81
82         if ( dbnum == -1 ) {
83                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
84                         if ( strcasecmp( backends[dbnum].be_type, "bdb2" )
85                             == 0 ) {
86                                 break;
87                         }
88                 }
89                 if ( dbnum == nbackends ) {
90                         fprintf( stderr, "No bdb2 database found in config file\n" );
91                         exit( EXIT_FAILURE );
92                 }
93         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
94                 fprintf( stderr, "Database number selected via -n is out of range\n" );
95                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
96                 exit( EXIT_FAILURE );
97         } else if ( strcasecmp( backends[dbnum].be_type, "bdb2" ) != 0 ) {
98                 fprintf( stderr, "Database number %d selected via -n is not an bdb2 database\n", dbnum );
99                 exit( EXIT_FAILURE );
100         }
101
102         slap_startup(dbnum);
103
104         be = &backends[dbnum];
105
106         /* disable write sync'ing */
107         li = (struct ldbminfo *) be->be_private;
108         li->li_dbcachewsync = 0;
109
110         bdb2i_attr_masks( be->be_private, attr, &indexmask, &syntaxmask );
111         if ( indexmask == 0 ) {
112                 exit( EXIT_SUCCESS );
113         }
114
115         id = 0;
116         stop = 0;
117         lineno = 0;
118         buf = NULL;
119         lcur = lmax = 0;
120         vals[0] = &bv;
121         vals[1] = NULL;
122         while ( ! stop ) {
123                 char            *type, *val, *s;
124                 ber_len_t       vlen;
125
126                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
127                         int     len;
128
129                         lineno++;
130                         len = strlen( line );
131                         while ( lcur + len + 1 > lmax ) {
132                                 lmax += BUFSIZ;
133                                 buf = (char *) ch_realloc( buf, lmax );
134                         }
135                         strcpy( buf + lcur, line );
136                         lcur += len;
137                 } else {
138                         stop = 1;
139                 }
140                 if ( line[0] == '\n' || stop && buf && *buf ) {
141                         if ( *buf != '\n' ) {
142                                 if (isdigit((unsigned char) *buf)) {
143                                         id = atol(buf);
144                                 } else {
145                                         id++;
146                                 }
147                                 s = buf;
148                                 elineno = 0;
149                                 while ( (linep = ldif_getline( &s )) != NULL ) {
150                                         elineno++;
151                                         if ( ldif_parse_line( linep, &type, &val,
152                                             &vlen ) != 0 ) {
153                                                 Debug( LDAP_DEBUG_PARSE,
154                             "bad line %d in entry ending at line %d ignored\n",
155                                                     elineno, elineno, 0 );
156                                                 continue;
157                                         }
158
159                                         if ( strcasecmp( type, attr ) == 0 ) {
160                                                 bv.bv_val = val;
161                                                 bv.bv_len = vlen;
162                                                 bdb2i_index_add_values( be, attr,
163                                                     vals, id );
164                                         }
165                                 }
166                         }
167                         *buf = '\0';
168                         lcur = 0;
169                 }
170         }
171
172         slap_shutdown(dbnum);
173         slap_destroy();
174
175         exit( EXIT_SUCCESS );
176 }