]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2ldbm.c
Import ldif2ldbm 'no write sync' change...
[openldap] / servers / slapd / tools / ldif2ldbm.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/string.h>
6 #include <ac/ctype.h>
7 #include <ac/socket.h>
8 #include <ac/unistd.h>
9 #include <ac/wait.h>
10
11 #include <sys/param.h>
12
13 #include "ldapconfig.h"
14 #include "../slap.h"
15 #include "../back-ldbm/back-ldbm.h"
16 #include "ldif.h"
17
18 #define INDEXCMD                "ldif2index"
19 #define ID2ENTRYCMD             "ldif2id2entry"
20 #define ID2CHILDRENCMD          "ldif2id2children"
21 #define MAXARGS                 100
22
23 int             ldap_debug;
24 int             ldap_syslog;
25 int             ldap_syslog_level;
26 int             global_schemacheck;
27 long            num_entries_sent;
28 long            num_bytes_sent;
29 int             active_threads;
30 char            *default_referral;
31 struct objclass *global_oc;
32 time_t          currenttime;
33 pthread_t       listener_tid;
34 pthread_mutex_t num_sent_mutex;
35 pthread_mutex_t entry2str_mutex;
36 pthread_mutex_t active_threads_mutex;
37 pthread_mutex_t new_conn_mutex;
38 pthread_mutex_t currenttime_mutex;
39 pthread_mutex_t replog_mutex;
40 pthread_mutex_t ops_mutex;
41 pthread_mutex_t regex_mutex;
42
43 static void fork_child( char *prog, char *args[] );
44 static void     wait4kids( int nkidval );
45
46 static char     *indexcmd;
47 static char     *tailorfile;
48 static char     *inputfile;
49 static int      maxkids = 1;
50 static int      nkids;
51
52 static void
53 usage( char *name )
54 {
55         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
56         exit( 1 );
57 }
58
59 int
60 main( int argc, char **argv )
61 {
62         int             i, stop, status;
63         char            *linep, *buf, *sbindir;
64         char            *args[10];
65         char            buf2[20], buf3[20];
66         char            line[BUFSIZ];
67         char            cmd[MAXPATHLEN];
68         int             lineno, elineno;
69         int             lmax, lcur;
70         int             dbnum;
71         ID              id;
72         Backend         *be = NULL;
73         struct ldbminfo *li;
74         struct berval   bv;
75         struct berval   *vals[2];
76         Avlnode         *avltypes = NULL;
77
78         sbindir = DEFAULT_SBINDIR;
79         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
80         dbnum = -1;
81         while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
82                 switch ( i ) {
83                 case 'd':       /* turn on debugging */
84                         ldap_debug = atoi( optarg );
85                         break;
86
87                 case 's':       /* alternate sbindir (index cmd location) */
88                 case 'e':       /* accept -e for backwards compatibility */
89                         sbindir = strdup( optarg );
90                         break;
91
92                 case 'f':       /* specify a tailor file */
93                         tailorfile = strdup( optarg );
94                         break;
95
96                 case 'i':       /* input file */
97                         inputfile = strdup( optarg );
98                         break;
99
100                 case 'j':       /* number of parallel index procs */
101                         maxkids = atoi( optarg );
102                         break;
103
104                 case 'n':       /* which config file db to index */
105                         dbnum = atoi( optarg ) - 1;
106                         break;
107
108                 default:
109                         usage( argv[0] );
110                         break;
111                 }
112         }
113         if ( inputfile == NULL ) {
114                 usage( argv[0] );
115         } else {
116                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
117                         perror( inputfile );
118                         exit( 1 );
119                 }
120         }
121
122         /*
123          * initialize stuff and figure out which backend we're dealing with
124          */
125
126         init();
127         read_config( tailorfile, &be, NULL );
128
129         if ( dbnum == -1 ) {
130                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
131                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
132                             == 0 ) {
133                                 break;
134                         }
135                 }
136                 if ( dbnum == nbackends ) {
137                         fprintf( stderr, "No ldbm database found in config file\n" );
138                         exit( 1 );
139                 }
140         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
141                 fprintf( stderr, "Database number selected via -n is out of range\n" );
142                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
143                 exit( 1 );
144         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
145                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
146                 exit( 1 );
147         }
148         be = &backends[dbnum];
149
150         /* disable write sync'ing */
151         li = (struct ldbminfo *) be->be_private;
152         li->li_dbcachewsync = 0;
153
154         /*
155          * generate the id2entry index
156          */
157
158         i = 0;
159         sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
160         args[i++] = cmd;
161         args[i++] = "-i";
162         args[i++] = inputfile;
163         args[i++] = "-f";
164         args[i++] = tailorfile;
165         args[i++] = "-n";
166         sprintf( buf2, "%d", dbnum+1 );
167         args[i++] = buf2;
168         if ( ldap_debug ) {
169                 sprintf( buf3, "%d", ldap_debug );
170                 args[i++] = "-d";
171                 args[i++] = buf3;
172         }
173         args[i++] = NULL;
174         fork_child( cmd, args );
175
176         /*
177          * generate the dn2id and id2children indexes
178          */
179
180         i = 0;
181         sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
182         args[i++] = cmd;
183         args[i++] = "-i";
184         args[i++] = inputfile;
185         args[i++] = "-f";
186         args[i++] = tailorfile;
187         args[i++] = "-n";
188         sprintf( buf2, "%d", dbnum+1 );
189         args[i++] = buf2;
190         if ( ldap_debug ) {
191                 sprintf( buf3, "%d", ldap_debug );
192                 args[i++] = "-d";
193                 args[i++] = buf3;
194         }
195         args[i++] = NULL;
196         fork_child( cmd, args );
197
198         /*
199          * generate the attribute indexes
200          */
201
202         i = 0;
203         sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
204         args[i++] = cmd;
205         args[i++] = "-i";
206         args[i++] = inputfile;
207         args[i++] = "-f";
208         args[i++] = tailorfile;
209         args[i++] = "-n";
210         sprintf( buf2, "%d", dbnum+1 );
211         args[i++] = buf2;
212         if ( ldap_debug ) {
213                 sprintf( buf3, "%d", ldap_debug );
214                 args[i++] = "-d";
215                 args[i++] = buf3;
216         }
217         args[i++] = NULL;               /* will hold the attribute name */
218         args[i++] = NULL;
219
220         id = 0;
221         stop = 0;
222         buf = NULL;
223         lineno = 0;
224         lcur = lmax = 0;
225         vals[0] = &bv;
226         vals[1] = NULL;
227         while ( ! stop ) {
228                 char            *type, *val, *s;
229                 int             vlen, indexmask, syntaxmask;
230                 Datum           key, data;
231
232                 memset( &key, 0, sizeof( key ));
233                 memset( &data, 0, sizeof( data ));
234
235                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
236                         int     len;
237
238                         lineno++;
239                         len = strlen( line );
240                         while ( lcur + len + 1 > lmax ) {
241                                 lmax += BUFSIZ;
242                                 buf = (char *) ch_realloc( buf, lmax );
243                         }
244                         strcpy( buf + lcur, line );
245                         lcur += len;
246                 } else {
247                         stop = 1;
248                 }
249                 if ( line[0] == '\n' || stop && buf && *buf ) {
250                         id++;
251                         s = buf;
252                         elineno = 0;
253                         while ( (linep = str_getline( &s )) != NULL ) {
254                                 elineno++;
255                                 if ( str_parse_line( linep, &type, &val, &vlen )
256                                     != 0 ) {
257                                         Debug( LDAP_DEBUG_PARSE,
258                             "bad line %d in entry ending at line %d ignored\n",
259                                             elineno, lineno, 0 );
260                                         continue;
261                                 }
262
263                                 if ( !isascii( *type ) || isdigit( *type ) )
264                                         continue;
265
266                                 type = strdup( type );
267                                 if ( avl_insert( &avltypes, type, strcasecmp,
268                                     avl_dup_error ) != 0 ) {
269                                         free( type );
270                                 } else {
271                                         attr_masks( be->be_private, type,
272                                             &indexmask, &syntaxmask );
273                                         if ( indexmask ) {
274                                                 args[i - 2] = type;
275                                                 fork_child( cmd, args );
276                                         }
277                                 }
278                         }
279                         *buf = '\0';
280                         lcur = 0;
281                 }
282         }
283         (*be->be_close)( be );
284
285         wait4kids( -1 );
286
287         exit( 0 );
288 }
289
290 static void
291 fork_child( char *prog, char *args[] )
292 {
293         int     status, pid;
294
295         wait4kids( maxkids );
296
297         switch ( pid = fork() ) {
298         case 0:         /* child */
299                 execvp( prog, args );
300                 fprintf( stderr, "%s: ", prog );
301                 perror( "execv" );
302                 exit( -1 );
303                 break;
304
305         case -1:        /* trouble */
306                 fprintf( stderr, "Could not fork to run %s\n", prog );
307                 perror( "fork" );
308                 break;
309
310         default:        /* parent */
311                 nkids++;
312                 break;
313         }
314 }
315
316 static void
317 wait4kids( int nkidval )
318 {
319         int             status;
320         unsigned char   *p;
321
322         while ( nkids >= nkidval ) {
323                 wait( &status );
324                 p = (unsigned char *) &status;
325                 if ( p[sizeof(int) - 1] == 0177 ) {
326                         fprintf( stderr,
327                             "stopping: child stopped with signal %d\n",
328                             p[sizeof(int) - 2] );
329                 } else if ( p[sizeof(int) - 1] != 0 ) {
330                         fprintf( stderr, 
331                             "stopping: child terminated with signal %d\n",
332                             p[sizeof(int) - 1] );
333                         exit( p[sizeof(int) - 1] );
334                 } else if ( p[sizeof(int) - 2] != 0 ) {
335                         fprintf( stderr, 
336                             "stopping: child exited with status %d\n",
337                             p[sizeof(int) - 2] );
338                         exit( p[sizeof(int) - 2] );
339                 } else {
340                         nkids--;
341                 }
342         }
343 }