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 )
30 int i, cargc, indb, stop, status;
32 char *defargv[MAXARGS];
34 char line[BUFSIZ], idbuf[BUFSIZ];
42 struct berval *vals[2];
43 Avlnode *avltypes = NULL;
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
85 read_config( tailorfile, &be, NULL );
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 );
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" );
125 char *type, *val, *s;
129 ldbm_datum_init( key );
130 ldbm_datum_init( data );
132 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
135 len = strlen( line );
136 if ( buf == NULL || *buf == '\0' ) {
137 if (!isdigit(line[0])) {
138 sprintf( idbuf, "%d\n", id + 1 );
139 idlen = strlen( idbuf );
148 while ( lcur + len + idlen + 1 > lmax ) {
150 buf = (char *) ch_realloc( buf, lmax );
154 strcpy( buf + lcur, idbuf );
157 strcpy( buf + lcur, line );
162 if ( line[0] == '\n' || stop && buf && *buf ) {
163 if ( *buf != '\n' ) {
167 key.dptr = (char *) &id;
168 key.dsize = sizeof(ID);
171 if (buf[len - 1] == '\n')
173 data.dsize = len + 1;
174 if ( ldbm_store( db->dbc_db, key, data,
175 LDBM_INSERT ) != 0 ) {
176 fputs("id2entry ldbm_store failed\n",
186 (*be->be_close)( be );
189 sprintf( line, "%s/NEXTID",
190 ((struct ldbminfo *) be->be_private)->li_directory );
191 if ( (fp = fopen( line, "w" )) == NULL ) {
193 fprintf( stderr, "Could not write next id %ld\n", id );
195 fprintf( fp, "%ld\n", id );