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 );
71 fprintf( stderr, "Error, entries missing!\n");
72 for (i=0; i<nholes; i++) {
73 fprintf(stderr, " entry %ld: %s\n",
74 holes[i].id, holes[i].dn.bv_val);
82 ID bdb_tool_entry_next(
87 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
90 assert( slapMode & SLAP_TOOL_MODE );
91 assert( bdb != NULL );
94 rc = bdb->bi_id2entry->bdi_db->cursor(
95 bdb->bi_id2entry->bdi_db, NULL, &cursor,
102 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
108 if( data.data == NULL ) {
112 AC_MEMCPY( &id, key.data, key.size );
116 ID bdb_tool_dn2id_get(
121 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
122 DB *db = bdb->bi_dn2id->bdi_db;
128 key.size = dn->bv_len + 2;
129 key.data = ch_malloc( key.size );
130 ((char*)key.data)[0] = DN_BASE_PREFIX;
131 AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
135 data.ulen = sizeof(ID);
136 data.flags = DB_DBT_USERMEM;
138 rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
142 LDAP_LOG ( INDEX, ERR, "bdb_tool_dn2id_get: get failed %s (%d)\n",
143 db_strerror(rc), rc, 0 );
145 Debug( LDAP_DEBUG_TRACE, "bdb_tool_dn2id_get: get failed: %s (%d)\n",
146 db_strerror( rc ), rc, 0 );
155 int bdb_tool_id2entry_get(
161 return bdb_id2entry( be, NULL, id, e );
164 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
170 assert( be != NULL );
171 assert( slapMode & SLAP_TOOL_MODE );
172 assert( data.data != NULL );
175 DBT2bv( &data, &bv );
177 rc = entry_decode( &bv, &e );
179 if( rc == LDAP_SUCCESS ) {
184 EntryInfo *ei = NULL;
188 op.o_tmpmemctx = NULL;
189 op.o_tmpmfuncs = &ch_mfuncs;
191 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
192 if ( rc == LDAP_SUCCESS )
199 static int bdb_tool_next_id(
206 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
207 struct berval dn = e->e_nname;
209 EntryInfo *ei = NULL;
212 rc = bdb_cache_find_ndn( op, tid, &dn, &ei );
213 if ( ei ) bdb_cache_entryinfo_unlock( ei );
214 if ( rc == DB_NOTFOUND ) {
215 if ( be_issuffix( op->o_bd, &dn ) ) {
218 dnParent( &dn, &pdn );
220 rc = bdb_tool_next_id( op, tid, e, text, 1 );
226 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
228 snprintf( text->bv_val, text->bv_len,
229 "next_id failed: %s (%d)",
230 db_strerror(rc), rc );
232 LDAP_LOG ( TOOLS, ERR,
233 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
235 Debug( LDAP_DEBUG_ANY,
236 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
240 rc = bdb_dn2id_add( op, tid, ei, e );
242 snprintf( text->bv_val, text->bv_len,
243 "dn2id_add failed: %s (%d)",
244 db_strerror(rc), rc );
246 LDAP_LOG ( TOOLS, ERR,
247 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
249 Debug( LDAP_DEBUG_ANY,
250 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
253 if ( nholes == nhmax - 1 ) {
254 if ( holes == hbuf ) {
255 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
256 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
258 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
262 ber_dupbv( &holes[nholes].dn, &dn );
263 holes[nholes++].id = e->e_id;
265 } else if ( !hole ) {
268 e->e_id = ei->bei_id;
270 for ( i=0; i<nholes; i++) {
271 if ( holes[i].id == e->e_id ) {
273 free(holes[i].dn.bv_val);
274 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
278 } else if ( holes[i].id > e->e_id ) {
286 ID bdb_tool_entry_put(
289 struct berval *text )
292 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
296 assert( be != NULL );
297 assert( slapMode & SLAP_TOOL_MODE );
300 assert( text->bv_val );
301 assert( text->bv_val[0] == '\0' ); /* overconservative? */
304 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
305 (long) e->e_id, e->e_dn, 0 );
307 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
308 (long) e->e_id, e->e_dn, 0 );
311 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
312 bdb->bi_db_opflags );
314 snprintf( text->bv_val, text->bv_len,
315 "txn_begin failed: %s (%d)",
316 db_strerror(rc), rc );
318 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
320 Debug( LDAP_DEBUG_ANY,
321 "=> bdb_tool_entry_put: %s\n",
322 text->bv_val, 0, 0 );
328 op.o_tmpmemctx = NULL;
329 op.o_tmpmfuncs = &ch_mfuncs;
331 /* add dn2id indices */
332 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
338 rc = bdb_id2entry_add( be, tid, e );
340 snprintf( text->bv_val, text->bv_len,
341 "id2entry_add failed: %s (%d)",
342 db_strerror(rc), rc );
344 LDAP_LOG ( TOOLS, ERR,
345 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
347 Debug( LDAP_DEBUG_ANY,
348 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
353 rc = bdb_index_entry_add( &op, tid, e );
355 snprintf( text->bv_val, text->bv_len,
356 "index_entry_add failed: %s (%d)",
357 db_strerror(rc), rc );
359 LDAP_LOG ( TOOLS, ERR,
360 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
362 Debug( LDAP_DEBUG_ANY,
363 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
370 rc = TXN_COMMIT( tid, 0 );
372 snprintf( text->bv_val, text->bv_len,
373 "txn_commit failed: %s (%d)",
374 db_strerror(rc), rc );
376 LDAP_LOG ( TOOLS, ERR,
377 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
379 Debug( LDAP_DEBUG_ANY,
380 "=> bdb_tool_entry_put: %s\n",
381 text->bv_val, 0, 0 );
388 snprintf( text->bv_val, text->bv_len,
389 "txn_aborted! %s (%d)",
390 db_strerror(rc), rc );
392 LDAP_LOG ( TOOLS, ERR,
393 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
395 Debug( LDAP_DEBUG_ANY,
396 "=> bdb_tool_entry_put: %s\n",
397 text->bv_val, 0, 0 );
405 int bdb_tool_entry_reindex(
409 struct bdb_info *bi = (struct bdb_info *) be->be_private;
416 LDAP_LOG ( TOOLS, ARGS,
417 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
419 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
423 e = bdb_tool_entry_get( be, id );
427 LDAP_LOG ( TOOLS, DETAIL1,
428 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
431 Debug( LDAP_DEBUG_ANY,
432 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
438 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
441 LDAP_LOG ( TOOLS, ERR,
442 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
443 db_strerror(rc), rc, 0 );
445 Debug( LDAP_DEBUG_ANY,
446 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
447 db_strerror(rc), rc, 0 );
453 * just (re)add them for now
454 * assume that some other routine (not yet implemented)
455 * will zap index databases
460 LDAP_LOG ( TOOLS, ERR,
461 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
463 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
464 (long) id, e->e_dn, 0 );
468 op.o_tmpmemctx = NULL;
469 op.o_tmpmfuncs = &ch_mfuncs;
472 /* add dn2id indices */
473 rc = bdb_dn2id_add( &op, tid, NULL, e );
474 if( rc != 0 && rc != DB_KEYEXIST ) {
476 LDAP_LOG ( TOOLS, ERR,
477 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
478 db_strerror(rc), rc, 0 );
480 Debug( LDAP_DEBUG_ANY,
481 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
482 db_strerror(rc), rc, 0 );
488 rc = bdb_index_entry_add( &op, tid, e );
492 rc = TXN_COMMIT( tid, 0 );
495 LDAP_LOG ( TOOLS, ERR,
496 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
497 db_strerror(rc), rc, 0 );
499 Debug( LDAP_DEBUG_ANY,
500 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
501 db_strerror(rc), rc, 0 );
509 LDAP_LOG ( TOOLS, DETAIL1,
510 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
511 db_strerror(rc), rc, 0 );
513 Debug( LDAP_DEBUG_ANY,
514 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
515 db_strerror(rc), rc, 0 );
519 bdb_entry_release( &op, e, 0 );
524 ID bdb_tool_entry_modify(
527 struct berval *text )
530 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
534 assert( be != NULL );
535 assert( slapMode & SLAP_TOOL_MODE );
538 assert( text->bv_val );
539 assert( text->bv_val[0] == '\0' ); /* overconservative? */
541 assert ( e->e_id != NOID );
542 assert ( e->e_id != 0 );
545 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
546 (long) e->e_id, e->e_dn, 0 );
548 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
549 (long) e->e_id, e->e_dn, 0 );
552 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
553 bdb->bi_db_opflags );
555 snprintf( text->bv_val, text->bv_len,
556 "txn_begin failed: %s (%d)",
557 db_strerror(rc), rc );
559 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
561 Debug( LDAP_DEBUG_ANY,
562 "=> bdb_tool_entry_put: %s\n",
563 text->bv_val, 0, 0 );
569 op.o_tmpmemctx = NULL;
570 op.o_tmpmfuncs = &ch_mfuncs;
573 rc = bdb_id2entry_update( be, tid, e );
575 snprintf( text->bv_val, text->bv_len,
576 "id2entry_add failed: %s (%d)",
577 db_strerror(rc), rc );
579 LDAP_LOG ( TOOLS, ERR,
580 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
582 Debug( LDAP_DEBUG_ANY,
583 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
588 rc = bdb_index_entry_del( &op, tid, e );
590 snprintf( text->bv_val, text->bv_len,
591 "index_entry_del failed: %s (%d)",
592 db_strerror(rc), rc );
594 LDAP_LOG ( TOOLS, ERR,
595 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
597 Debug( LDAP_DEBUG_ANY,
598 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
603 rc = bdb_index_entry_add( &op, tid, e );
605 snprintf( text->bv_val, text->bv_len,
606 "index_entry_add failed: %s (%d)",
607 db_strerror(rc), rc );
609 LDAP_LOG ( TOOLS, ERR,
610 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
612 Debug( LDAP_DEBUG_ANY,
613 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
620 rc = TXN_COMMIT( tid, 0 );
622 snprintf( text->bv_val, text->bv_len,
623 "txn_commit failed: %s (%d)",
624 db_strerror(rc), rc );
626 LDAP_LOG ( TOOLS, ERR,
627 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
629 Debug( LDAP_DEBUG_ANY,
630 "=> bdb_tool_entry_put: %s\n",
631 text->bv_val, 0, 0 );
638 snprintf( text->bv_val, text->bv_len,
639 "txn_aborted! %s (%d)",
640 db_strerror(rc), rc );
642 LDAP_LOG ( TOOLS, ERR,
643 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
645 Debug( LDAP_DEBUG_ANY,
646 "=> bdb_tool_entry_put: %s\n",
647 text->bv_val, 0, 0 );