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