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(
145 if ( BER_BVISEMPTY(dn) )
150 op.o_tmpmemctx = NULL;
151 op.o_tmpmfuncs = &ch_mfuncs;
155 bdb_dn2id( &op, NULL, dn, &ei );
160 int bdb_tool_id2entry_get(
166 int rc = bdb_id2entry( be, NULL, id, e );
168 if ( rc == DB_NOTFOUND && id == 0 ) {
169 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
170 struct berval gluebv = BER_BVC("glue");
171 dummy->e_name.bv_val = ch_strdup( "" );
172 dummy->e_nname.bv_val = ch_strdup( "" );
173 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
174 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
182 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
188 assert( be != NULL );
189 assert( slapMode & SLAP_TOOL_MODE );
190 assert( data.data != NULL );
193 DBT2bv( &data, &bv );
195 #ifdef SLAP_ZONE_ALLOC
196 /* FIXME: will add ctx later */
197 rc = entry_decode( &bv, &e, NULL );
199 rc = entry_decode( &bv, &e );
202 if( rc == LDAP_SUCCESS ) {
207 EntryInfo *ei = NULL;
213 op.o_tmpmemctx = NULL;
214 op.o_tmpmfuncs = &ch_mfuncs;
216 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
217 if ( rc == LDAP_SUCCESS )
224 static int bdb_tool_next_id(
231 struct berval dn = e->e_name;
232 struct berval ndn = e->e_nname;
233 struct berval pdn, npdn;
234 EntryInfo *ei = NULL, eidummy;
237 if (ndn.bv_len == 0) {
242 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
243 if ( ei ) bdb_cache_entryinfo_unlock( ei );
244 if ( rc == DB_NOTFOUND ) {
245 if ( !be_issuffix( op->o_bd, &ndn ) ) {
247 dnParent( &dn, &pdn );
248 dnParent( &ndn, &npdn );
251 rc = bdb_tool_next_id( op, tid, e, text, 1 );
257 /* If parent didn't exist, it was created just now
258 * and its ID is now in e->e_id. Make sure the current
259 * entry gets added under the new parent ID.
261 if ( eid != e->e_id ) {
262 eidummy.bei_id = e->e_id;
266 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
268 snprintf( text->bv_val, text->bv_len,
269 "next_id failed: %s (%d)",
270 db_strerror(rc), rc );
271 Debug( LDAP_DEBUG_ANY,
272 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
275 rc = bdb_dn2id_add( op, tid, ei, e );
277 snprintf( text->bv_val, text->bv_len,
278 "dn2id_add failed: %s (%d)",
279 db_strerror(rc), rc );
280 Debug( LDAP_DEBUG_ANY,
281 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
283 if ( nholes == nhmax - 1 ) {
284 if ( holes == hbuf ) {
285 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
286 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
288 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
292 ber_dupbv( &holes[nholes].dn, &ndn );
293 holes[nholes++].id = e->e_id;
295 } else if ( !hole ) {
298 e->e_id = ei->bei_id;
300 for ( i=0; i<nholes; i++) {
301 if ( holes[i].id == e->e_id ) {
303 free(holes[i].dn.bv_val);
304 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
308 } else if ( holes[i].id > e->e_id ) {
316 ID bdb_tool_entry_put(
319 struct berval *text )
322 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
327 assert( be != NULL );
328 assert( slapMode & SLAP_TOOL_MODE );
330 assert( text != NULL );
331 assert( text->bv_val != NULL );
332 assert( text->bv_val[0] == '\0' ); /* overconservative? */
334 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
335 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
337 if (! (slapMode & SLAP_TOOL_QUICK)) {
338 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
339 bdb->bi_db_opflags );
341 snprintf( text->bv_val, text->bv_len,
342 "txn_begin failed: %s (%d)",
343 db_strerror(rc), rc );
344 Debug( LDAP_DEBUG_ANY,
345 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
346 text->bv_val, 0, 0 );
353 op.o_tmpmemctx = NULL;
354 op.o_tmpmfuncs = &ch_mfuncs;
356 /* add dn2id indices */
357 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
363 rc = bdb_id2entry_add( be, tid, e );
365 snprintf( text->bv_val, text->bv_len,
366 "id2entry_add failed: %s (%d)",
367 db_strerror(rc), rc );
368 Debug( LDAP_DEBUG_ANY,
369 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
370 text->bv_val, 0, 0 );
374 if ( !bdb->bi_linear_index )
375 rc = bdb_index_entry_add( &op, tid, e );
377 snprintf( text->bv_val, text->bv_len,
378 "index_entry_add failed: %s (%d)",
379 db_strerror(rc), rc );
380 Debug( LDAP_DEBUG_ANY,
381 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
382 text->bv_val, 0, 0 );
388 if ( !( slapMode & SLAP_TOOL_QUICK )) {
389 rc = TXN_COMMIT( tid, 0 );
391 snprintf( text->bv_val, text->bv_len,
392 "txn_commit failed: %s (%d)",
393 db_strerror(rc), rc );
394 Debug( LDAP_DEBUG_ANY,
395 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
396 text->bv_val, 0, 0 );
402 if ( !( slapMode & SLAP_TOOL_QUICK )) {
404 snprintf( text->bv_val, text->bv_len,
405 "txn_aborted! %s (%d)",
406 db_strerror(rc), rc );
407 Debug( LDAP_DEBUG_ANY,
408 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
409 text->bv_val, 0, 0 );
417 int bdb_tool_entry_reindex(
421 struct bdb_info *bi = (struct bdb_info *) be->be_private;
428 Debug( LDAP_DEBUG_ARGS,
429 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
432 /* No indexes configured, nothing to do. Could return an
433 * error here to shortcut things.
439 /* Get the first attribute to index */
440 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
441 index_attrs = bi->bi_attrs;
442 bi->bi_attrs = &index_dummy;
443 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
446 e = bdb_tool_entry_get( be, id );
449 Debug( LDAP_DEBUG_ANY,
450 LDAP_XSTRING(bdb_tool_entry_reindex)
451 ": could not locate id=%ld\n",
456 if (! (slapMode & SLAP_TOOL_QUICK)) {
457 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
459 Debug( LDAP_DEBUG_ANY,
460 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
461 "txn_begin failed: %s (%d)\n",
462 db_strerror(rc), rc, 0 );
468 * just (re)add them for now
469 * assume that some other routine (not yet implemented)
470 * will zap index databases
474 Debug( LDAP_DEBUG_TRACE,
475 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
476 (long) id, e->e_dn, 0 );
480 op.o_tmpmemctx = NULL;
481 op.o_tmpmfuncs = &ch_mfuncs;
483 rc = bdb_index_entry_add( &op, tid, e );
487 if (! (slapMode & SLAP_TOOL_QUICK)) {
488 rc = TXN_COMMIT( tid, 0 );
490 Debug( LDAP_DEBUG_ANY,
491 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
492 ": txn_commit failed: %s (%d)\n",
493 db_strerror(rc), rc, 0 );
499 if (! (slapMode & SLAP_TOOL_QUICK)) {
501 Debug( LDAP_DEBUG_ANY,
502 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
503 ": txn_aborted! %s (%d)\n",
504 db_strerror(rc), rc, 0 );
508 bdb_entry_release( &op, e, 0 );
513 ID bdb_tool_entry_modify(
516 struct berval *text )
519 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
524 assert( be != NULL );
525 assert( slapMode & SLAP_TOOL_MODE );
527 assert( text != NULL );
528 assert( text->bv_val != NULL );
529 assert( text->bv_val[0] == '\0' ); /* overconservative? */
531 assert ( e->e_id != NOID );
533 Debug( LDAP_DEBUG_TRACE,
534 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
535 (long) e->e_id, e->e_dn, 0 );
537 if (! (slapMode & SLAP_TOOL_QUICK)) {
538 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
539 bdb->bi_db_opflags );
541 snprintf( text->bv_val, text->bv_len,
542 "txn_begin failed: %s (%d)",
543 db_strerror(rc), rc );
544 Debug( LDAP_DEBUG_ANY,
545 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
546 text->bv_val, 0, 0 );
553 op.o_tmpmemctx = NULL;
554 op.o_tmpmfuncs = &ch_mfuncs;
557 rc = bdb_id2entry_update( be, tid, e );
559 snprintf( text->bv_val, text->bv_len,
560 "id2entry_add failed: %s (%d)",
561 db_strerror(rc), rc );
562 Debug( LDAP_DEBUG_ANY,
563 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
564 text->bv_val, 0, 0 );
569 /* FIXME: this is bogus, we don't have the old values to delete
570 * from the index because the given entry has already been modified.
572 rc = bdb_index_entry_del( &op, tid, e );
574 snprintf( text->bv_val, text->bv_len,
575 "index_entry_del failed: %s (%d)",
576 db_strerror(rc), rc );
577 Debug( LDAP_DEBUG_ANY,
578 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
579 text->bv_val, 0, 0 );
584 rc = bdb_index_entry_add( &op, tid, e );
586 snprintf( text->bv_val, text->bv_len,
587 "index_entry_add failed: %s (%d)",
588 db_strerror(rc), rc );
589 Debug( LDAP_DEBUG_ANY,
590 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
591 text->bv_val, 0, 0 );
597 if (! (slapMode & SLAP_TOOL_QUICK)) {
598 rc = TXN_COMMIT( tid, 0 );
600 snprintf( text->bv_val, text->bv_len,
601 "txn_commit failed: %s (%d)",
602 db_strerror(rc), rc );
603 Debug( LDAP_DEBUG_ANY,
604 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
605 "%s\n", text->bv_val, 0, 0 );
611 if (! (slapMode & SLAP_TOOL_QUICK)) {
613 snprintf( text->bv_val, text->bv_len,
614 "txn_aborted! %s (%d)",
615 db_strerror(rc), rc );
616 Debug( LDAP_DEBUG_ANY,
617 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
618 text->bv_val, 0, 0 );