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