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