1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
25 static DBC *cursor = NULL;
28 typedef struct dn_id {
33 #define HOLE_SIZE 4096
34 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
35 static unsigned nhmax = HOLE_SIZE;
36 static unsigned nholes;
38 static Avlnode *index_attrs, index_dummy;
40 int bdb_tool_entry_open(
41 BackendDB *be, int mode )
43 /* initialize key and data thangs */
46 key.flags = DB_DBT_REALLOC;
47 data.flags = DB_DBT_REALLOC;
52 int bdb_tool_entry_close(
67 cursor->c_close( cursor );
73 fprintf( stderr, "Error, entries missing!\n");
74 for (i=0; i<nholes; i++) {
75 fprintf(stderr, " entry %ld: %s\n",
76 holes[i].id, holes[i].dn.bv_val);
84 static int bdb_reindex_cmp(const void *a, const void *b) { return 0; }
86 ID bdb_tool_entry_next(
91 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
94 assert( slapMode & SLAP_TOOL_MODE );
95 assert( bdb != NULL );
99 rc = bdb->bi_id2entry->bdi_db->cursor(
100 bdb->bi_id2entry->bdi_db, NULL, &cursor,
101 bdb->bi_db_opflags );
107 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
110 /* If we're doing linear indexing and there are more attrs to
111 * index, and we're at the end of the database, start over.
113 if ( bdb->bi_attrs == &index_dummy ) {
114 if ( index_attrs && rc == DB_NOTFOUND ) {
115 /* optional - do a checkpoint here? */
116 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
117 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
120 bdb->bi_attrs = NULL;
128 if( data.data == NULL ) {
132 BDB_DISK2ID( key.data, &id );
136 ID bdb_tool_dn2id_get(
143 EntryInfo *ei = NULL;
146 if ( BER_BVISEMPTY(dn) )
151 op.o_tmpmemctx = NULL;
152 op.o_tmpmfuncs = &ch_mfuncs;
154 rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
155 if ( ei ) bdb_cache_entryinfo_unlock( ei );
156 if ( rc == DB_NOTFOUND )
162 int bdb_tool_id2entry_get(
168 int rc = bdb_id2entry( be, NULL, id, e );
170 if ( rc == DB_NOTFOUND && id == 0 ) {
171 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
172 struct berval gluebv = BER_BVC("glue");
173 dummy->e_name.bv_val = ch_strdup( "" );
174 dummy->e_nname.bv_val = ch_strdup( "" );
175 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
176 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
184 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
192 assert( be != NULL );
193 assert( slapMode & SLAP_TOOL_MODE );
194 assert( data.data != NULL );
197 DBT2bv( &data, &bv );
199 #ifdef SLAP_ZONE_ALLOC
200 /* FIXME: will add ctx later */
201 rc = entry_decode( &bv, &e, NULL );
203 rc = entry_decode( &bv, &e );
206 if( rc == LDAP_SUCCESS ) {
211 EntryInfo *ei = NULL;
217 op.o_tmpmemctx = NULL;
218 op.o_tmpmfuncs = &ch_mfuncs;
220 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
221 if ( rc == LDAP_SUCCESS )
228 static int bdb_tool_next_id(
235 struct berval dn = e->e_name;
236 struct berval ndn = e->e_nname;
237 struct berval pdn, npdn;
238 EntryInfo *ei = NULL, eidummy;
241 if (ndn.bv_len == 0) {
246 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
247 if ( ei ) bdb_cache_entryinfo_unlock( ei );
248 if ( rc == DB_NOTFOUND ) {
249 if ( !be_issuffix( op->o_bd, &ndn ) ) {
251 dnParent( &dn, &pdn );
252 dnParent( &ndn, &npdn );
255 rc = bdb_tool_next_id( op, tid, e, text, 1 );
261 /* If parent didn't exist, it was created just now
262 * and its ID is now in e->e_id. Make sure the current
263 * entry gets added under the new parent ID.
265 if ( eid != e->e_id ) {
266 eidummy.bei_id = e->e_id;
270 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
272 snprintf( text->bv_val, text->bv_len,
273 "next_id failed: %s (%d)",
274 db_strerror(rc), rc );
275 Debug( LDAP_DEBUG_ANY,
276 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
279 rc = bdb_dn2id_add( op, tid, ei, e );
281 snprintf( text->bv_val, text->bv_len,
282 "dn2id_add failed: %s (%d)",
283 db_strerror(rc), rc );
284 Debug( LDAP_DEBUG_ANY,
285 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
287 if ( nholes == nhmax - 1 ) {
288 if ( holes == hbuf ) {
289 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
290 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
292 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
296 ber_dupbv( &holes[nholes].dn, &ndn );
297 holes[nholes++].id = e->e_id;
299 } else if ( !hole ) {
302 e->e_id = ei->bei_id;
304 for ( i=0; i<nholes; i++) {
305 if ( holes[i].id == e->e_id ) {
307 free(holes[i].dn.bv_val);
308 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
312 } else if ( holes[i].id > e->e_id ) {
320 ID bdb_tool_entry_put(
323 struct berval *text )
326 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
331 assert( be != NULL );
332 assert( slapMode & SLAP_TOOL_MODE );
334 assert( text != NULL );
335 assert( text->bv_val != NULL );
336 assert( text->bv_val[0] == '\0' ); /* overconservative? */
338 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
339 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
341 if (! (slapMode & SLAP_TOOL_QUICK)) {
342 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
343 bdb->bi_db_opflags );
345 snprintf( text->bv_val, text->bv_len,
346 "txn_begin failed: %s (%d)",
347 db_strerror(rc), rc );
348 Debug( LDAP_DEBUG_ANY,
349 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
350 text->bv_val, 0, 0 );
357 op.o_tmpmemctx = NULL;
358 op.o_tmpmfuncs = &ch_mfuncs;
360 /* add dn2id indices */
361 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
367 rc = bdb_id2entry_add( be, tid, e );
369 snprintf( text->bv_val, text->bv_len,
370 "id2entry_add failed: %s (%d)",
371 db_strerror(rc), rc );
372 Debug( LDAP_DEBUG_ANY,
373 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
374 text->bv_val, 0, 0 );
378 if ( !bdb->bi_linear_index )
379 rc = bdb_index_entry_add( &op, tid, e );
381 snprintf( text->bv_val, text->bv_len,
382 "index_entry_add failed: %s (%d)",
383 db_strerror(rc), rc );
384 Debug( LDAP_DEBUG_ANY,
385 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
386 text->bv_val, 0, 0 );
392 if ( !( slapMode & SLAP_TOOL_QUICK )) {
393 rc = TXN_COMMIT( tid, 0 );
395 snprintf( text->bv_val, text->bv_len,
396 "txn_commit failed: %s (%d)",
397 db_strerror(rc), rc );
398 Debug( LDAP_DEBUG_ANY,
399 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
400 text->bv_val, 0, 0 );
406 if ( !( slapMode & SLAP_TOOL_QUICK )) {
408 snprintf( text->bv_val, text->bv_len,
409 "txn_aborted! %s (%d)",
410 db_strerror(rc), rc );
411 Debug( LDAP_DEBUG_ANY,
412 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
413 text->bv_val, 0, 0 );
421 int bdb_tool_entry_reindex(
425 struct bdb_info *bi = (struct bdb_info *) be->be_private;
432 Debug( LDAP_DEBUG_ARGS,
433 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
436 /* No indexes configured, nothing to do. Could return an
437 * error here to shortcut things.
443 /* Get the first attribute to index */
444 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
445 index_attrs = bi->bi_attrs;
446 bi->bi_attrs = &index_dummy;
447 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
450 e = bdb_tool_entry_get( be, id );
453 Debug( LDAP_DEBUG_ANY,
454 LDAP_XSTRING(bdb_tool_entry_reindex)
455 ": could not locate id=%ld\n",
460 if (! (slapMode & SLAP_TOOL_QUICK)) {
461 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
463 Debug( LDAP_DEBUG_ANY,
464 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
465 "txn_begin failed: %s (%d)\n",
466 db_strerror(rc), rc, 0 );
472 * just (re)add them for now
473 * assume that some other routine (not yet implemented)
474 * will zap index databases
478 Debug( LDAP_DEBUG_TRACE,
479 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
480 (long) id, e->e_dn, 0 );
484 op.o_tmpmemctx = NULL;
485 op.o_tmpmfuncs = &ch_mfuncs;
487 rc = bdb_index_entry_add( &op, tid, e );
491 if (! (slapMode & SLAP_TOOL_QUICK)) {
492 rc = TXN_COMMIT( tid, 0 );
494 Debug( LDAP_DEBUG_ANY,
495 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
496 ": txn_commit failed: %s (%d)\n",
497 db_strerror(rc), rc, 0 );
503 if (! (slapMode & SLAP_TOOL_QUICK)) {
505 Debug( LDAP_DEBUG_ANY,
506 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
507 ": txn_aborted! %s (%d)\n",
508 db_strerror(rc), rc, 0 );
512 bdb_entry_release( &op, e, 0 );
517 ID bdb_tool_entry_modify(
520 struct berval *text )
523 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
528 assert( be != NULL );
529 assert( slapMode & SLAP_TOOL_MODE );
531 assert( text != NULL );
532 assert( text->bv_val != NULL );
533 assert( text->bv_val[0] == '\0' ); /* overconservative? */
535 assert ( e->e_id != NOID );
537 Debug( LDAP_DEBUG_TRACE,
538 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
539 (long) e->e_id, e->e_dn, 0 );
541 if (! (slapMode & SLAP_TOOL_QUICK)) {
542 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
543 bdb->bi_db_opflags );
545 snprintf( text->bv_val, text->bv_len,
546 "txn_begin failed: %s (%d)",
547 db_strerror(rc), rc );
548 Debug( LDAP_DEBUG_ANY,
549 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
550 text->bv_val, 0, 0 );
557 op.o_tmpmemctx = NULL;
558 op.o_tmpmfuncs = &ch_mfuncs;
561 rc = bdb_id2entry_update( be, tid, e );
563 snprintf( text->bv_val, text->bv_len,
564 "id2entry_add failed: %s (%d)",
565 db_strerror(rc), rc );
566 Debug( LDAP_DEBUG_ANY,
567 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
568 text->bv_val, 0, 0 );
573 /* FIXME: this is bogus, we don't have the old values to delete
574 * from the index because the given entry has already been modified.
576 rc = bdb_index_entry_del( &op, tid, e );
578 snprintf( text->bv_val, text->bv_len,
579 "index_entry_del failed: %s (%d)",
580 db_strerror(rc), rc );
581 Debug( LDAP_DEBUG_ANY,
582 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
583 text->bv_val, 0, 0 );
588 rc = bdb_index_entry_add( &op, tid, e );
590 snprintf( text->bv_val, text->bv_len,
591 "index_entry_add failed: %s (%d)",
592 db_strerror(rc), rc );
593 Debug( LDAP_DEBUG_ANY,
594 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
595 text->bv_val, 0, 0 );
601 if (! (slapMode & SLAP_TOOL_QUICK)) {
602 rc = TXN_COMMIT( tid, 0 );
604 snprintf( text->bv_val, text->bv_len,
605 "txn_commit failed: %s (%d)",
606 db_strerror(rc), rc );
607 Debug( LDAP_DEBUG_ANY,
608 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
609 "%s\n", text->bv_val, 0, 0 );
615 if (! (slapMode & SLAP_TOOL_QUICK)) {
617 snprintf( text->bv_val, text->bv_len,
618 "txn_aborted! %s (%d)",
619 db_strerror(rc), rc );
620 Debug( LDAP_DEBUG_ANY,
621 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
622 text->bv_val, 0, 0 );