]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2index.c
Import ldif2ldbm ldif fix from -devel. Still doesn't completely
[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 int             ldap_debug;
20 int             ldap_syslog;
21 int             ldap_syslog_level;
22 int             global_schemacheck;
23 long            num_entries_sent;
24 long            num_bytes_sent;
25 int             active_threads;
26 char            *default_referral;
27 struct objclass *global_oc;
28 time_t          currenttime;
29 pthread_t       listener_tid;
30 pthread_mutex_t num_sent_mutex;
31 pthread_mutex_t entry2str_mutex;
32 pthread_mutex_t active_threads_mutex;
33 pthread_mutex_t new_conn_mutex;
34 pthread_mutex_t currenttime_mutex;
35 pthread_mutex_t replog_mutex;
36 pthread_mutex_t ops_mutex;
37 pthread_mutex_t regex_mutex;
38
39
40 static void
41 usage( char *name )
42 {
43         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber] attr\n", name );
44         exit( 1 );
45 }
46
47 int
48 main( int argc, char **argv )
49 {
50         int             i, cargc, indb, stop;
51         char            *cargv[MAXARGS];
52         char            *defargv[MAXARGS];
53         char            *tailorfile, *inputfile;
54         char            *linep, *buf, *attr;
55         char            line[BUFSIZ];
56         int             lineno, elineno;
57         int             lmax, lcur, indexmask, syntaxmask;
58         int             dbnum;
59         unsigned long   id;
60         Backend         *be = NULL;
61         struct berval   bv;
62         struct berval   *vals[2];
63
64         inputfile = NULL;
65         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
66         dbnum = -1;
67         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
68                 switch ( i ) {
69                 case 'd':       /* turn on debugging */
70                         ldap_debug = atoi( optarg );
71                         break;
72
73                 case 'f':       /* specify a tailor file */
74                         tailorfile = strdup( optarg );
75                         break;
76
77                 case 'i':       /* input file */
78                         inputfile = strdup( optarg );
79                         break;
80
81                 case 'n':       /* which config file db to index */
82                         dbnum = atoi( optarg ) - 1;
83                         break;
84
85                 default:
86                         usage( argv[0] );
87                         break;
88                 }
89         }
90         attr = attr_normalize( argv[argc - 1] );
91         if ( inputfile == NULL ) {
92                 usage( argv[0] );
93         } else {
94                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
95                         perror( inputfile );
96                         exit( 1 );
97                 }
98         }
99
100         init();
101         read_config( tailorfile, &be, NULL );
102
103         if ( dbnum == -1 ) {
104                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
105                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
106                             == 0 ) {
107                                 break;
108                         }
109                 }
110                 if ( dbnum == nbackends ) {
111                         fprintf( stderr, "No ldbm database found in config file\n" );
112                         exit( 1 );
113                 }
114         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
115                 fprintf( stderr, "Database number selected via -n is out of range\n" );
116                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
117                 exit( 1 );
118         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
119                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
120                 exit( 1 );
121         }
122         be = &backends[dbnum];
123
124         attr_masks( be->be_private, attr, &indexmask, &syntaxmask );
125         if ( indexmask == 0 ) {
126                 exit( 0 );
127         }
128
129         id = 0;
130         stop = 0;
131         lineno = 0;
132         buf = NULL;
133         lcur = lmax = 0;
134         vals[0] = &bv;
135         vals[1] = NULL;
136         while ( ! stop ) {
137                 char            *type, *val, *s;
138                 int             vlen;
139
140                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
141                         int     len;
142
143                         lineno++;
144                         len = strlen( line );
145                         while ( lcur + len + 1 > lmax ) {
146                                 lmax += BUFSIZ;
147                                 buf = (char *) ch_realloc( buf, lmax );
148                         }
149                         strcpy( buf + lcur, line );
150                         lcur += len;
151                 } else {
152                         stop = 1;
153                 }
154                 if ( line[0] == '\n' || stop && buf && *buf ) {
155                         if ( *buf != '\n' ) {
156                                 if (isdigit(*buf)) {
157                                         id = atol(buf);
158                                 } else {
159                                         id++;
160                                 }
161                                 s = buf;
162                                 elineno = 0;
163                                 while ( (linep = str_getline( &s )) != NULL ) {
164                                         elineno++;
165                                         if ( str_parse_line( linep, &type, &val,
166                                             &vlen ) != 0 ) {
167                                                 Debug( LDAP_DEBUG_PARSE,
168                             "bad line %d in entry ending at line %d ignored\n",
169                                                     elineno, elineno, 0 );
170                                                 continue;
171                                         }
172
173                                         if ( strcasecmp( type, attr ) == 0 ) {
174                                                 bv.bv_val = val;
175                                                 bv.bv_len = vlen;
176                                                 index_add_values( be, attr,
177                                                     vals, id );
178                                         }
179                                 }
180                         }
181                         *buf = '\0';
182                         lcur = 0;
183                 }
184         }
185         (*be->be_close)( be );
186
187         exit( 0 );
188 }