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 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(
145 if ( BER_BVISEMPTY(dn) )
150 op.o_tmpmemctx = NULL;
151 op.o_tmpmfuncs = &ch_mfuncs;
155 bdb_dn2id( &op, NULL, dn, &ei );
160 static struct berval ocbva[] = {
162 BER_BVC("syncProviderSubentry"),
166 int bdb_tool_id2entry_get(
172 int rc = bdb_id2entry( be, NULL, id, e );
174 if ( rc == DB_NOTFOUND && id == 0 ) {
175 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
176 dummy->e_name.bv_val = ch_strdup( "" );
177 dummy->e_nname.bv_val = ch_strdup( "" );
178 attr_merge( dummy, slap_schema.si_ad_objectClass, ocbva, NULL );
179 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
187 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
193 assert( be != NULL );
194 assert( slapMode & SLAP_TOOL_MODE );
195 assert( data.data != NULL );
198 DBT2bv( &data, &bv );
200 #ifdef SLAP_ZONE_ALLOC
201 /* FIXME: will add ctx later */
202 rc = entry_decode( &bv, &e, NULL );
204 rc = entry_decode( &bv, &e );
207 if( rc == LDAP_SUCCESS ) {
212 EntryInfo *ei = NULL;
218 op.o_tmpmemctx = NULL;
219 op.o_tmpmfuncs = &ch_mfuncs;
221 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
222 if ( rc == LDAP_SUCCESS )
229 static int bdb_tool_next_id(
236 struct berval dn = e->e_name;
237 struct berval ndn = e->e_nname;
238 struct berval pdn, npdn;
239 EntryInfo *ei = NULL, eidummy;
242 if (ndn.bv_len == 0) {
247 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
248 if ( ei ) bdb_cache_entryinfo_unlock( ei );
249 if ( rc == DB_NOTFOUND ) {
250 if ( !be_issuffix( op->o_bd, &ndn ) ) {
252 dnParent( &dn, &pdn );
253 dnParent( &ndn, &npdn );
256 rc = bdb_tool_next_id( op, tid, e, text, 1 );
262 /* If parent didn't exist, it was created just now
263 * and its ID is now in e->e_id. Make sure the current
264 * entry gets added under the new parent ID.
266 if ( eid != e->e_id ) {
267 eidummy.bei_id = e->e_id;
271 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
273 snprintf( text->bv_val, text->bv_len,
274 "next_id failed: %s (%d)",
275 db_strerror(rc), rc );
276 Debug( LDAP_DEBUG_ANY,
277 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
280 rc = bdb_dn2id_add( op, tid, ei, e );
282 snprintf( text->bv_val, text->bv_len,
283 "dn2id_add failed: %s (%d)",
284 db_strerror(rc), rc );
285 Debug( LDAP_DEBUG_ANY,
286 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
288 if ( nholes == nhmax - 1 ) {
289 if ( holes == hbuf ) {
290 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
291 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
293 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
297 ber_dupbv( &holes[nholes].dn, &ndn );
298 holes[nholes++].id = e->e_id;
300 } else if ( !hole ) {
303 e->e_id = ei->bei_id;
305 for ( i=0; i<nholes; i++) {
306 if ( holes[i].id == e->e_id ) {
308 free(holes[i].dn.bv_val);
309 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
313 } else if ( holes[i].id > e->e_id ) {
321 ID bdb_tool_entry_put(
324 struct berval *text )
327 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
332 assert( be != NULL );
333 assert( slapMode & SLAP_TOOL_MODE );
336 assert( text->bv_val );
337 assert( text->bv_val[0] == '\0' ); /* overconservative? */
339 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
340 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
342 if (! (slapMode & SLAP_TOOL_QUICK)) {
343 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
344 bdb->bi_db_opflags );
346 snprintf( text->bv_val, text->bv_len,
347 "txn_begin failed: %s (%d)",
348 db_strerror(rc), rc );
349 Debug( LDAP_DEBUG_ANY,
350 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
351 text->bv_val, 0, 0 );
358 op.o_tmpmemctx = NULL;
359 op.o_tmpmfuncs = &ch_mfuncs;
361 /* add dn2id indices */
362 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
368 rc = bdb_id2entry_add( be, tid, e );
370 snprintf( text->bv_val, text->bv_len,
371 "id2entry_add failed: %s (%d)",
372 db_strerror(rc), rc );
373 Debug( LDAP_DEBUG_ANY,
374 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
375 text->bv_val, 0, 0 );
379 if ( !bdb->bi_linear_index )
380 rc = bdb_index_entry_add( &op, tid, e );
382 snprintf( text->bv_val, text->bv_len,
383 "index_entry_add failed: %s (%d)",
384 db_strerror(rc), rc );
385 Debug( LDAP_DEBUG_ANY,
386 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
387 text->bv_val, 0, 0 );
393 if ( !( slapMode & SLAP_TOOL_QUICK )) {
394 rc = TXN_COMMIT( tid, 0 );
396 snprintf( text->bv_val, text->bv_len,
397 "txn_commit failed: %s (%d)",
398 db_strerror(rc), rc );
399 Debug( LDAP_DEBUG_ANY,
400 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
401 text->bv_val, 0, 0 );
407 if ( !( slapMode & SLAP_TOOL_QUICK )) {
409 snprintf( text->bv_val, text->bv_len,
410 "txn_aborted! %s (%d)",
411 db_strerror(rc), rc );
412 Debug( LDAP_DEBUG_ANY,
413 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
414 text->bv_val, 0, 0 );
422 int bdb_tool_entry_reindex(
426 struct bdb_info *bi = (struct bdb_info *) be->be_private;
433 Debug( LDAP_DEBUG_ARGS,
434 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
437 /* No indexes configured, nothing to do. Could return an
438 * error here to shortcut things.
444 /* Get the first attribute to index */
445 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
446 index_attrs = bi->bi_attrs;
447 bi->bi_attrs = &index_dummy;
448 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
451 e = bdb_tool_entry_get( be, id );
454 Debug( LDAP_DEBUG_ANY,
455 LDAP_XSTRING(bdb_tool_entry_reindex)
456 ": could not locate id=%ld\n",
461 if (! (slapMode & SLAP_TOOL_QUICK)) {
462 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
464 Debug( LDAP_DEBUG_ANY,
465 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
466 "txn_begin failed: %s (%d)\n",
467 db_strerror(rc), rc, 0 );
473 * just (re)add them for now
474 * assume that some other routine (not yet implemented)
475 * will zap index databases
479 Debug( LDAP_DEBUG_TRACE,
480 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
481 (long) id, e->e_dn, 0 );
485 op.o_tmpmemctx = NULL;
486 op.o_tmpmfuncs = &ch_mfuncs;
488 rc = bdb_index_entry_add( &op, tid, e );
492 if (! (slapMode & SLAP_TOOL_QUICK)) {
493 rc = TXN_COMMIT( tid, 0 );
495 Debug( LDAP_DEBUG_ANY,
496 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
497 ": txn_commit failed: %s (%d)\n",
498 db_strerror(rc), rc, 0 );
504 if (! (slapMode & SLAP_TOOL_QUICK)) {
506 Debug( LDAP_DEBUG_ANY,
507 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
508 ": txn_aborted! %s (%d)\n",
509 db_strerror(rc), rc, 0 );
513 bdb_entry_release( &op, e, 0 );
518 ID bdb_tool_entry_modify(
521 struct berval *text )
524 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
529 assert( be != NULL );
530 assert( slapMode & SLAP_TOOL_MODE );
533 assert( text->bv_val );
534 assert( text->bv_val[0] == '\0' ); /* overconservative? */
536 assert ( e->e_id != NOID );
538 Debug( LDAP_DEBUG_TRACE,
539 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
540 (long) e->e_id, e->e_dn, 0 );
542 if (! (slapMode & SLAP_TOOL_QUICK)) {
543 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
544 bdb->bi_db_opflags );
546 snprintf( text->bv_val, text->bv_len,
547 "txn_begin failed: %s (%d)",
548 db_strerror(rc), rc );
549 Debug( LDAP_DEBUG_ANY,
550 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
551 text->bv_val, 0, 0 );
558 op.o_tmpmemctx = NULL;
559 op.o_tmpmfuncs = &ch_mfuncs;
562 rc = bdb_id2entry_update( be, tid, e );
564 snprintf( text->bv_val, text->bv_len,
565 "id2entry_add failed: %s (%d)",
566 db_strerror(rc), rc );
567 Debug( LDAP_DEBUG_ANY,
568 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
569 text->bv_val, 0, 0 );
574 /* FIXME: this is bogus, we don't have the old values to delete
575 * from the index because the given entry has already been modified.
577 rc = bdb_index_entry_del( &op, tid, e );
579 snprintf( text->bv_val, text->bv_len,
580 "index_entry_del failed: %s (%d)",
581 db_strerror(rc), rc );
582 Debug( LDAP_DEBUG_ANY,
583 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
584 text->bv_val, 0, 0 );
589 rc = bdb_index_entry_add( &op, tid, e );
591 snprintf( text->bv_val, text->bv_len,
592 "index_entry_add failed: %s (%d)",
593 db_strerror(rc), rc );
594 Debug( LDAP_DEBUG_ANY,
595 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
596 text->bv_val, 0, 0 );
602 if (! (slapMode & SLAP_TOOL_QUICK)) {
603 rc = TXN_COMMIT( tid, 0 );
605 snprintf( text->bv_val, text->bv_len,
606 "txn_commit failed: %s (%d)",
607 db_strerror(rc), rc );
608 Debug( LDAP_DEBUG_ANY,
609 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
610 "%s\n", text->bv_val, 0, 0 );
616 if (! (slapMode & SLAP_TOOL_QUICK)) {
618 snprintf( text->bv_val, text->bv_len,
619 "txn_aborted! %s (%d)",
620 db_strerror(rc), rc );
621 Debug( LDAP_DEBUG_ANY,
622 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
623 text->bv_val, 0, 0 );