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 if ( e->e_id == NOID ) {
272 for ( i=0; i<nholes; i++) {
273 if ( holes[i].id == e->e_id ) {
275 free(holes[i].dn.bv_val);
276 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
280 } else if ( holes[i].id > e->e_id ) {
288 ID bdb_tool_entry_put(
291 struct berval *text )
294 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
298 assert( be != NULL );
299 assert( slapMode & SLAP_TOOL_MODE );
302 assert( text->bv_val );
303 assert( text->bv_val[0] == '\0' ); /* overconservative? */
306 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
307 (long) e->e_id, e->e_dn, 0 );
309 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
310 (long) e->e_id, e->e_dn, 0 );
313 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
314 bdb->bi_db_opflags );
316 snprintf( text->bv_val, text->bv_len,
317 "txn_begin failed: %s (%d)",
318 db_strerror(rc), rc );
320 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
322 Debug( LDAP_DEBUG_ANY,
323 "=> bdb_tool_entry_put: %s\n",
324 text->bv_val, 0, 0 );
330 op.o_tmpmemctx = NULL;
331 op.o_tmpmfuncs = &ch_mfuncs;
333 /* add dn2id indices */
334 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
340 rc = bdb_id2entry_add( be, tid, e );
342 snprintf( text->bv_val, text->bv_len,
343 "id2entry_add failed: %s (%d)",
344 db_strerror(rc), rc );
346 LDAP_LOG ( TOOLS, ERR,
347 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
349 Debug( LDAP_DEBUG_ANY,
350 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
355 rc = bdb_index_entry_add( &op, tid, e );
357 snprintf( text->bv_val, text->bv_len,
358 "index_entry_add failed: %s (%d)",
359 db_strerror(rc), rc );
361 LDAP_LOG ( TOOLS, ERR,
362 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
364 Debug( LDAP_DEBUG_ANY,
365 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
372 rc = TXN_COMMIT( tid, 0 );
374 snprintf( text->bv_val, text->bv_len,
375 "txn_commit failed: %s (%d)",
376 db_strerror(rc), rc );
378 LDAP_LOG ( TOOLS, ERR,
379 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
381 Debug( LDAP_DEBUG_ANY,
382 "=> bdb_tool_entry_put: %s\n",
383 text->bv_val, 0, 0 );
390 snprintf( text->bv_val, text->bv_len,
391 "txn_aborted! %s (%d)",
392 db_strerror(rc), rc );
394 LDAP_LOG ( TOOLS, ERR,
395 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
397 Debug( LDAP_DEBUG_ANY,
398 "=> bdb_tool_entry_put: %s\n",
399 text->bv_val, 0, 0 );
407 int bdb_tool_entry_reindex(
411 struct bdb_info *bi = (struct bdb_info *) be->be_private;
418 LDAP_LOG ( TOOLS, ARGS,
419 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
421 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
425 e = bdb_tool_entry_get( be, id );
429 LDAP_LOG ( TOOLS, DETAIL1,
430 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
433 Debug( LDAP_DEBUG_ANY,
434 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
440 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
443 LDAP_LOG ( TOOLS, ERR,
444 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
445 db_strerror(rc), rc, 0 );
447 Debug( LDAP_DEBUG_ANY,
448 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
449 db_strerror(rc), rc, 0 );
455 * just (re)add them for now
456 * assume that some other routine (not yet implemented)
457 * will zap index databases
462 LDAP_LOG ( TOOLS, ERR,
463 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
465 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
466 (long) id, e->e_dn, 0 );
470 op.o_tmpmemctx = NULL;
471 op.o_tmpmfuncs = &ch_mfuncs;
474 /* add dn2id indices */
475 rc = bdb_dn2id_add( &op, tid, NULL, e );
476 if( rc != 0 && rc != DB_KEYEXIST ) {
478 LDAP_LOG ( TOOLS, ERR,
479 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
480 db_strerror(rc), rc, 0 );
482 Debug( LDAP_DEBUG_ANY,
483 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
484 db_strerror(rc), rc, 0 );
490 rc = bdb_index_entry_add( &op, tid, e );
494 rc = TXN_COMMIT( tid, 0 );
497 LDAP_LOG ( TOOLS, ERR,
498 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
499 db_strerror(rc), rc, 0 );
501 Debug( LDAP_DEBUG_ANY,
502 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
503 db_strerror(rc), rc, 0 );
511 LDAP_LOG ( TOOLS, DETAIL1,
512 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
513 db_strerror(rc), rc, 0 );
515 Debug( LDAP_DEBUG_ANY,
516 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
517 db_strerror(rc), rc, 0 );
521 bdb_entry_release( &op, e, 0 );
526 ID bdb_tool_entry_modify(
529 struct berval *text )
532 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
536 assert( be != NULL );
537 assert( slapMode & SLAP_TOOL_MODE );
540 assert( text->bv_val );
541 assert( text->bv_val[0] == '\0' ); /* overconservative? */
543 assert ( e->e_id != NOID );
544 assert ( e->e_id != 0 );
547 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
548 (long) e->e_id, e->e_dn, 0 );
550 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
551 (long) e->e_id, e->e_dn, 0 );
554 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
555 bdb->bi_db_opflags );
557 snprintf( text->bv_val, text->bv_len,
558 "txn_begin failed: %s (%d)",
559 db_strerror(rc), rc );
561 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
563 Debug( LDAP_DEBUG_ANY,
564 "=> bdb_tool_entry_put: %s\n",
565 text->bv_val, 0, 0 );
571 op.o_tmpmemctx = NULL;
572 op.o_tmpmfuncs = &ch_mfuncs;
575 rc = bdb_id2entry_update( be, tid, e );
577 snprintf( text->bv_val, text->bv_len,
578 "id2entry_add failed: %s (%d)",
579 db_strerror(rc), rc );
581 LDAP_LOG ( TOOLS, ERR,
582 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
584 Debug( LDAP_DEBUG_ANY,
585 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
590 rc = bdb_index_entry_del( &op, tid, e );
592 snprintf( text->bv_val, text->bv_len,
593 "index_entry_del failed: %s (%d)",
594 db_strerror(rc), rc );
596 LDAP_LOG ( TOOLS, ERR,
597 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
599 Debug( LDAP_DEBUG_ANY,
600 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
605 rc = bdb_index_entry_add( &op, tid, e );
607 snprintf( text->bv_val, text->bv_len,
608 "index_entry_add failed: %s (%d)",
609 db_strerror(rc), rc );
611 LDAP_LOG ( TOOLS, ERR,
612 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
614 Debug( LDAP_DEBUG_ANY,
615 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
622 rc = TXN_COMMIT( tid, 0 );
624 snprintf( text->bv_val, text->bv_len,
625 "txn_commit failed: %s (%d)",
626 db_strerror(rc), rc );
628 LDAP_LOG ( TOOLS, ERR,
629 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
631 Debug( LDAP_DEBUG_ANY,
632 "=> bdb_tool_entry_put: %s\n",
633 text->bv_val, 0, 0 );
640 snprintf( text->bv_val, text->bv_len,
641 "txn_aborted! %s (%d)",
642 db_strerror(rc), rc );
644 LDAP_LOG ( TOOLS, ERR,
645 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
647 Debug( LDAP_DEBUG_ANY,
648 "=> bdb_tool_entry_put: %s\n",
649 text->bv_val, 0, 0 );