1 /* tools.c - tools for slap tools */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
15 static DBC *cursor = NULL;
18 typedef struct dn_id {
23 #define HOLE_SIZE 4096
24 dn_id hbuf[HOLE_SIZE], *holes = hbuf;
25 unsigned nhmax = HOLE_SIZE;
28 int bdb_tool_entry_open(
29 BackendDB *be, int mode )
31 /* initialize key and data thangs */
34 key.flags = DB_DBT_REALLOC;
35 data.flags = DB_DBT_REALLOC;
40 int bdb_tool_entry_close(
55 cursor->c_close( cursor );
61 fprintf( stderr, "Error, entries missing!\n");
62 for (i=0; i<nholes; i++) {
63 fprintf(stderr, " entry %d: %s\n",
64 holes[i].id, holes[i].dn.bv_val, 0);
72 ID bdb_tool_entry_next(
77 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
80 assert( slapMode & SLAP_TOOL_MODE );
81 assert( bdb != NULL );
84 rc = bdb->bi_id2entry->bdi_db->cursor(
85 bdb->bi_id2entry->bdi_db, NULL, &cursor,
92 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
98 if( data.data == NULL ) {
102 AC_MEMCPY( &id, key.data, key.size );
106 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
112 assert( be != NULL );
113 assert( slapMode & SLAP_TOOL_MODE );
114 assert( data.data != NULL );
116 DBT2bv( &data, &bv );
118 rc = entry_decode( &bv, &e );
120 if( rc == LDAP_SUCCESS ) {
125 bdb_fix_dn(be, id, e);
131 int bdb_tool_next_id(
138 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
139 struct berval dn = e->e_nname;
143 rc = bdb_dn2id( be, tid, &dn, &e->e_id, 0 );
144 if ( rc == DB_NOTFOUND ) {
145 if ( be_issuffix( be, &dn ) ) {
148 dnParent( &dn, &pdn );
150 rc = bdb_tool_next_id( be, tid, e, text, 1 );
155 rc = bdb_next_id( be, tid, &e->e_id );
157 snprintf( text->bv_val, text->bv_len,
158 "next_id failed: %s (%d)",
159 db_strerror(rc), rc );
161 LDAP_LOG ( TOOLS, ERR,
162 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
164 Debug( LDAP_DEBUG_ANY,
165 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
170 rc = bdb_dn2id_add( be, tid, &pdn, e );
172 snprintf( text->bv_val, text->bv_len,
173 "dn2id_add failed: %s (%d)",
174 db_strerror(rc), rc );
176 LDAP_LOG ( TOOLS, ERR,
177 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
179 Debug( LDAP_DEBUG_ANY,
180 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
183 if ( nholes == nhmax - 1 ) {
184 if ( holes == hbuf ) {
185 holes = ch_malloc( nhmax * sizeof(ID) * 2 );
186 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
188 holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
192 ber_dupbv( &holes[nholes].dn, &dn );
193 holes[nholes++].id = e->e_id;
195 } else if ( !hole ) {
198 for ( i=0; i<nholes; i++) {
199 if ( holes[i].id == e->e_id ) {
201 free(holes[i].dn.bv_val);
202 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
206 } else if ( holes[i].id > e->e_id ) {
214 ID bdb_tool_entry_put(
217 struct berval *text )
220 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
225 assert( be != NULL );
226 assert( slapMode & SLAP_TOOL_MODE );
229 assert( text->bv_val );
230 assert( text->bv_val[0] == '\0' ); /* overconservative? */
233 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
234 (long) e->e_id, e->e_dn, 0 );
236 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
237 (long) e->e_id, e->e_dn, 0 );
240 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
241 bdb->bi_db_opflags );
243 snprintf( text->bv_val, text->bv_len,
244 "txn_begin failed: %s (%d)",
245 db_strerror(rc), rc );
247 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
249 Debug( LDAP_DEBUG_ANY,
250 "=> bdb_tool_entry_put: %s\n",
251 text->bv_val, 0, 0 );
256 /* add dn2id indices */
257 rc = bdb_tool_next_id( be, tid, e, text, 0 );
263 rc = bdb_id2entry_add( be, tid, e );
265 snprintf( text->bv_val, text->bv_len,
266 "id2entry_add failed: %s (%d)",
267 db_strerror(rc), rc );
269 LDAP_LOG ( TOOLS, ERR,
270 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
272 Debug( LDAP_DEBUG_ANY,
273 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
279 op.o_tmpmemctx = NULL;
280 op.o_tmpmfuncs = &ch_mfuncs;
281 rc = bdb_index_entry_add( &op, tid, e );
283 snprintf( text->bv_val, text->bv_len,
284 "index_entry_add failed: %s (%d)",
285 db_strerror(rc), rc );
287 LDAP_LOG ( TOOLS, ERR,
288 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
290 Debug( LDAP_DEBUG_ANY,
291 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
298 rc = TXN_COMMIT( tid, 0 );
300 snprintf( text->bv_val, text->bv_len,
301 "txn_commit failed: %s (%d)",
302 db_strerror(rc), rc );
304 LDAP_LOG ( TOOLS, ERR,
305 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
307 Debug( LDAP_DEBUG_ANY,
308 "=> bdb_tool_entry_put: %s\n",
309 text->bv_val, 0, 0 );
316 snprintf( text->bv_val, text->bv_len,
317 "txn_aborted! %s (%d)",
318 db_strerror(rc), rc );
320 LDAP_LOG ( TOOLS, ERR,
321 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
323 Debug( LDAP_DEBUG_ANY,
324 "=> bdb_tool_entry_put: %s\n",
325 text->bv_val, 0, 0 );
333 int bdb_tool_entry_reindex(
337 struct bdb_info *bi = (struct bdb_info *) be->be_private;
345 LDAP_LOG ( TOOLS, ARGS,
346 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
348 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
352 e = bdb_tool_entry_get( be, id );
356 LDAP_LOG ( TOOLS, DETAIL1,
357 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
360 Debug( LDAP_DEBUG_ANY,
361 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
367 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
370 LDAP_LOG ( TOOLS, ERR,
371 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
372 db_strerror(rc), rc, 0 );
374 Debug( LDAP_DEBUG_ANY,
375 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
376 db_strerror(rc), rc, 0 );
382 * just (re)add them for now
383 * assume that some other routine (not yet implemented)
384 * will zap index databases
389 LDAP_LOG ( TOOLS, ERR,
390 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
392 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
393 (long) id, e->e_dn, 0 );
396 /* add dn2id indices */
397 if ( be_issuffix( be, &e->e_nname ) ) {
400 dnParent( &e->e_nname, &pdn );
402 rc = bdb_dn2id_add( be, tid, &pdn, e );
403 if( rc != 0 && rc != DB_KEYEXIST ) {
405 LDAP_LOG ( TOOLS, ERR,
406 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
407 db_strerror(rc), rc, 0 );
409 Debug( LDAP_DEBUG_ANY,
410 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
411 db_strerror(rc), rc, 0 );
417 op.o_tmpmemctx = NULL;
418 op.o_tmpmfuncs = &ch_mfuncs;
419 rc = bdb_index_entry_add( &op, tid, e );
423 rc = TXN_COMMIT( tid, 0 );
426 LDAP_LOG ( TOOLS, ERR,
427 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
428 db_strerror(rc), rc, 0 );
430 Debug( LDAP_DEBUG_ANY,
431 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
432 db_strerror(rc), rc, 0 );
440 LDAP_LOG ( TOOLS, DETAIL1,
441 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
442 db_strerror(rc), rc, 0 );
444 Debug( LDAP_DEBUG_ANY,
445 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
446 db_strerror(rc), rc, 0 );