1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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 int bdb_tool_entry_open(
39 BackendDB *be, int mode )
41 /* initialize key and data thangs */
44 key.flags = DB_DBT_REALLOC;
45 data.flags = DB_DBT_REALLOC;
50 int bdb_tool_entry_close(
65 cursor->c_close( cursor );
72 for (i=0; i<nholes; i++) {
73 if (holes[i].dn.bv_len) {
76 fprintf( stderr, "Error, entries missing!\n");
79 fprintf(stderr, " entry %ld: %s\n",
80 holes[i].id, holes[i].dn.bv_val);
89 ID bdb_tool_entry_next(
94 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
97 assert( slapMode & SLAP_TOOL_MODE );
98 assert( bdb != NULL );
100 if (cursor == NULL) {
101 rc = bdb->bi_id2entry->bdi_db->cursor(
102 bdb->bi_id2entry->bdi_db, NULL, &cursor,
103 bdb->bi_db_opflags );
109 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
115 if( data.data == NULL ) {
119 AC_MEMCPY( &id, key.data, key.size );
123 ID bdb_tool_dn2id_get(
128 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
129 DB *db = bdb->bi_dn2id->bdi_db;
135 key.size = dn->bv_len + 2;
136 key.data = ch_malloc( key.size );
137 ((char*)key.data)[0] = DN_BASE_PREFIX;
138 AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
142 data.ulen = sizeof(ID);
143 data.flags = DB_DBT_USERMEM;
145 rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
149 LDAP_LOG ( INDEX, ERR, "bdb_tool_dn2id_get: get failed %s (%d)\n",
150 db_strerror(rc), rc, 0 );
152 Debug( LDAP_DEBUG_TRACE, "bdb_tool_dn2id_get: get failed: %s (%d)\n",
153 db_strerror( rc ), rc, 0 );
162 int bdb_tool_id2entry_get(
168 return bdb_id2entry( be, NULL, id, e );
171 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
177 assert( be != NULL );
178 assert( slapMode & SLAP_TOOL_MODE );
179 assert( data.data != NULL );
182 DBT2bv( &data, &bv );
184 rc = entry_decode( &bv, &e );
186 if( rc == LDAP_SUCCESS ) {
191 EntryInfo *ei = NULL;
195 op.o_tmpmemctx = NULL;
196 op.o_tmpmfuncs = &ch_mfuncs;
198 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
199 if ( rc == LDAP_SUCCESS )
206 static int bdb_tool_next_id(
213 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
214 struct berval dn = e->e_nname;
216 EntryInfo *ei = NULL;
219 rc = bdb_cache_find_ndn( op, tid, &dn, &ei );
220 if ( ei ) bdb_cache_entryinfo_unlock( ei );
221 if ( rc == DB_NOTFOUND ) {
222 if ( be_issuffix( op->o_bd, &dn ) ) {
225 dnParent( &dn, &pdn );
227 rc = bdb_tool_next_id( op, tid, e, text, 1 );
233 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
235 snprintf( text->bv_val, text->bv_len,
236 "next_id failed: %s (%d)",
237 db_strerror(rc), rc );
239 LDAP_LOG ( TOOLS, ERR,
240 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
242 Debug( LDAP_DEBUG_ANY,
243 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
247 rc = bdb_dn2id_add( op, tid, ei, e );
249 snprintf( text->bv_val, text->bv_len,
250 "dn2id_add failed: %s (%d)",
251 db_strerror(rc), rc );
253 LDAP_LOG ( TOOLS, ERR,
254 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
256 Debug( LDAP_DEBUG_ANY,
257 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
260 if ( nholes == nhmax - 1 ) {
261 if ( holes == hbuf ) {
262 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
263 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
265 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
269 ber_dupbv( &holes[nholes].dn, &dn );
270 holes[nholes++].id = e->e_id;
272 } else if ( !hole ) {
275 e->e_id = ei->bei_id;
277 for ( i=0; i<nholes; i++) {
278 if ( holes[i].id == e->e_id ) {
280 free(holes[i].dn.bv_val);
281 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
285 } else if ( holes[i].id > e->e_id ) {
293 ID bdb_tool_entry_put(
296 struct berval *text )
299 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
303 assert( be != NULL );
304 assert( slapMode & SLAP_TOOL_MODE );
307 assert( text->bv_val );
308 assert( text->bv_val[0] == '\0' ); /* overconservative? */
311 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
312 (long) e->e_id, e->e_dn, 0 );
314 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
315 (long) e->e_id, e->e_dn, 0 );
318 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
319 bdb->bi_db_opflags );
321 snprintf( text->bv_val, text->bv_len,
322 "txn_begin failed: %s (%d)",
323 db_strerror(rc), rc );
325 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
327 Debug( LDAP_DEBUG_ANY,
328 "=> bdb_tool_entry_put: %s\n",
329 text->bv_val, 0, 0 );
335 op.o_tmpmemctx = NULL;
336 op.o_tmpmfuncs = &ch_mfuncs;
338 /* add dn2id indices */
339 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
345 rc = bdb_id2entry_add( be, tid, e );
347 snprintf( text->bv_val, text->bv_len,
348 "id2entry_add failed: %s (%d)",
349 db_strerror(rc), rc );
351 LDAP_LOG ( TOOLS, ERR,
352 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
354 Debug( LDAP_DEBUG_ANY,
355 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
360 rc = bdb_index_entry_add( &op, tid, e );
362 snprintf( text->bv_val, text->bv_len,
363 "index_entry_add failed: %s (%d)",
364 db_strerror(rc), rc );
366 LDAP_LOG ( TOOLS, ERR,
367 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
369 Debug( LDAP_DEBUG_ANY,
370 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
377 rc = TXN_COMMIT( tid, 0 );
379 snprintf( text->bv_val, text->bv_len,
380 "txn_commit failed: %s (%d)",
381 db_strerror(rc), rc );
383 LDAP_LOG ( TOOLS, ERR,
384 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
386 Debug( LDAP_DEBUG_ANY,
387 "=> bdb_tool_entry_put: %s\n",
388 text->bv_val, 0, 0 );
395 snprintf( text->bv_val, text->bv_len,
396 "txn_aborted! %s (%d)",
397 db_strerror(rc), rc );
399 LDAP_LOG ( TOOLS, ERR,
400 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
402 Debug( LDAP_DEBUG_ANY,
403 "=> bdb_tool_entry_put: %s\n",
404 text->bv_val, 0, 0 );
412 int bdb_tool_entry_reindex(
416 struct bdb_info *bi = (struct bdb_info *) be->be_private;
423 LDAP_LOG ( TOOLS, ARGS,
424 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
426 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
430 e = bdb_tool_entry_get( be, id );
434 LDAP_LOG ( TOOLS, DETAIL1,
435 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
438 Debug( LDAP_DEBUG_ANY,
439 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
445 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
448 LDAP_LOG ( TOOLS, ERR,
449 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
450 db_strerror(rc), rc, 0 );
452 Debug( LDAP_DEBUG_ANY,
453 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
454 db_strerror(rc), rc, 0 );
460 * just (re)add them for now
461 * assume that some other routine (not yet implemented)
462 * will zap index databases
467 LDAP_LOG ( TOOLS, ERR,
468 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
470 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
471 (long) id, e->e_dn, 0 );
475 op.o_tmpmemctx = NULL;
476 op.o_tmpmfuncs = &ch_mfuncs;
479 /* add dn2id indices */
480 rc = bdb_dn2id_add( &op, tid, NULL, e );
481 if( rc != 0 && rc != DB_KEYEXIST ) {
483 LDAP_LOG ( TOOLS, ERR,
484 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
485 db_strerror(rc), rc, 0 );
487 Debug( LDAP_DEBUG_ANY,
488 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
489 db_strerror(rc), rc, 0 );
495 rc = bdb_index_entry_add( &op, tid, e );
499 rc = TXN_COMMIT( tid, 0 );
502 LDAP_LOG ( TOOLS, ERR,
503 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
504 db_strerror(rc), rc, 0 );
506 Debug( LDAP_DEBUG_ANY,
507 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
508 db_strerror(rc), rc, 0 );
516 LDAP_LOG ( TOOLS, DETAIL1,
517 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
518 db_strerror(rc), rc, 0 );
520 Debug( LDAP_DEBUG_ANY,
521 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
522 db_strerror(rc), rc, 0 );
526 bdb_entry_release( &op, e, 0 );
531 ID bdb_tool_entry_modify(
534 struct berval *text )
537 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
541 assert( be != NULL );
542 assert( slapMode & SLAP_TOOL_MODE );
545 assert( text->bv_val );
546 assert( text->bv_val[0] == '\0' ); /* overconservative? */
548 assert ( e->e_id != NOID );
549 assert ( e->e_id != 0 );
552 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
553 (long) e->e_id, e->e_dn, 0 );
555 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
556 (long) e->e_id, e->e_dn, 0 );
559 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
560 bdb->bi_db_opflags );
562 snprintf( text->bv_val, text->bv_len,
563 "txn_begin failed: %s (%d)",
564 db_strerror(rc), rc );
566 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
568 Debug( LDAP_DEBUG_ANY,
569 "=> bdb_tool_entry_put: %s\n",
570 text->bv_val, 0, 0 );
576 op.o_tmpmemctx = NULL;
577 op.o_tmpmfuncs = &ch_mfuncs;
580 rc = bdb_id2entry_update( be, tid, e );
582 snprintf( text->bv_val, text->bv_len,
583 "id2entry_add failed: %s (%d)",
584 db_strerror(rc), rc );
586 LDAP_LOG ( TOOLS, ERR,
587 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
589 Debug( LDAP_DEBUG_ANY,
590 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
595 rc = bdb_index_entry_del( &op, tid, e );
597 snprintf( text->bv_val, text->bv_len,
598 "index_entry_del failed: %s (%d)",
599 db_strerror(rc), rc );
601 LDAP_LOG ( TOOLS, ERR,
602 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
604 Debug( LDAP_DEBUG_ANY,
605 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
610 rc = bdb_index_entry_add( &op, tid, e );
612 snprintf( text->bv_val, text->bv_len,
613 "index_entry_add failed: %s (%d)",
614 db_strerror(rc), rc );
616 LDAP_LOG ( TOOLS, ERR,
617 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
619 Debug( LDAP_DEBUG_ANY,
620 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
627 rc = TXN_COMMIT( tid, 0 );
629 snprintf( text->bv_val, text->bv_len,
630 "txn_commit failed: %s (%d)",
631 db_strerror(rc), rc );
633 LDAP_LOG ( TOOLS, ERR,
634 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
636 Debug( LDAP_DEBUG_ANY,
637 "=> bdb_tool_entry_put: %s\n",
638 text->bv_val, 0, 0 );
645 snprintf( text->bv_val, text->bv_len,
646 "txn_aborted! %s (%d)",
647 db_strerror(rc), rc );
649 LDAP_LOG ( TOOLS, ERR,
650 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
652 Debug( LDAP_DEBUG_ANY,
653 "=> bdb_tool_entry_put: %s\n",
654 text->bv_val, 0, 0 );