]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2entry.c
Import ldif2ldbm ldif fix from -devel. Still doesn't completely
[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                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
145                         int     len, idlen;
146
147                         len = strlen( line );
148                         if ( buf == NULL || *buf == '\0' ) {
149                                 if (!isdigit(line[0])) {
150                                         sprintf( idbuf, "%d\n", id + 1 );
151                                         idlen = strlen( idbuf );
152                                 } else {
153                                         id = atol(line) - 1;
154                                         idlen = 0;
155                                 }
156                         } else {
157                                 idlen = 0;
158                         }
159
160                         while ( lcur + len + idlen + 1 > lmax ) {
161                                 lmax += BUFSIZ;
162                                 buf = (char *) ch_realloc( buf, lmax );
163                         }
164
165                         if ( idlen > 0 ) {
166                                 strcpy( buf + lcur, idbuf );
167                                 lcur += idlen;
168                         }
169                         strcpy( buf + lcur, line );
170                         lcur += len;
171                 } else {
172                         stop = 1;
173                 }
174                 if ( line[0] == '\n' || stop && buf && *buf ) {
175                         if ( *buf != '\n' ) {
176                                 int len;
177
178                                 id++;
179                                 key.dptr = (char *) &id;
180                                 key.dsize = sizeof(ID);
181                                 data.dptr = buf;
182                                 len = strlen(buf);
183                                 if (buf[len - 1] == '\n')
184                                         buf[--len] = '\0';
185                                 data.dsize = len + 1;
186                                 if ( ldbm_store( db->dbc_db, key, data,
187                                     LDBM_INSERT ) != 0 ) {
188                                         fputs("id2entry ldbm_store failed\n",
189                                               stderr);
190                                         exit( 1 );
191                                 }
192                         }
193                         *buf = '\0';
194                         lcur = 0;
195                         line[0] = '\0';
196                 }
197         }
198         (*be->be_close)( be );
199
200         id++;
201         sprintf( line, "%s/NEXTID",
202             ((struct ldbminfo *) be->be_private)->li_directory );
203         if ( (fp = fopen( line, "w" )) == NULL ) {
204                 perror( line );
205                 fprintf( stderr, "Could not write next id %ld\n", id );
206         } else {
207                 fprintf( fp, "%ld\n", id );
208                 fclose( fp );
209         }
210
211         exit( 0 );
212 }