]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2children.c
Update projects to use ldif2common.*
[openldap] / servers / slapd / tools / ldif2id2children.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 #include "portable.h"
6
7 #include <stdio.h>
8
9 #include <ac/stdlib.h>
10
11 #include <ac/ctype.h>
12 #include <ac/string.h>
13 #include <ac/socket.h>
14 #include <ac/unistd.h>
15
16 #include "ldif2common.h"
17 #include "../back-ldbm/back-ldbm.h"
18 #include "ldif.h"
19
20 int
21 main( int argc, char **argv )
22 {
23         int             stop;
24         char            *linep, *buf;
25         char            line[BUFSIZ];
26         int             lineno, elineno;
27         int             lmax, lcur;
28         ID              id;
29         DBCache *db, *db2;
30         Backend         *be = NULL;
31         struct ldbminfo *li;
32         struct berval   bv;
33         struct berval   *vals[2];
34
35         ldbm_ignore_nextid_file = 1;
36
37         slap_ldif_init( argc, argv, LDIF2ID2CHILDREN, "ldbm", SLAP_TOOL_MODE );
38
39         slap_startup(dbnum);
40         be = &backends[dbnum];
41
42         /* disable write sync'ing */
43         li = (struct ldbminfo *) be->be_private;
44         li->li_dbcachewsync = 0;
45
46         /*
47          * first, make the dn2id index
48          */
49
50         if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_NEWDB ))
51             == NULL ) {
52                 perror( "dn2id file" );
53                 exit( EXIT_FAILURE );
54         }
55
56         id = 0;
57         stop = 0;
58         lineno = 0;
59         buf = NULL;
60         lcur = lmax = 0;
61         vals[0] = &bv;
62         vals[1] = NULL;
63         while ( ! stop ) {
64                 char            *type, *val, *s;
65                 ber_len_t               vlen;
66                 Datum           key, data;
67
68                 ldbm_datum_init( key );
69                 ldbm_datum_init( data );
70
71                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
72                         int     len;
73
74                         lineno++;
75                         len = strlen( line );
76                         while ( lcur + len + 1 > lmax ) {
77                                 lmax += BUFSIZ;
78                                 buf = (char *) ch_realloc( buf, lmax );
79                         }
80                         strcpy( buf + lcur, line );
81                         lcur += len;
82                 } else {
83                         stop = 1;
84                 }
85                 if ( line[0] == '\n' || stop && buf && *buf ) {
86                         if ( *buf != '\n' ) {
87                                 if (isdigit((unsigned char) *buf)) {
88                                         id = atol(buf);
89                                 } else {
90                                         id++;
91                                 }
92                                 s = buf;
93                                 elineno = 0;
94                                 while ( (linep = ldif_getline( &s )) != NULL ) {
95                                         elineno++;
96                                         if ( ldif_parse_line( linep, &type, &val,
97                                             &vlen ) != 0 ) {
98                                                 Debug( LDAP_DEBUG_PARSE,
99                             "bad line %d in entry ending at line %d ignored\n",
100                                                     elineno, lineno, 0 );
101                                                 continue;
102                                         }
103
104                                         if ( strcmp( type, "dn" ) == 0 )
105                                                 break;
106                                 }
107
108                                 if ( linep == NULL ) {
109                                         fprintf( stderr, "entry %ld has no dn\n",
110                                             id );
111                                 } else {
112                                         (void) dn_normalize_case( val );
113                                         key.dptr = val;
114                                         key.dsize = strlen( val ) + 1;
115                                         data.dptr = (char *) &id;
116                                         data.dsize = sizeof(ID);
117                                         if ( ldbm_store( db->dbc_db, key, data,
118                                             LDBM_REPLACE ) != 0 ) {
119                                                 perror( "dn2id ldbm_store" );
120                                                 exit( EXIT_FAILURE );
121                                         }
122                                 }
123                         }
124                         *buf = '\0';
125                         lcur = 0;
126                         line[0] = '\0';
127                 }
128         }
129         if ( buf )
130                 free( buf );
131
132         /*
133          * next, make the id2children index
134          */
135
136         if ( (db2 = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
137             LDBM_NEWDB )) == NULL ) {
138                 perror( "id2children file" );
139                 exit( EXIT_FAILURE );
140         }
141
142         rewind( stdin );
143         id = 0;
144         stop = 0;
145         buf = NULL;
146         lineno = 0;
147         lcur = lmax = 0;
148         vals[0] = &bv;
149         vals[1] = NULL;
150         while ( ! stop ) {
151                 char    *type, *val, *s, *dn;
152                 ber_len_t       vlen;
153                 ID      pid;
154                 char    buf2[20];
155                 Datum   key, data;
156
157                 ldbm_datum_init( key );
158                 ldbm_datum_init( data );
159
160                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
161                         int     len;
162
163                         len = strlen( line );
164                         while ( lcur + len + 1 > lmax ) {
165                                 lmax += BUFSIZ;
166                                 buf = (char *) ch_realloc( buf, lmax );
167                         }
168                         strcpy( buf + lcur, line );
169                         lcur += len;
170                 } else {
171                         stop = 1;
172                 }
173                 if ( line[0] == '\n' || stop && buf && *buf ) {
174                         if ( * buf != '\n' ) {
175                                 if (isdigit((unsigned char) *buf)) {
176                                         id = atol(buf);
177                                 } else {
178                                         id++;
179                                 }
180                                 s = buf;
181                                 while ( (linep = ldif_getline( &s )) != NULL ) {
182                                         if ( ldif_parse_line( linep, &type, &val,
183                                             &vlen ) != 0 ) {
184                                                 Debug( LDAP_DEBUG_PARSE,
185                                                     "bad line %d ignored\n",
186                                                     lineno, 0, 0 );
187                                                 continue;
188                                         }
189
190                                         if ( strcmp( type, "dn" ) == 0 )
191                                                 break;
192                                 }
193
194                                 if ( linep == NULL ) {
195                                         fprintf( stderr, "entry %ld has no dn\n",
196                                             id );
197                                 } else {
198                                         if ( (dn = dn_parent( be, val ))
199                                             == NULL ) {
200                                                 pid = 0;
201                                         } else {
202                                                 (void) dn_normalize_case( dn );
203                                                 key.dptr = dn;
204                                                 key.dsize = strlen( dn ) + 1;
205
206                                                 data = ldbm_fetch( db->dbc_db,
207                                                     key );
208                                                 free( dn );
209                                                 if ( data.dptr == NULL ) {
210                                                         (void) dn_normalize_case( val );
211                                                         if ( ! be_issuffix( be,
212                                                             val ) ) {
213         Debug( LDAP_DEBUG_PARSE, "no parent \"%s\" of \"%s\"\n", dn, val, 0 );
214                                                         }
215                                                         *buf = '\0';
216                                                         lcur = 0;
217                                                         line[0] = '\0';
218                                                         continue;
219                                                 }
220                                                 (void) memcpy( (char *) &pid,
221                                                     data.dptr, sizeof(ID) );
222
223                                                 ldbm_datum_free( db->dbc_db, data);
224                                         }
225
226                                         sprintf( buf2, "%c%ld", EQ_PREFIX, pid );
227                                         key.dptr = buf2;
228                                         key.dsize = strlen( buf2 ) + 1;
229                                         if ( idl_insert_key( be, db2, key, id )
230                                             != 0 ) {
231                                                 perror( "idl_insert_key" );
232                                                 exit( EXIT_FAILURE );
233                                         }
234                                 }
235                         }
236                         *buf = '\0';
237                         lcur = 0;
238                         line[0] = '\0';
239                 }
240         }
241
242 #ifdef SLAP_CLEANUP
243         ldbm_cache_close( be, db2 );
244         ldbm_cache_close( be, db );
245 #endif
246
247         slap_shutdown(dbnum);
248         slap_destroy();
249
250         return( EXIT_SUCCESS );
251 }