11 #include "ldapconfig.h"
13 #include "../back-ldbm/back-ldbm.h"
17 static char *tailorfile;
18 static char *inputfile;
23 fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
28 main( int argc, char **argv )
32 char line[BUFSIZ], idbuf[BUFSIZ];
40 struct berval *vals[2];
43 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
45 while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
47 case 'd': /* turn on debugging */
48 ldap_debug = atoi( optarg );
51 case 'f': /* specify a tailor file */
52 tailorfile = strdup( optarg );
55 case 'i': /* input file */
56 inputfile = strdup( optarg );
59 case 'n': /* which config file db to index */
60 dbnum = atoi( optarg ) - 1;
68 if ( inputfile == NULL ) {
71 if ( freopen( inputfile, "r", stdin ) == NULL ) {
78 * initialize stuff and figure out which backend we're dealing with
81 slap_init(SLAP_TOOL_MODE, "ldif2id2entry");
82 read_config( tailorfile );
85 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
86 if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
91 if ( dbnum == nbackends ) {
92 fprintf( stderr, "No ldbm database found in config file\n" );
95 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
96 fprintf( stderr, "Database number selected via -n is out of range\n" );
97 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
99 } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
100 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
106 be = &backends[dbnum];
108 /* disable write sync'ing */
109 li = (struct ldbminfo *) be->be_private;
110 li->li_dbcachewsync = 0;
112 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_NEWDB ))
114 perror( "id2entry file" );
127 ldbm_datum_init( key );
128 ldbm_datum_init( data );
130 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
133 len = strlen( line );
134 if ( buf == NULL || *buf == '\0' ) {
135 if (!isdigit((unsigned char) line[0])) {
136 sprintf( idbuf, "%ld\n", id + 1 );
137 idlen = strlen( idbuf );
146 while ( lcur + len + idlen + 1 > lmax ) {
148 buf = (char *) ch_realloc( buf, lmax );
152 strcpy( buf + lcur, idbuf );
155 strcpy( buf + lcur, line );
160 if ( line[0] == '\n' || stop && buf && *buf ) {
161 if ( *buf != '\n' ) {
165 key.dptr = (char *) &id;
166 key.dsize = sizeof(ID);
169 if (buf[len - 1] == '\n')
171 data.dsize = len + 1;
172 if ( ldbm_store( db->dbc_db, key, data,
173 LDBM_INSERT ) != 0 ) {
174 fputs("id2entry ldbm_store failed\n",
185 slap_shutdown(dbnum);
188 sprintf( line, "%s/NEXTID",
189 ((struct ldbminfo *) be->be_private)->li_directory );
190 if ( (fp = fopen( line, "w" )) == NULL ) {
192 fprintf( stderr, "Could not write next id %ld\n", id );
194 fprintf( fp, "%ld\n", id );