10 #include <ac/unistd.h>
12 #include "ldap_defaults.h"
14 #include "../back-ldbm/back-ldbm.h"
18 static char *tailorfile;
19 static char *inputfile;
24 fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
29 main( int argc, char **argv )
33 char line[BUFSIZ], idbuf[BUFSIZ];
41 struct berval *vals[2];
44 ldbm_ignore_nextid_file = 1;
46 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
48 while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
50 case 'd': /* turn on debugging */
51 ldap_debug = atoi( optarg );
54 case 'f': /* specify a tailor file */
55 tailorfile = strdup( optarg );
58 case 'i': /* input file */
59 inputfile = strdup( optarg );
62 case 'n': /* which config file db to index */
63 dbnum = atoi( optarg ) - 1;
71 if ( inputfile == NULL ) {
74 if ( freopen( inputfile, "r", stdin ) == NULL ) {
81 * initialize stuff and figure out which backend we're dealing with
84 slap_init(SLAP_TOOL_MODE, "ldif2id2entry");
85 read_config( tailorfile );
88 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
89 if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
94 if ( dbnum == nbackends ) {
95 fprintf( stderr, "No ldbm database found in config file\n" );
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 );
102 } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
103 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
109 be = &backends[dbnum];
111 /* disable write sync'ing */
112 li = (struct ldbminfo *) be->be_private;
113 li->li_dbcachewsync = 0;
115 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_NEWDB ))
117 perror( "id2entry file" );
130 ldbm_datum_init( key );
131 ldbm_datum_init( data );
133 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
136 len = strlen( line );
137 if ( buf == NULL || *buf == '\0' ) {
138 if (!isdigit((unsigned char) line[0])) {
139 sprintf( idbuf, "%ld\n", id + 1 );
140 idlen = strlen( idbuf );
149 while ( lcur + len + idlen + 1 > lmax ) {
151 buf = (char *) ch_realloc( buf, lmax );
155 strcpy( buf + lcur, idbuf );
158 strcpy( buf + lcur, line );
163 if ( line[0] == '\n' || stop && buf && *buf ) {
164 if ( *buf != '\n' ) {
168 key.dptr = (char *) &id;
169 key.dsize = sizeof(ID);
172 if (buf[len - 1] == '\n')
174 data.dsize = len + 1;
175 if ( ldbm_store( db->dbc_db, key, data,
176 LDBM_INSERT ) != 0 ) {
177 fputs("id2entry ldbm_store failed\n",
189 ldbm_cache_close( be, db );
192 slap_shutdown(dbnum);
195 sprintf( line, "%s/NEXTID",
196 ((struct ldbminfo *) be->be_private)->li_directory );
197 if ( (fp = fopen( line, "w" )) == NULL ) {
199 fprintf( stderr, "Could not write next id %ld\n", id );
201 fprintf( fp, "%ld\n", id );