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