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