]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2entry-bdb2.c
Integration of the BDB2 backend into the new init/startup/shutdown schema.
[openldap] / servers / slapd / tools / ldif2id2entry-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 "ldapconfig.h"
12 #include "../slap.h"
13 #include "../back-bdb2/back-bdb2.h"
14
15 #define MAXARGS                 100
16
17 static char     *tailorfile;
18 static char     *inputfile;
19  
20 static void
21 usage( char *name )
22 {
23         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
24         exit( 1 );
25 }
26
27 int
28 main( int argc, char **argv )
29 {
30         int             i, cargc, indb, stop, status;
31         char            *cargv[MAXARGS];
32         char            *defargv[MAXARGS];
33         char            *linep, *buf;
34         char            line[BUFSIZ], idbuf[BUFSIZ];
35         int             lmax, lcur;
36         int             dbnum;
37         ID              id;
38         struct dbcache  *db;
39         Backend         *be = NULL;
40         struct ldbminfo *li;
41         struct berval   bv;
42         struct berval   *vals[2];
43         Avlnode         *avltypes = NULL;
44         FILE            *fp;
45
46         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
47         dbnum = -1;
48         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
49                 switch ( i ) {
50                 case 'd':       /* turn on debugging */
51                         ldap_debug = atoi( optarg );
52                         break;
53
54                 case 'f':       /* specify a tailor file */
55                         tailorfile = strdup( optarg );
56                         break;
57
58                 case 'i':       /* input file */
59                         inputfile = strdup( optarg );
60                         break;
61
62                 case 'n':       /* which config file db to index */
63                         dbnum = atoi( optarg ) - 1;
64                         break;
65
66                 default:
67                         usage( argv[0] );
68                         break;
69                 }
70         }
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         /*
81          * initialize stuff and figure out which backend we're dealing with
82          */
83
84         slap_init(SLAP_TOOL_MODE, "ldif2id2entry");
85         read_config( tailorfile );
86
87         if ( dbnum == -1 ) {
88                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
89                         if ( strcasecmp( backends[dbnum].be_type, "bdb2" )
90                             == 0 ) {
91                                 break;
92                         }
93                 }
94                 if ( dbnum == nbackends ) {
95                         fprintf( stderr, "No bdb2 database found in config file\n" );
96                         exit( 1 );
97                 }
98         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
99                 fprintf( stderr, "Database number selected via -n is out of range\n" );
100                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
101                 exit( 1 );
102         } else if ( strcasecmp( backends[dbnum].be_type, "bdb2" ) != 0 ) {
103                 fprintf( stderr, "Database number %d selected via -n is not an bdb2 database\n", dbnum );
104                 exit( 1 );
105         }
106
107         slap_startup(dbnum);
108
109         be = &backends[dbnum];
110
111         /* disable write sync'ing */
112         li = (struct ldbminfo *) be->be_private;
113         li->li_dbcachewsync = 0;
114
115         if ( (db = bdb2i_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_NEWDB ))
116             == NULL ) {
117                 perror( "id2entry file" );
118                 exit( 1 );
119         }
120
121         id = 0;
122         stop = 0;
123         buf = NULL;
124         lcur = lmax = 0;
125         vals[0] = &bv;
126         vals[1] = NULL;
127         while ( ! stop ) {
128                 char            *type, *val, *s;
129                 int             vlen;
130                 Datum           key, data;
131
132                 ldbm_datum_init( key );
133                 ldbm_datum_init( data );
134
135                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
136                         int     len, idlen;
137
138                         len = strlen( line );
139                         if ( buf == NULL || *buf == '\0' ) {
140                                 if (!isdigit(line[0])) {
141                                         sprintf( idbuf, "%d\n", id + 1 );
142                                         idlen = strlen( idbuf );
143                                 } else {
144                                         id = atol(line) - 1;
145                                         idlen = 0;
146                                 }
147                         } else {
148                                 idlen = 0;
149                         }
150
151                         while ( lcur + len + idlen + 1 > lmax ) {
152                                 lmax += BUFSIZ;
153                                 buf = (char *) ch_realloc( buf, lmax );
154                         }
155
156                         if ( idlen > 0 ) {
157                                 strcpy( buf + lcur, idbuf );
158                                 lcur += idlen;
159                         }
160                         strcpy( buf + lcur, line );
161                         lcur += len;
162                 } else {
163                         stop = 1;
164                 }
165                 if ( line[0] == '\n' || stop && buf && *buf ) {
166                         if ( *buf != '\n' ) {
167                                 int len;
168
169                                 id++;
170                                 key.dptr = (char *) &id;
171                                 key.dsize = sizeof(ID);
172                                 data.dptr = buf;
173                                 len = strlen(buf);
174                                 if (buf[len - 1] == '\n')
175                                         buf[--len] = '\0';
176                                 data.dsize = len + 1;
177                                 if ( ldbm_store( db->dbc_db, key, data,
178                                     LDBM_INSERT ) != 0 ) {
179                                         fputs("id2entry ldbm_store failed\n",
180                                               stderr);
181                                         exit( 1 );
182                                 }
183                         }
184                         *buf = '\0';
185                         lcur = 0;
186                         line[0] = '\0';
187                 }
188         }
189
190         slap_shutdown(dbnum);
191
192         id++;
193         sprintf( line, "%s/NEXTID",
194             ((struct ldbminfo *) be->be_private)->li_directory );
195         if ( (fp = fopen( line, "w" )) == NULL ) {
196                 perror( line );
197                 fprintf( stderr, "Could not write next id %ld\n", id );
198         } else {
199                 fprintf( fp, "%ld\n", id );
200                 fclose( fp );
201         }
202
203         slap_destroy();
204
205         exit( 0 );
206 }