1 /* tools.c - tools for slap tools */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
16 static DBC *cursor = NULL;
19 typedef struct dn_id {
24 #define HOLE_SIZE 4096
25 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
26 static unsigned nhmax = HOLE_SIZE;
27 static unsigned nholes;
29 int bdb_tool_entry_open(
30 BackendDB *be, int mode )
32 /* initialize key and data thangs */
35 key.flags = DB_DBT_REALLOC;
36 data.flags = DB_DBT_REALLOC;
41 int bdb_tool_entry_close(
56 cursor->c_close( cursor );
62 fprintf( stderr, "Error, entries missing!\n");
63 for (i=0; i<nholes; i++) {
64 fprintf(stderr, " entry %ld: %s\n",
65 holes[i].id, holes[i].dn.bv_val);
73 ID bdb_tool_entry_next(
78 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
81 assert( slapMode & SLAP_TOOL_MODE );
82 assert( bdb != NULL );
85 rc = bdb->bi_id2entry->bdi_db->cursor(
86 bdb->bi_id2entry->bdi_db, NULL, &cursor,
93 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
99 if( data.data == NULL ) {
103 AC_MEMCPY( &id, key.data, key.size );
107 ID bdb_tool_dn2id_get(
112 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
113 DB *db = bdb->bi_dn2id->bdi_db;
119 key.size = dn->bv_len + 2;
120 key.data = ch_malloc( key.size );
121 ((char*)key.data)[0] = DN_BASE_PREFIX;
122 AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
126 data.ulen = sizeof(ID);
127 data.flags = DB_DBT_USERMEM;
129 rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
133 LDAP_LOG ( INDEX, ERR, "bdb_tool_dn2id_get: get failed %s (%d)\n",
134 db_strerror(rc), rc, 0 );
136 Debug( LDAP_DEBUG_TRACE, "bdb_tool_dn2id_get: get failed: %s (%d)\n",
137 db_strerror( rc ), rc, 0 );
146 int bdb_tool_id2entry_get(
152 return bdb_id2entry( be, NULL, id, e );
155 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
161 assert( be != NULL );
162 assert( slapMode & SLAP_TOOL_MODE );
163 assert( data.data != NULL );
166 DBT2bv( &data, &bv );
168 rc = entry_decode( &bv, &e );
170 if( rc == LDAP_SUCCESS ) {
175 EntryInfo *ei = NULL;
179 op.o_tmpmemctx = NULL;
180 op.o_tmpmfuncs = &ch_mfuncs;
182 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
183 if ( rc == LDAP_SUCCESS )
190 static int bdb_tool_next_id(
198 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
199 struct berval dn = e->e_nname;
201 EntryInfo *ei = NULL;
204 rc = bdb_cache_find_ndn( op, tid, &dn, &ei, locker );
205 if ( ei ) bdb_cache_entryinfo_unlock( ei );
206 if ( rc == DB_NOTFOUND ) {
207 if ( be_issuffix( op->o_bd, &dn ) ) {
210 dnParent( &dn, &pdn );
212 rc = bdb_tool_next_id( op, tid, e, text, 1, locker );
217 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
219 snprintf( text->bv_val, text->bv_len,
220 "next_id failed: %s (%d)",
221 db_strerror(rc), rc );
223 LDAP_LOG ( TOOLS, ERR,
224 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
226 Debug( LDAP_DEBUG_ANY,
227 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
232 rc = bdb_dn2id_add( op, tid, ei, e );
234 snprintf( text->bv_val, text->bv_len,
235 "dn2id_add failed: %s (%d)",
236 db_strerror(rc), rc );
238 LDAP_LOG ( TOOLS, ERR,
239 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
241 Debug( LDAP_DEBUG_ANY,
242 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
245 if ( nholes == nhmax - 1 ) {
246 if ( holes == hbuf ) {
247 holes = ch_malloc( nhmax * sizeof(ID) * 2 );
248 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
250 holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
254 ber_dupbv( &holes[nholes].dn, &dn );
255 holes[nholes++].id = e->e_id;
257 } else if ( !hole ) {
260 for ( i=0; i<nholes; i++) {
261 if ( holes[i].id == e->e_id ) {
263 free(holes[i].dn.bv_val);
264 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
268 } else if ( holes[i].id > e->e_id ) {
276 ID bdb_tool_entry_put(
279 struct berval *text )
282 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
287 assert( be != NULL );
288 assert( slapMode & SLAP_TOOL_MODE );
291 assert( text->bv_val );
292 assert( text->bv_val[0] == '\0' ); /* overconservative? */
295 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
296 (long) e->e_id, e->e_dn, 0 );
298 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
299 (long) e->e_id, e->e_dn, 0 );
302 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
303 bdb->bi_db_opflags );
305 snprintf( text->bv_val, text->bv_len,
306 "txn_begin failed: %s (%d)",
307 db_strerror(rc), rc );
309 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
311 Debug( LDAP_DEBUG_ANY,
312 "=> bdb_tool_entry_put: %s\n",
313 text->bv_val, 0, 0 );
319 op.o_tmpmemctx = NULL;
320 op.o_tmpmfuncs = &ch_mfuncs;
322 locker = TXN_ID( tid );
323 /* add dn2id indices */
324 rc = bdb_tool_next_id( &op, tid, e, text, 0, locker );
330 rc = bdb_id2entry_add( be, tid, e );
332 snprintf( text->bv_val, text->bv_len,
333 "id2entry_add failed: %s (%d)",
334 db_strerror(rc), rc );
336 LDAP_LOG ( TOOLS, ERR,
337 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
339 Debug( LDAP_DEBUG_ANY,
340 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
345 rc = bdb_index_entry_add( &op, tid, e );
347 snprintf( text->bv_val, text->bv_len,
348 "index_entry_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 );
362 rc = TXN_COMMIT( tid, 0 );
364 snprintf( text->bv_val, text->bv_len,
365 "txn_commit failed: %s (%d)",
366 db_strerror(rc), rc );
368 LDAP_LOG ( TOOLS, ERR,
369 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
371 Debug( LDAP_DEBUG_ANY,
372 "=> bdb_tool_entry_put: %s\n",
373 text->bv_val, 0, 0 );
380 snprintf( text->bv_val, text->bv_len,
381 "txn_aborted! %s (%d)",
382 db_strerror(rc), rc );
384 LDAP_LOG ( TOOLS, ERR,
385 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
387 Debug( LDAP_DEBUG_ANY,
388 "=> bdb_tool_entry_put: %s\n",
389 text->bv_val, 0, 0 );
397 int bdb_tool_entry_reindex(
401 struct bdb_info *bi = (struct bdb_info *) be->be_private;
408 LDAP_LOG ( TOOLS, ARGS,
409 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
411 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
415 e = bdb_tool_entry_get( be, id );
419 LDAP_LOG ( TOOLS, DETAIL1,
420 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
423 Debug( LDAP_DEBUG_ANY,
424 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
430 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
433 LDAP_LOG ( TOOLS, ERR,
434 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
435 db_strerror(rc), rc, 0 );
437 Debug( LDAP_DEBUG_ANY,
438 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
439 db_strerror(rc), rc, 0 );
445 * just (re)add them for now
446 * assume that some other routine (not yet implemented)
447 * will zap index databases
452 LDAP_LOG ( TOOLS, ERR,
453 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
455 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
456 (long) id, e->e_dn, 0 );
460 op.o_tmpmemctx = NULL;
461 op.o_tmpmfuncs = &ch_mfuncs;
464 /* add dn2id indices */
465 rc = bdb_dn2id_add( &op, tid, NULL, e );
466 if( rc != 0 && rc != DB_KEYEXIST ) {
468 LDAP_LOG ( TOOLS, ERR,
469 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
470 db_strerror(rc), rc, 0 );
472 Debug( LDAP_DEBUG_ANY,
473 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
474 db_strerror(rc), rc, 0 );
480 rc = bdb_index_entry_add( &op, tid, e );
484 rc = TXN_COMMIT( tid, 0 );
487 LDAP_LOG ( TOOLS, ERR,
488 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
489 db_strerror(rc), rc, 0 );
491 Debug( LDAP_DEBUG_ANY,
492 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
493 db_strerror(rc), rc, 0 );
501 LDAP_LOG ( TOOLS, DETAIL1,
502 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
503 db_strerror(rc), rc, 0 );
505 Debug( LDAP_DEBUG_ANY,
506 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
507 db_strerror(rc), rc, 0 );
511 bdb_entry_release( &op, e, 0 );
516 ID bdb_tool_entry_modify(
519 struct berval *text )
522 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 );
534 assert ( e->e_id != 0 );
537 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
538 (long) e->e_id, e->e_dn, 0 );
540 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
541 (long) e->e_id, e->e_dn, 0 );
544 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
545 bdb->bi_db_opflags );
547 snprintf( text->bv_val, text->bv_len,
548 "txn_begin failed: %s (%d)",
549 db_strerror(rc), rc );
551 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
553 Debug( LDAP_DEBUG_ANY,
554 "=> bdb_tool_entry_put: %s\n",
555 text->bv_val, 0, 0 );
561 op.o_tmpmemctx = NULL;
562 op.o_tmpmfuncs = &ch_mfuncs;
565 rc = bdb_id2entry_update( be, tid, e );
567 snprintf( text->bv_val, text->bv_len,
568 "id2entry_add failed: %s (%d)",
569 db_strerror(rc), rc );
571 LDAP_LOG ( TOOLS, ERR,
572 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
574 Debug( LDAP_DEBUG_ANY,
575 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
580 rc = bdb_index_entry_del( &op, tid, e );
582 snprintf( text->bv_val, text->bv_len,
583 "index_entry_del 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_add( &op, tid, e );
597 snprintf( text->bv_val, text->bv_len,
598 "index_entry_add 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 );
612 rc = TXN_COMMIT( tid, 0 );
614 snprintf( text->bv_val, text->bv_len,
615 "txn_commit failed: %s (%d)",
616 db_strerror(rc), rc );
618 LDAP_LOG ( TOOLS, ERR,
619 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
621 Debug( LDAP_DEBUG_ANY,
622 "=> bdb_tool_entry_put: %s\n",
623 text->bv_val, 0, 0 );
630 snprintf( text->bv_val, text->bv_len,
631 "txn_aborted! %s (%d)",
632 db_strerror(rc), rc );
634 LDAP_LOG ( TOOLS, ERR,
635 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
637 Debug( LDAP_DEBUG_ANY,
638 "=> bdb_tool_entry_put: %s\n",
639 text->bv_val, 0, 0 );