1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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 Avlnode *index_attrs, index_dummy;
40 int bdb_tool_entry_open(
41 BackendDB *be, int mode )
43 /* initialize key and data thangs */
46 key.flags = DB_DBT_REALLOC;
47 data.flags = DB_DBT_REALLOC;
52 int bdb_tool_entry_close(
67 cursor->c_close( cursor );
73 fprintf( stderr, "Error, entries missing!\n");
74 for (i=0; i<nholes; i++) {
75 fprintf(stderr, " entry %ld: %s\n",
76 holes[i].id, holes[i].dn.bv_val);
84 static int bdb_reindex_cmp(const void *a, const void *b) { return 0; }
86 ID bdb_tool_entry_next(
91 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
94 assert( slapMode & SLAP_TOOL_MODE );
95 assert( bdb != NULL );
99 rc = bdb->bi_id2entry->bdi_db->cursor(
100 bdb->bi_id2entry->bdi_db, NULL, &cursor,
101 bdb->bi_db_opflags );
107 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
110 /* If we're doing linear indexing and there are more attrs to
111 * index, and we're at the end of the database, start over.
113 if ( bdb->bi_attrs == &index_dummy ) {
114 if ( index_attrs && rc == DB_NOTFOUND ) {
115 /* optional - do a checkpoint here? */
116 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
117 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
120 bdb->bi_attrs = NULL;
128 if( data.data == NULL ) {
132 BDB_DISK2ID( key.data, &id );
136 ID bdb_tool_dn2id_get(
147 op.o_tmpmemctx = NULL;
148 op.o_tmpmfuncs = &ch_mfuncs;
152 bdb_dn2id( &op, NULL, dn, &ei );
157 int bdb_tool_id2entry_get(
163 return bdb_id2entry( be, NULL, id, e );
166 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
172 assert( be != NULL );
173 assert( slapMode & SLAP_TOOL_MODE );
174 assert( data.data != NULL );
177 DBT2bv( &data, &bv );
179 #ifdef SLAP_ZONE_ALLOC
180 /* FIXME: will add ctx later */
181 rc = entry_decode( &bv, &e, NULL );
183 rc = entry_decode( &bv, &e );
186 if( rc == LDAP_SUCCESS ) {
191 EntryInfo *ei = NULL;
197 op.o_tmpmemctx = NULL;
198 op.o_tmpmfuncs = &ch_mfuncs;
200 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
201 if ( rc == LDAP_SUCCESS )
208 static int bdb_tool_next_id(
215 struct berval dn = e->e_name;
216 struct berval ndn = e->e_nname;
217 struct berval pdn, npdn;
218 EntryInfo *ei = NULL;
221 if (ndn.bv_len == 0) return 0;
223 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
224 if ( ei ) bdb_cache_entryinfo_unlock( ei );
225 if ( rc == DB_NOTFOUND ) {
226 if ( !be_issuffix( op->o_bd, &ndn ) ) {
227 dnParent( &dn, &pdn );
228 dnParent( &ndn, &npdn );
231 rc = bdb_tool_next_id( op, tid, e, text, 1 );
238 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
240 snprintf( text->bv_val, text->bv_len,
241 "next_id failed: %s (%d)",
242 db_strerror(rc), rc );
243 Debug( LDAP_DEBUG_ANY,
244 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
247 rc = bdb_dn2id_add( op, tid, ei, e );
249 snprintf( text->bv_val, text->bv_len,
250 "dn2id_add failed: %s (%d)",
251 db_strerror(rc), rc );
252 Debug( LDAP_DEBUG_ANY,
253 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
255 if ( nholes == nhmax - 1 ) {
256 if ( holes == hbuf ) {
257 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
258 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
260 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
264 ber_dupbv( &holes[nholes].dn, &ndn );
265 holes[nholes++].id = e->e_id;
267 } else if ( !hole ) {
270 e->e_id = ei->bei_id;
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;
299 assert( be != NULL );
300 assert( slapMode & SLAP_TOOL_MODE );
303 assert( text->bv_val );
304 assert( text->bv_val[0] == '\0' ); /* overconservative? */
306 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
307 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
309 if (! (slapMode & SLAP_TOOL_QUICK)) {
310 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
311 bdb->bi_db_opflags );
313 snprintf( text->bv_val, text->bv_len,
314 "txn_begin failed: %s (%d)",
315 db_strerror(rc), rc );
316 Debug( LDAP_DEBUG_ANY,
317 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
318 text->bv_val, 0, 0 );
325 op.o_tmpmemctx = NULL;
326 op.o_tmpmfuncs = &ch_mfuncs;
328 /* add dn2id indices */
329 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
335 rc = bdb_id2entry_add( be, tid, e );
337 snprintf( text->bv_val, text->bv_len,
338 "id2entry_add failed: %s (%d)",
339 db_strerror(rc), rc );
340 Debug( LDAP_DEBUG_ANY,
341 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
342 text->bv_val, 0, 0 );
346 if ( !bdb->bi_linear_index )
347 rc = bdb_index_entry_add( &op, tid, e );
349 snprintf( text->bv_val, text->bv_len,
350 "index_entry_add failed: %s (%d)",
351 db_strerror(rc), rc );
352 Debug( LDAP_DEBUG_ANY,
353 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
354 text->bv_val, 0, 0 );
360 if ( !( slapMode & SLAP_TOOL_QUICK )) {
361 rc = TXN_COMMIT( tid, 0 );
363 snprintf( text->bv_val, text->bv_len,
364 "txn_commit failed: %s (%d)",
365 db_strerror(rc), rc );
366 Debug( LDAP_DEBUG_ANY,
367 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
368 text->bv_val, 0, 0 );
374 if ( !( slapMode & SLAP_TOOL_QUICK )) {
376 snprintf( text->bv_val, text->bv_len,
377 "txn_aborted! %s (%d)",
378 db_strerror(rc), rc );
379 Debug( LDAP_DEBUG_ANY,
380 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
381 text->bv_val, 0, 0 );
389 int bdb_tool_entry_reindex(
393 struct bdb_info *bi = (struct bdb_info *) be->be_private;
400 Debug( LDAP_DEBUG_ARGS,
401 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
404 /* No indexes configured, nothing to do. Could return an
405 * error here to shortcut things.
411 /* Get the first attribute to index */
412 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
413 index_attrs = bi->bi_attrs;
414 bi->bi_attrs = &index_dummy;
415 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
418 e = bdb_tool_entry_get( be, id );
421 Debug( LDAP_DEBUG_ANY,
422 LDAP_XSTRING(bdb_tool_entry_reindex)
423 ": could not locate id=%ld\n",
428 if (! (slapMode & SLAP_TOOL_QUICK)) {
429 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
431 Debug( LDAP_DEBUG_ANY,
432 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
433 "txn_begin failed: %s (%d)\n",
434 db_strerror(rc), rc, 0 );
440 * just (re)add them for now
441 * assume that some other routine (not yet implemented)
442 * will zap index databases
446 Debug( LDAP_DEBUG_TRACE,
447 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
448 (long) id, e->e_dn, 0 );
452 op.o_tmpmemctx = NULL;
453 op.o_tmpmfuncs = &ch_mfuncs;
455 rc = bdb_index_entry_add( &op, tid, e );
459 if (! (slapMode & SLAP_TOOL_QUICK)) {
460 rc = TXN_COMMIT( tid, 0 );
462 Debug( LDAP_DEBUG_ANY,
463 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
464 ": txn_commit failed: %s (%d)\n",
465 db_strerror(rc), rc, 0 );
471 if (! (slapMode & SLAP_TOOL_QUICK)) {
473 Debug( LDAP_DEBUG_ANY,
474 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
475 ": txn_aborted! %s (%d)\n",
476 db_strerror(rc), rc, 0 );
480 bdb_entry_release( &op, e, 0 );
485 ID bdb_tool_entry_modify(
488 struct berval *text )
491 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
496 assert( be != NULL );
497 assert( slapMode & SLAP_TOOL_MODE );
500 assert( text->bv_val );
501 assert( text->bv_val[0] == '\0' ); /* overconservative? */
503 assert ( e->e_id != NOID );
504 assert ( e->e_id != 0 );
506 Debug( LDAP_DEBUG_TRACE,
507 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
508 (long) e->e_id, e->e_dn, 0 );
510 if (! (slapMode & SLAP_TOOL_QUICK)) {
511 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
512 bdb->bi_db_opflags );
514 snprintf( text->bv_val, text->bv_len,
515 "txn_begin failed: %s (%d)",
516 db_strerror(rc), rc );
517 Debug( LDAP_DEBUG_ANY,
518 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
519 text->bv_val, 0, 0 );
526 op.o_tmpmemctx = NULL;
527 op.o_tmpmfuncs = &ch_mfuncs;
530 rc = bdb_id2entry_update( be, tid, e );
532 snprintf( text->bv_val, text->bv_len,
533 "id2entry_add failed: %s (%d)",
534 db_strerror(rc), rc );
535 Debug( LDAP_DEBUG_ANY,
536 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
537 text->bv_val, 0, 0 );
541 rc = bdb_index_entry_del( &op, tid, e );
543 snprintf( text->bv_val, text->bv_len,
544 "index_entry_del failed: %s (%d)",
545 db_strerror(rc), rc );
546 Debug( LDAP_DEBUG_ANY,
547 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
548 text->bv_val, 0, 0 );
552 rc = bdb_index_entry_add( &op, tid, e );
554 snprintf( text->bv_val, text->bv_len,
555 "index_entry_add failed: %s (%d)",
556 db_strerror(rc), rc );
557 Debug( LDAP_DEBUG_ANY,
558 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
559 text->bv_val, 0, 0 );
565 if (! (slapMode & SLAP_TOOL_QUICK)) {
566 rc = TXN_COMMIT( tid, 0 );
568 snprintf( text->bv_val, text->bv_len,
569 "txn_commit failed: %s (%d)",
570 db_strerror(rc), rc );
571 Debug( LDAP_DEBUG_ANY,
572 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
573 "%s\n", text->bv_val, 0, 0 );
579 if (! (slapMode & SLAP_TOOL_QUICK)) {
581 snprintf( text->bv_val, text->bv_len,
582 "txn_aborted! %s (%d)",
583 db_strerror(rc), rc );
584 Debug( LDAP_DEBUG_ANY,
585 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
586 text->bv_val, 0, 0 );