]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2index-bdb2.c
Rework ac/socket.h for HAVE_WINSOCK:
[openldap] / servers / slapd / tools / ldif2index-bdb2.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-bdb2/back-bdb2.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         inputfile = NULL;
43         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
44         dbnum = -1;
45         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
46                 switch ( i ) {
47                 case 'd':       /* turn on debugging */
48                         ldap_debug = atoi( optarg );
49                         break;
50
51                 case 'f':       /* specify a tailor file */
52                         tailorfile = strdup( optarg );
53                         break;
54
55                 case 'i':       /* input file */
56                         inputfile = strdup( optarg );
57                         break;
58
59                 case 'n':       /* which config file db to index */
60                         dbnum = atoi( optarg ) - 1;
61                         break;
62
63                 default:
64                         usage( argv[0] );
65                         break;
66                 }
67         }
68         attr = attr_normalize( argv[argc - 1] );
69         if ( inputfile == NULL ) {
70                 usage( argv[0] );
71         } else {
72                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
73                         perror( inputfile );
74                         exit( 1 );
75                 }
76         }
77
78         slap_init(SLAP_TOOL_MODE, ch_strdup(argv[0]));
79         read_config( tailorfile );
80
81         if ( dbnum == -1 ) {
82                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
83                         if ( strcasecmp( backends[dbnum].be_type, "bdb2" )
84                             == 0 ) {
85                                 break;
86                         }
87                 }
88                 if ( dbnum == nbackends ) {
89                         fprintf( stderr, "No bdb2 database found in config file\n" );
90                         exit( 1 );
91                 }
92         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
93                 fprintf( stderr, "Database number selected via -n is out of range\n" );
94                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
95                 exit( 1 );
96         } else if ( strcasecmp( backends[dbnum].be_type, "bdb2" ) != 0 ) {
97                 fprintf( stderr, "Database number %d selected via -n is not an bdb2 database\n", dbnum );
98                 exit( 1 );
99         }
100
101         slap_startup(dbnum);
102
103         be = &backends[dbnum];
104
105         /* disable write sync'ing */
106         li = (struct ldbminfo *) be->be_private;
107         li->li_dbcachewsync = 0;
108
109         bdb2i_attr_masks( be->be_private, attr, &indexmask, &syntaxmask );
110         if ( indexmask == 0 ) {
111                 exit( 0 );
112         }
113
114         id = 0;
115         stop = 0;
116         lineno = 0;
117         buf = NULL;
118         lcur = lmax = 0;
119         vals[0] = &bv;
120         vals[1] = NULL;
121         while ( ! stop ) {
122                 char            *type, *val, *s;
123                 int             vlen;
124
125                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
126                         int     len;
127
128                         lineno++;
129                         len = strlen( line );
130                         while ( lcur + len + 1 > lmax ) {
131                                 lmax += BUFSIZ;
132                                 buf = (char *) ch_realloc( buf, lmax );
133                         }
134                         strcpy( buf + lcur, line );
135                         lcur += len;
136                 } else {
137                         stop = 1;
138                 }
139                 if ( line[0] == '\n' || stop && buf && *buf ) {
140                         if ( *buf != '\n' ) {
141                                 if (isdigit((unsigned char) *buf)) {
142                                         id = atol(buf);
143                                 } else {
144                                         id++;
145                                 }
146                                 s = buf;
147                                 elineno = 0;
148                                 while ( (linep = ldif_getline( &s )) != NULL ) {
149                                         elineno++;
150                                         if ( ldif_parse_line( linep, &type, &val,
151                                             &vlen ) != 0 ) {
152                                                 Debug( LDAP_DEBUG_PARSE,
153                             "bad line %d in entry ending at line %d ignored\n",
154                                                     elineno, elineno, 0 );
155                                                 continue;
156                                         }
157
158                                         if ( strcasecmp( type, attr ) == 0 ) {
159                                                 bv.bv_val = val;
160                                                 bv.bv_len = vlen;
161                                                 bdb2i_index_add_values( be, attr,
162                                                     vals, id );
163                                         }
164                                 }
165                         }
166                         *buf = '\0';
167                         lcur = 0;
168                 }
169         }
170
171         slap_shutdown(dbnum);
172         slap_destroy();
173
174         exit( 0 );
175 }