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, eidummy;
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 ) ) {
228 dnParent( &dn, &pdn );
229 dnParent( &ndn, &npdn );
232 rc = bdb_tool_next_id( op, tid, e, text, 1 );
238 /* If parent didn't exist, it was created just now
239 * and its ID is now in e->e_id. Make sure the current
240 * entry gets added under the new parent ID.
242 if ( eid != e->e_id ) {
243 eidummy.bei_id = e->e_id;
247 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
249 snprintf( text->bv_val, text->bv_len,
250 "next_id 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 );
256 rc = bdb_dn2id_add( op, tid, ei, e );
258 snprintf( text->bv_val, text->bv_len,
259 "dn2id_add failed: %s (%d)",
260 db_strerror(rc), rc );
261 Debug( LDAP_DEBUG_ANY,
262 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
264 if ( nholes == nhmax - 1 ) {
265 if ( holes == hbuf ) {
266 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
267 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
269 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
273 ber_dupbv( &holes[nholes].dn, &ndn );
274 holes[nholes++].id = e->e_id;
276 } else if ( !hole ) {
279 e->e_id = ei->bei_id;
281 for ( i=0; i<nholes; i++) {
282 if ( holes[i].id == e->e_id ) {
284 free(holes[i].dn.bv_val);
285 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
289 } else if ( holes[i].id > e->e_id ) {
297 ID bdb_tool_entry_put(
300 struct berval *text )
303 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
308 assert( be != NULL );
309 assert( slapMode & SLAP_TOOL_MODE );
312 assert( text->bv_val );
313 assert( text->bv_val[0] == '\0' ); /* overconservative? */
315 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
316 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
318 if (! (slapMode & SLAP_TOOL_QUICK)) {
319 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
320 bdb->bi_db_opflags );
322 snprintf( text->bv_val, text->bv_len,
323 "txn_begin failed: %s (%d)",
324 db_strerror(rc), rc );
325 Debug( LDAP_DEBUG_ANY,
326 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
327 text->bv_val, 0, 0 );
334 op.o_tmpmemctx = NULL;
335 op.o_tmpmfuncs = &ch_mfuncs;
337 /* add dn2id indices */
338 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
344 rc = bdb_id2entry_add( be, tid, e );
346 snprintf( text->bv_val, text->bv_len,
347 "id2entry_add failed: %s (%d)",
348 db_strerror(rc), rc );
349 Debug( LDAP_DEBUG_ANY,
350 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
351 text->bv_val, 0, 0 );
355 if ( !bdb->bi_linear_index )
356 rc = bdb_index_entry_add( &op, tid, e );
358 snprintf( text->bv_val, text->bv_len,
359 "index_entry_add failed: %s (%d)",
360 db_strerror(rc), rc );
361 Debug( LDAP_DEBUG_ANY,
362 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
363 text->bv_val, 0, 0 );
369 if ( !( slapMode & SLAP_TOOL_QUICK )) {
370 rc = TXN_COMMIT( tid, 0 );
372 snprintf( text->bv_val, text->bv_len,
373 "txn_commit failed: %s (%d)",
374 db_strerror(rc), rc );
375 Debug( LDAP_DEBUG_ANY,
376 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
377 text->bv_val, 0, 0 );
383 if ( !( slapMode & SLAP_TOOL_QUICK )) {
385 snprintf( text->bv_val, text->bv_len,
386 "txn_aborted! %s (%d)",
387 db_strerror(rc), rc );
388 Debug( LDAP_DEBUG_ANY,
389 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
390 text->bv_val, 0, 0 );
398 int bdb_tool_entry_reindex(
402 struct bdb_info *bi = (struct bdb_info *) be->be_private;
409 Debug( LDAP_DEBUG_ARGS,
410 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
413 /* No indexes configured, nothing to do. Could return an
414 * error here to shortcut things.
420 /* Get the first attribute to index */
421 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
422 index_attrs = bi->bi_attrs;
423 bi->bi_attrs = &index_dummy;
424 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
427 e = bdb_tool_entry_get( be, id );
430 Debug( LDAP_DEBUG_ANY,
431 LDAP_XSTRING(bdb_tool_entry_reindex)
432 ": could not locate id=%ld\n",
437 if (! (slapMode & SLAP_TOOL_QUICK)) {
438 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
440 Debug( LDAP_DEBUG_ANY,
441 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
442 "txn_begin failed: %s (%d)\n",
443 db_strerror(rc), rc, 0 );
449 * just (re)add them for now
450 * assume that some other routine (not yet implemented)
451 * will zap index databases
455 Debug( LDAP_DEBUG_TRACE,
456 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
457 (long) id, e->e_dn, 0 );
461 op.o_tmpmemctx = NULL;
462 op.o_tmpmfuncs = &ch_mfuncs;
464 rc = bdb_index_entry_add( &op, tid, e );
468 if (! (slapMode & SLAP_TOOL_QUICK)) {
469 rc = TXN_COMMIT( tid, 0 );
471 Debug( LDAP_DEBUG_ANY,
472 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
473 ": txn_commit failed: %s (%d)\n",
474 db_strerror(rc), rc, 0 );
480 if (! (slapMode & SLAP_TOOL_QUICK)) {
482 Debug( LDAP_DEBUG_ANY,
483 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
484 ": txn_aborted! %s (%d)\n",
485 db_strerror(rc), rc, 0 );
489 bdb_entry_release( &op, e, 0 );
494 ID bdb_tool_entry_modify(
497 struct berval *text )
500 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
505 assert( be != NULL );
506 assert( slapMode & SLAP_TOOL_MODE );
509 assert( text->bv_val );
510 assert( text->bv_val[0] == '\0' ); /* overconservative? */
512 assert ( e->e_id != NOID );
513 assert ( e->e_id != 0 );
515 Debug( LDAP_DEBUG_TRACE,
516 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
517 (long) e->e_id, e->e_dn, 0 );
519 if (! (slapMode & SLAP_TOOL_QUICK)) {
520 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
521 bdb->bi_db_opflags );
523 snprintf( text->bv_val, text->bv_len,
524 "txn_begin failed: %s (%d)",
525 db_strerror(rc), rc );
526 Debug( LDAP_DEBUG_ANY,
527 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
528 text->bv_val, 0, 0 );
535 op.o_tmpmemctx = NULL;
536 op.o_tmpmfuncs = &ch_mfuncs;
539 rc = bdb_id2entry_update( be, tid, e );
541 snprintf( text->bv_val, text->bv_len,
542 "id2entry_add failed: %s (%d)",
543 db_strerror(rc), rc );
544 Debug( LDAP_DEBUG_ANY,
545 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
546 text->bv_val, 0, 0 );
550 rc = bdb_index_entry_del( &op, tid, e );
552 snprintf( text->bv_val, text->bv_len,
553 "index_entry_del failed: %s (%d)",
554 db_strerror(rc), rc );
555 Debug( LDAP_DEBUG_ANY,
556 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
557 text->bv_val, 0, 0 );
561 rc = bdb_index_entry_add( &op, tid, e );
563 snprintf( text->bv_val, text->bv_len,
564 "index_entry_add failed: %s (%d)",
565 db_strerror(rc), rc );
566 Debug( LDAP_DEBUG_ANY,
567 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
568 text->bv_val, 0, 0 );
574 if (! (slapMode & SLAP_TOOL_QUICK)) {
575 rc = TXN_COMMIT( tid, 0 );
577 snprintf( text->bv_val, text->bv_len,
578 "txn_commit failed: %s (%d)",
579 db_strerror(rc), rc );
580 Debug( LDAP_DEBUG_ANY,
581 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
582 "%s\n", text->bv_val, 0, 0 );
588 if (! (slapMode & SLAP_TOOL_QUICK)) {
590 snprintf( text->bv_val, text->bv_len,
591 "txn_aborted! %s (%d)",
592 db_strerror(rc), rc );
593 Debug( LDAP_DEBUG_ANY,
594 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
595 text->bv_val, 0, 0 );