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 )
190 assert( be != NULL );
191 assert( slapMode & SLAP_TOOL_MODE );
192 assert( data.data != NULL );
195 DBT2bv( &data, &bv );
197 #ifdef SLAP_ZONE_ALLOC
198 /* FIXME: will add ctx later */
199 rc = entry_decode( &bv, &e, NULL );
201 rc = entry_decode( &bv, &e );
204 if( rc == LDAP_SUCCESS ) {
209 EntryInfo *ei = NULL;
215 op.o_tmpmemctx = NULL;
216 op.o_tmpmfuncs = &ch_mfuncs;
218 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
219 if ( rc == LDAP_SUCCESS )
226 static int bdb_tool_next_id(
233 struct berval dn = e->e_name;
234 struct berval ndn = e->e_nname;
235 struct berval pdn, npdn;
236 EntryInfo *ei = NULL, eidummy;
239 if (ndn.bv_len == 0) {
244 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
245 if ( ei ) bdb_cache_entryinfo_unlock( ei );
246 if ( rc == DB_NOTFOUND ) {
247 if ( !be_issuffix( op->o_bd, &ndn ) ) {
249 dnParent( &dn, &pdn );
250 dnParent( &ndn, &npdn );
253 rc = bdb_tool_next_id( op, tid, e, text, 1 );
259 /* If parent didn't exist, it was created just now
260 * and its ID is now in e->e_id. Make sure the current
261 * entry gets added under the new parent ID.
263 if ( eid != e->e_id ) {
264 eidummy.bei_id = e->e_id;
268 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
270 snprintf( text->bv_val, text->bv_len,
271 "next_id failed: %s (%d)",
272 db_strerror(rc), rc );
273 Debug( LDAP_DEBUG_ANY,
274 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
277 rc = bdb_dn2id_add( op, tid, ei, e );
279 snprintf( text->bv_val, text->bv_len,
280 "dn2id_add failed: %s (%d)",
281 db_strerror(rc), rc );
282 Debug( LDAP_DEBUG_ANY,
283 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
285 if ( nholes == nhmax - 1 ) {
286 if ( holes == hbuf ) {
287 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
288 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
290 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
294 ber_dupbv( &holes[nholes].dn, &ndn );
295 holes[nholes++].id = e->e_id;
297 } else if ( !hole ) {
300 e->e_id = ei->bei_id;
302 for ( i=0; i<nholes; i++) {
303 if ( holes[i].id == e->e_id ) {
305 free(holes[i].dn.bv_val);
306 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
310 } else if ( holes[i].id > e->e_id ) {
318 ID bdb_tool_entry_put(
321 struct berval *text )
324 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
329 assert( be != NULL );
330 assert( slapMode & SLAP_TOOL_MODE );
333 assert( text->bv_val );
334 assert( text->bv_val[0] == '\0' ); /* overconservative? */
336 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
337 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
339 if (! (slapMode & SLAP_TOOL_QUICK)) {
340 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
341 bdb->bi_db_opflags );
343 snprintf( text->bv_val, text->bv_len,
344 "txn_begin failed: %s (%d)",
345 db_strerror(rc), rc );
346 Debug( LDAP_DEBUG_ANY,
347 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
348 text->bv_val, 0, 0 );
355 op.o_tmpmemctx = NULL;
356 op.o_tmpmfuncs = &ch_mfuncs;
358 /* add dn2id indices */
359 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
365 rc = bdb_id2entry_add( be, tid, e );
367 snprintf( text->bv_val, text->bv_len,
368 "id2entry_add failed: %s (%d)",
369 db_strerror(rc), rc );
370 Debug( LDAP_DEBUG_ANY,
371 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
372 text->bv_val, 0, 0 );
376 if ( !bdb->bi_linear_index )
377 rc = bdb_index_entry_add( &op, tid, e );
379 snprintf( text->bv_val, text->bv_len,
380 "index_entry_add failed: %s (%d)",
381 db_strerror(rc), rc );
382 Debug( LDAP_DEBUG_ANY,
383 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
384 text->bv_val, 0, 0 );
390 if ( !( slapMode & SLAP_TOOL_QUICK )) {
391 rc = TXN_COMMIT( tid, 0 );
393 snprintf( text->bv_val, text->bv_len,
394 "txn_commit failed: %s (%d)",
395 db_strerror(rc), rc );
396 Debug( LDAP_DEBUG_ANY,
397 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
398 text->bv_val, 0, 0 );
404 if ( !( slapMode & SLAP_TOOL_QUICK )) {
406 snprintf( text->bv_val, text->bv_len,
407 "txn_aborted! %s (%d)",
408 db_strerror(rc), rc );
409 Debug( LDAP_DEBUG_ANY,
410 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
411 text->bv_val, 0, 0 );
419 int bdb_tool_entry_reindex(
423 struct bdb_info *bi = (struct bdb_info *) be->be_private;
430 Debug( LDAP_DEBUG_ARGS,
431 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
434 /* No indexes configured, nothing to do. Could return an
435 * error here to shortcut things.
441 /* Get the first attribute to index */
442 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
443 index_attrs = bi->bi_attrs;
444 bi->bi_attrs = &index_dummy;
445 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
448 e = bdb_tool_entry_get( be, id );
451 Debug( LDAP_DEBUG_ANY,
452 LDAP_XSTRING(bdb_tool_entry_reindex)
453 ": could not locate id=%ld\n",
458 if (! (slapMode & SLAP_TOOL_QUICK)) {
459 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
461 Debug( LDAP_DEBUG_ANY,
462 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
463 "txn_begin failed: %s (%d)\n",
464 db_strerror(rc), rc, 0 );
470 * just (re)add them for now
471 * assume that some other routine (not yet implemented)
472 * will zap index databases
476 Debug( LDAP_DEBUG_TRACE,
477 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
478 (long) id, e->e_dn, 0 );
482 op.o_tmpmemctx = NULL;
483 op.o_tmpmfuncs = &ch_mfuncs;
485 rc = bdb_index_entry_add( &op, tid, e );
489 if (! (slapMode & SLAP_TOOL_QUICK)) {
490 rc = TXN_COMMIT( tid, 0 );
492 Debug( LDAP_DEBUG_ANY,
493 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
494 ": txn_commit failed: %s (%d)\n",
495 db_strerror(rc), rc, 0 );
501 if (! (slapMode & SLAP_TOOL_QUICK)) {
503 Debug( LDAP_DEBUG_ANY,
504 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
505 ": txn_aborted! %s (%d)\n",
506 db_strerror(rc), rc, 0 );
510 bdb_entry_release( &op, e, 0 );
515 ID bdb_tool_entry_modify(
518 struct berval *text )
521 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
526 assert( be != NULL );
527 assert( slapMode & SLAP_TOOL_MODE );
530 assert( text->bv_val );
531 assert( text->bv_val[0] == '\0' ); /* overconservative? */
533 assert ( e->e_id != NOID );
535 Debug( LDAP_DEBUG_TRACE,
536 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
537 (long) e->e_id, e->e_dn, 0 );
539 if (! (slapMode & SLAP_TOOL_QUICK)) {
540 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
541 bdb->bi_db_opflags );
543 snprintf( text->bv_val, text->bv_len,
544 "txn_begin failed: %s (%d)",
545 db_strerror(rc), rc );
546 Debug( LDAP_DEBUG_ANY,
547 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
548 text->bv_val, 0, 0 );
555 op.o_tmpmemctx = NULL;
556 op.o_tmpmfuncs = &ch_mfuncs;
559 rc = bdb_id2entry_update( be, tid, e );
561 snprintf( text->bv_val, text->bv_len,
562 "id2entry_add failed: %s (%d)",
563 db_strerror(rc), rc );
564 Debug( LDAP_DEBUG_ANY,
565 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
566 text->bv_val, 0, 0 );
571 /* FIXME: this is bogus, we don't have the old values to delete
572 * from the index because the given entry has already been modified.
574 rc = bdb_index_entry_del( &op, tid, e );
576 snprintf( text->bv_val, text->bv_len,
577 "index_entry_del failed: %s (%d)",
578 db_strerror(rc), rc );
579 Debug( LDAP_DEBUG_ANY,
580 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
581 text->bv_val, 0, 0 );
586 rc = bdb_index_entry_add( &op, tid, e );
588 snprintf( text->bv_val, text->bv_len,
589 "index_entry_add failed: %s (%d)",
590 db_strerror(rc), rc );
591 Debug( LDAP_DEBUG_ANY,
592 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
593 text->bv_val, 0, 0 );
599 if (! (slapMode & SLAP_TOOL_QUICK)) {
600 rc = TXN_COMMIT( tid, 0 );
602 snprintf( text->bv_val, text->bv_len,
603 "txn_commit failed: %s (%d)",
604 db_strerror(rc), rc );
605 Debug( LDAP_DEBUG_ANY,
606 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
607 "%s\n", text->bv_val, 0, 0 );
613 if (! (slapMode & SLAP_TOOL_QUICK)) {
615 snprintf( text->bv_val, text->bv_len,
616 "txn_aborted! %s (%d)",
617 db_strerror(rc), rc );
618 Debug( LDAP_DEBUG_ANY,
619 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
620 text->bv_val, 0, 0 );