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 );
225 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
227 snprintf( text->bv_val, text->bv_len,
228 "next_id failed: %s (%d)",
229 db_strerror(rc), rc );
231 LDAP_LOG ( TOOLS, ERR,
232 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
234 Debug( LDAP_DEBUG_ANY,
235 "=> 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(ID) * 2 );
256 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
258 holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
262 ber_dupbv( &holes[nholes].dn, &dn );
263 holes[nholes++].id = e->e_id;
265 } else if ( !hole ) {
268 for ( i=0; i<nholes; i++) {
269 if ( holes[i].id == e->e_id ) {
271 free(holes[i].dn.bv_val);
272 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
276 } else if ( holes[i].id > e->e_id ) {
284 ID bdb_tool_entry_put(
287 struct berval *text )
290 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
294 assert( be != NULL );
295 assert( slapMode & SLAP_TOOL_MODE );
298 assert( text->bv_val );
299 assert( text->bv_val[0] == '\0' ); /* overconservative? */
302 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
303 (long) e->e_id, e->e_dn, 0 );
305 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
306 (long) e->e_id, e->e_dn, 0 );
309 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
310 bdb->bi_db_opflags );
312 snprintf( text->bv_val, text->bv_len,
313 "txn_begin failed: %s (%d)",
314 db_strerror(rc), rc );
316 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
318 Debug( LDAP_DEBUG_ANY,
319 "=> bdb_tool_entry_put: %s\n",
320 text->bv_val, 0, 0 );
326 op.o_tmpmemctx = NULL;
327 op.o_tmpmfuncs = &ch_mfuncs;
329 /* add dn2id indices */
330 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
336 rc = bdb_id2entry_add( be, tid, e );
338 snprintf( text->bv_val, text->bv_len,
339 "id2entry_add failed: %s (%d)",
340 db_strerror(rc), rc );
342 LDAP_LOG ( TOOLS, ERR,
343 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
345 Debug( LDAP_DEBUG_ANY,
346 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
351 rc = bdb_index_entry_add( &op, tid, e );
353 snprintf( text->bv_val, text->bv_len,
354 "index_entry_add failed: %s (%d)",
355 db_strerror(rc), rc );
357 LDAP_LOG ( TOOLS, ERR,
358 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
360 Debug( LDAP_DEBUG_ANY,
361 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
368 rc = TXN_COMMIT( tid, 0 );
370 snprintf( text->bv_val, text->bv_len,
371 "txn_commit failed: %s (%d)",
372 db_strerror(rc), rc );
374 LDAP_LOG ( TOOLS, ERR,
375 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
377 Debug( LDAP_DEBUG_ANY,
378 "=> bdb_tool_entry_put: %s\n",
379 text->bv_val, 0, 0 );
386 snprintf( text->bv_val, text->bv_len,
387 "txn_aborted! %s (%d)",
388 db_strerror(rc), rc );
390 LDAP_LOG ( TOOLS, ERR,
391 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
393 Debug( LDAP_DEBUG_ANY,
394 "=> bdb_tool_entry_put: %s\n",
395 text->bv_val, 0, 0 );
403 int bdb_tool_entry_reindex(
407 struct bdb_info *bi = (struct bdb_info *) be->be_private;
414 LDAP_LOG ( TOOLS, ARGS,
415 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
417 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
421 e = bdb_tool_entry_get( be, id );
425 LDAP_LOG ( TOOLS, DETAIL1,
426 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
429 Debug( LDAP_DEBUG_ANY,
430 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
436 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
439 LDAP_LOG ( TOOLS, ERR,
440 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
441 db_strerror(rc), rc, 0 );
443 Debug( LDAP_DEBUG_ANY,
444 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
445 db_strerror(rc), rc, 0 );
451 * just (re)add them for now
452 * assume that some other routine (not yet implemented)
453 * will zap index databases
458 LDAP_LOG ( TOOLS, ERR,
459 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
461 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
462 (long) id, e->e_dn, 0 );
466 op.o_tmpmemctx = NULL;
467 op.o_tmpmfuncs = &ch_mfuncs;
470 /* add dn2id indices */
471 rc = bdb_dn2id_add( &op, tid, NULL, e );
472 if( rc != 0 && rc != DB_KEYEXIST ) {
474 LDAP_LOG ( TOOLS, ERR,
475 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
476 db_strerror(rc), rc, 0 );
478 Debug( LDAP_DEBUG_ANY,
479 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
480 db_strerror(rc), rc, 0 );
486 rc = bdb_index_entry_add( &op, tid, e );
490 rc = TXN_COMMIT( tid, 0 );
493 LDAP_LOG ( TOOLS, ERR,
494 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
495 db_strerror(rc), rc, 0 );
497 Debug( LDAP_DEBUG_ANY,
498 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
499 db_strerror(rc), rc, 0 );
507 LDAP_LOG ( TOOLS, DETAIL1,
508 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
509 db_strerror(rc), rc, 0 );
511 Debug( LDAP_DEBUG_ANY,
512 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
513 db_strerror(rc), rc, 0 );
517 bdb_entry_release( &op, e, 0 );
522 ID bdb_tool_entry_modify(
525 struct berval *text )
528 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
532 assert( be != NULL );
533 assert( slapMode & SLAP_TOOL_MODE );
536 assert( text->bv_val );
537 assert( text->bv_val[0] == '\0' ); /* overconservative? */
539 assert ( e->e_id != NOID );
540 assert ( e->e_id != 0 );
543 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
544 (long) e->e_id, e->e_dn, 0 );
546 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
547 (long) e->e_id, e->e_dn, 0 );
550 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
551 bdb->bi_db_opflags );
553 snprintf( text->bv_val, text->bv_len,
554 "txn_begin failed: %s (%d)",
555 db_strerror(rc), rc );
557 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
559 Debug( LDAP_DEBUG_ANY,
560 "=> bdb_tool_entry_put: %s\n",
561 text->bv_val, 0, 0 );
567 op.o_tmpmemctx = NULL;
568 op.o_tmpmfuncs = &ch_mfuncs;
571 rc = bdb_id2entry_update( be, tid, e );
573 snprintf( text->bv_val, text->bv_len,
574 "id2entry_add failed: %s (%d)",
575 db_strerror(rc), rc );
577 LDAP_LOG ( TOOLS, ERR,
578 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
580 Debug( LDAP_DEBUG_ANY,
581 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
586 rc = bdb_index_entry_del( &op, tid, e );
588 snprintf( text->bv_val, text->bv_len,
589 "index_entry_del failed: %s (%d)",
590 db_strerror(rc), rc );
592 LDAP_LOG ( TOOLS, ERR,
593 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
595 Debug( LDAP_DEBUG_ANY,
596 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
601 rc = bdb_index_entry_add( &op, tid, e );
603 snprintf( text->bv_val, text->bv_len,
604 "index_entry_add failed: %s (%d)",
605 db_strerror(rc), rc );
607 LDAP_LOG ( TOOLS, ERR,
608 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
610 Debug( LDAP_DEBUG_ANY,
611 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
618 rc = TXN_COMMIT( tid, 0 );
620 snprintf( text->bv_val, text->bv_len,
621 "txn_commit failed: %s (%d)",
622 db_strerror(rc), rc );
624 LDAP_LOG ( TOOLS, ERR,
625 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
627 Debug( LDAP_DEBUG_ANY,
628 "=> bdb_tool_entry_put: %s\n",
629 text->bv_val, 0, 0 );
636 snprintf( text->bv_val, text->bv_len,
637 "txn_aborted! %s (%d)",
638 db_strerror(rc), rc );
640 LDAP_LOG ( TOOLS, ERR,
641 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
643 Debug( LDAP_DEBUG_ANY,
644 "=> bdb_tool_entry_put: %s\n",
645 text->bv_val, 0, 0 );