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(
197 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
198 struct berval dn = e->e_nname;
200 EntryInfo *ei = NULL;
203 rc = bdb_cache_find_ndn( op, tid, &dn, &ei );
204 if ( ei ) bdb_cache_entryinfo_unlock( ei );
205 if ( rc == DB_NOTFOUND ) {
206 if ( be_issuffix( op->o_bd, &dn ) ) {
209 dnParent( &dn, &pdn );
211 rc = bdb_tool_next_id( op, tid, e, text, 1 );
216 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
218 snprintf( text->bv_val, text->bv_len,
219 "next_id failed: %s (%d)",
220 db_strerror(rc), rc );
222 LDAP_LOG ( TOOLS, ERR,
223 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
225 Debug( LDAP_DEBUG_ANY,
226 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
231 rc = bdb_dn2id_add( op, tid, ei, e );
233 snprintf( text->bv_val, text->bv_len,
234 "dn2id_add failed: %s (%d)",
235 db_strerror(rc), rc );
237 LDAP_LOG ( TOOLS, ERR,
238 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
240 Debug( LDAP_DEBUG_ANY,
241 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
244 if ( nholes == nhmax - 1 ) {
245 if ( holes == hbuf ) {
246 holes = ch_malloc( nhmax * sizeof(ID) * 2 );
247 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
249 holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
253 ber_dupbv( &holes[nholes].dn, &dn );
254 holes[nholes++].id = e->e_id;
256 } else if ( !hole ) {
259 for ( i=0; i<nholes; i++) {
260 if ( holes[i].id == e->e_id ) {
262 free(holes[i].dn.bv_val);
263 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
267 } else if ( holes[i].id > e->e_id ) {
275 ID bdb_tool_entry_put(
278 struct berval *text )
281 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
285 assert( be != NULL );
286 assert( slapMode & SLAP_TOOL_MODE );
289 assert( text->bv_val );
290 assert( text->bv_val[0] == '\0' ); /* overconservative? */
293 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
294 (long) e->e_id, e->e_dn, 0 );
296 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
297 (long) e->e_id, e->e_dn, 0 );
300 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
301 bdb->bi_db_opflags );
303 snprintf( text->bv_val, text->bv_len,
304 "txn_begin failed: %s (%d)",
305 db_strerror(rc), rc );
307 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
309 Debug( LDAP_DEBUG_ANY,
310 "=> bdb_tool_entry_put: %s\n",
311 text->bv_val, 0, 0 );
317 op.o_tmpmemctx = NULL;
318 op.o_tmpmfuncs = &ch_mfuncs;
320 /* add dn2id indices */
321 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
327 rc = bdb_id2entry_add( be, tid, e );
329 snprintf( text->bv_val, text->bv_len,
330 "id2entry_add failed: %s (%d)",
331 db_strerror(rc), rc );
333 LDAP_LOG ( TOOLS, ERR,
334 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
336 Debug( LDAP_DEBUG_ANY,
337 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
342 rc = bdb_index_entry_add( &op, tid, e );
344 snprintf( text->bv_val, text->bv_len,
345 "index_entry_add failed: %s (%d)",
346 db_strerror(rc), rc );
348 LDAP_LOG ( TOOLS, ERR,
349 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
351 Debug( LDAP_DEBUG_ANY,
352 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
359 rc = TXN_COMMIT( tid, 0 );
361 snprintf( text->bv_val, text->bv_len,
362 "txn_commit failed: %s (%d)",
363 db_strerror(rc), rc );
365 LDAP_LOG ( TOOLS, ERR,
366 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
368 Debug( LDAP_DEBUG_ANY,
369 "=> bdb_tool_entry_put: %s\n",
370 text->bv_val, 0, 0 );
377 snprintf( text->bv_val, text->bv_len,
378 "txn_aborted! %s (%d)",
379 db_strerror(rc), rc );
381 LDAP_LOG ( TOOLS, ERR,
382 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
384 Debug( LDAP_DEBUG_ANY,
385 "=> bdb_tool_entry_put: %s\n",
386 text->bv_val, 0, 0 );
394 int bdb_tool_entry_reindex(
398 struct bdb_info *bi = (struct bdb_info *) be->be_private;
405 LDAP_LOG ( TOOLS, ARGS,
406 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
408 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
412 e = bdb_tool_entry_get( be, id );
416 LDAP_LOG ( TOOLS, DETAIL1,
417 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
420 Debug( LDAP_DEBUG_ANY,
421 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
427 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
430 LDAP_LOG ( TOOLS, ERR,
431 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
432 db_strerror(rc), rc, 0 );
434 Debug( LDAP_DEBUG_ANY,
435 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
436 db_strerror(rc), rc, 0 );
442 * just (re)add them for now
443 * assume that some other routine (not yet implemented)
444 * will zap index databases
449 LDAP_LOG ( TOOLS, ERR,
450 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
452 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
453 (long) id, e->e_dn, 0 );
457 op.o_tmpmemctx = NULL;
458 op.o_tmpmfuncs = &ch_mfuncs;
461 /* add dn2id indices */
462 rc = bdb_dn2id_add( &op, tid, NULL, e );
463 if( rc != 0 && rc != DB_KEYEXIST ) {
465 LDAP_LOG ( TOOLS, ERR,
466 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
467 db_strerror(rc), rc, 0 );
469 Debug( LDAP_DEBUG_ANY,
470 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
471 db_strerror(rc), rc, 0 );
477 rc = bdb_index_entry_add( &op, tid, e );
481 rc = TXN_COMMIT( tid, 0 );
484 LDAP_LOG ( TOOLS, ERR,
485 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
486 db_strerror(rc), rc, 0 );
488 Debug( LDAP_DEBUG_ANY,
489 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
490 db_strerror(rc), rc, 0 );
498 LDAP_LOG ( TOOLS, DETAIL1,
499 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
500 db_strerror(rc), rc, 0 );
502 Debug( LDAP_DEBUG_ANY,
503 "=> bdb_tool_entry_reindex: 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;
523 assert( be != NULL );
524 assert( slapMode & SLAP_TOOL_MODE );
527 assert( text->bv_val );
528 assert( text->bv_val[0] == '\0' ); /* overconservative? */
530 assert ( e->e_id != NOID );
531 assert ( e->e_id != 0 );
534 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
535 (long) e->e_id, e->e_dn, 0 );
537 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
538 (long) e->e_id, e->e_dn, 0 );
541 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
542 bdb->bi_db_opflags );
544 snprintf( text->bv_val, text->bv_len,
545 "txn_begin failed: %s (%d)",
546 db_strerror(rc), rc );
548 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
550 Debug( LDAP_DEBUG_ANY,
551 "=> bdb_tool_entry_put: %s\n",
552 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 );
568 LDAP_LOG ( TOOLS, ERR,
569 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
571 Debug( LDAP_DEBUG_ANY,
572 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
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 );
583 LDAP_LOG ( TOOLS, ERR,
584 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
586 Debug( LDAP_DEBUG_ANY,
587 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
592 rc = bdb_index_entry_add( &op, tid, e );
594 snprintf( text->bv_val, text->bv_len,
595 "index_entry_add failed: %s (%d)",
596 db_strerror(rc), rc );
598 LDAP_LOG ( TOOLS, ERR,
599 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
601 Debug( LDAP_DEBUG_ANY,
602 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
609 rc = TXN_COMMIT( tid, 0 );
611 snprintf( text->bv_val, text->bv_len,
612 "txn_commit failed: %s (%d)",
613 db_strerror(rc), rc );
615 LDAP_LOG ( TOOLS, ERR,
616 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
618 Debug( LDAP_DEBUG_ANY,
619 "=> bdb_tool_entry_put: %s\n",
620 text->bv_val, 0, 0 );
627 snprintf( text->bv_val, text->bv_len,
628 "txn_aborted! %s (%d)",
629 db_strerror(rc), rc );
631 LDAP_LOG ( TOOLS, ERR,
632 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
634 Debug( LDAP_DEBUG_ANY,
635 "=> bdb_tool_entry_put: %s\n",
636 text->bv_val, 0, 0 );