]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2index.c
ITS#141: Add ldbm_ignore_nextid_file, since tools/ldif2* handles it privately
[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, stop;
30         char            *tailorfile, *inputfile;
31         char            *linep, *buf, *attr;
32         char            line[BUFSIZ];
33         int             lineno, elineno;
34         int             lmax, lcur, indexmask, syntaxmask;
35         int             dbnum;
36         unsigned long   id;
37         Backend         *be = NULL;
38         struct ldbminfo *li;
39         struct berval   bv;
40         struct berval   *vals[2];
41
42         ldbm_ignore_nextid_file = 1;
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         slap_init(SLAP_TOOL_MODE, ch_strdup(argv[0]));
81         read_config( tailorfile );
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
103         slap_startup(dbnum);
104
105         be = &backends[dbnum];
106
107         /* disable write sync'ing */
108         li = (struct ldbminfo *) be->be_private;
109         li->li_dbcachewsync = 0;
110
111         attr_masks( be->be_private, attr, &indexmask, &syntaxmask );
112         if ( indexmask == 0 ) {
113                 exit( 0 );
114         }
115
116         id = 0;
117         stop = 0;
118         lineno = 0;
119         buf = NULL;
120         lcur = lmax = 0;
121         vals[0] = &bv;
122         vals[1] = NULL;
123         while ( ! stop ) {
124                 char            *type, *val, *s;
125                 int             vlen;
126
127                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
128                         int     len;
129
130                         lineno++;
131                         len = strlen( line );
132                         while ( lcur + len + 1 > lmax ) {
133                                 lmax += BUFSIZ;
134                                 buf = (char *) ch_realloc( buf, lmax );
135                         }
136                         strcpy( buf + lcur, line );
137                         lcur += len;
138                 } else {
139                         stop = 1;
140                 }
141                 if ( line[0] == '\n' || stop && buf && *buf ) {
142                         if ( *buf != '\n' ) {
143                                 if (isdigit((unsigned char) *buf)) {
144                                         id = atol(buf);
145                                 } else {
146                                         id++;
147                                 }
148                                 s = buf;
149                                 elineno = 0;
150                                 while ( (linep = ldif_getline( &s )) != NULL ) {
151                                         elineno++;
152                                         if ( ldif_parse_line( linep, &type, &val,
153                                             &vlen ) != 0 ) {
154                                                 Debug( LDAP_DEBUG_PARSE,
155                             "bad line %d in entry ending at line %d ignored\n",
156                                                     elineno, elineno, 0 );
157                                                 continue;
158                                         }
159
160                                         if ( strcasecmp( type, attr ) == 0 ) {
161                                                 bv.bv_val = val;
162                                                 bv.bv_len = vlen;
163                                                 index_add_values( be, attr,
164                                                     vals, id );
165                                         }
166                                 }
167                         }
168                         *buf = '\0';
169                         lcur = 0;
170                 }
171         }
172
173         slap_shutdown(dbnum);
174         slap_destroy();
175
176         return( 0 );
177 }