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