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