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 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 AC_MEMCPY( &id, key.data, key.size );
136 ID bdb_tool_dn2id_get(
141 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
142 DB *db = bdb->bi_dn2id->bdi_db;
148 key.size = dn->bv_len + 2;
149 key.data = ch_malloc( key.size );
150 ((char*)key.data)[0] = DN_BASE_PREFIX;
151 AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
155 data.ulen = sizeof(ID);
156 data.flags = DB_DBT_USERMEM;
158 rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
161 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_tool_dn2id_get)
162 ": get failed: %s (%d)\n",
163 db_strerror( rc ), rc, 0 );
171 int bdb_tool_id2entry_get(
177 return bdb_id2entry( be, NULL, id, e );
180 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
186 assert( be != NULL );
187 assert( slapMode & SLAP_TOOL_MODE );
188 assert( data.data != NULL );
191 DBT2bv( &data, &bv );
193 rc = entry_decode( &bv, &e );
195 if( rc == LDAP_SUCCESS ) {
200 EntryInfo *ei = NULL;
206 op.o_tmpmemctx = NULL;
207 op.o_tmpmfuncs = &ch_mfuncs;
209 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
210 if ( rc == LDAP_SUCCESS )
217 static int bdb_tool_next_id(
224 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
225 struct berval dn = e->e_name;
226 struct berval ndn = e->e_nname;
227 struct berval pdn, npdn;
228 EntryInfo *ei = NULL;
231 if (ndn.bv_len == 0) return 0;
233 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
234 if ( ei ) bdb_cache_entryinfo_unlock( ei );
235 if ( rc == DB_NOTFOUND ) {
236 if ( !be_issuffix( op->o_bd, &ndn ) ) {
237 dnParent( &dn, &pdn );
238 dnParent( &ndn, &npdn );
241 rc = bdb_tool_next_id( op, tid, e, text, 1 );
248 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
250 snprintf( text->bv_val, text->bv_len,
251 "next_id failed: %s (%d)",
252 db_strerror(rc), rc );
253 Debug( LDAP_DEBUG_ANY,
254 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
257 rc = bdb_dn2id_add( op, tid, ei, e );
259 snprintf( text->bv_val, text->bv_len,
260 "dn2id_add failed: %s (%d)",
261 db_strerror(rc), rc );
262 Debug( LDAP_DEBUG_ANY,
263 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
265 if ( nholes == nhmax - 1 ) {
266 if ( holes == hbuf ) {
267 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
268 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
270 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
274 ber_dupbv( &holes[nholes].dn, &ndn );
275 holes[nholes++].id = e->e_id;
277 } else if ( !hole ) {
280 e->e_id = ei->bei_id;
282 for ( i=0; i<nholes; i++) {
283 if ( holes[i].id == e->e_id ) {
285 free(holes[i].dn.bv_val);
286 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
290 } else if ( holes[i].id > e->e_id ) {
298 ID bdb_tool_entry_put(
301 struct berval *text )
304 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
309 assert( be != NULL );
310 assert( slapMode & SLAP_TOOL_MODE );
313 assert( text->bv_val );
314 assert( text->bv_val[0] == '\0' ); /* overconservative? */
316 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
317 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
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 );
333 op.o_tmpmemctx = NULL;
334 op.o_tmpmfuncs = &ch_mfuncs;
336 /* add dn2id indices */
337 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
343 rc = bdb_id2entry_add( be, tid, e );
345 snprintf( text->bv_val, text->bv_len,
346 "id2entry_add failed: %s (%d)",
347 db_strerror(rc), rc );
348 Debug( LDAP_DEBUG_ANY,
349 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
350 text->bv_val, 0, 0 );
354 if ( !bdb->bi_linear_index )
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 );
360 Debug( LDAP_DEBUG_ANY,
361 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
362 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 );
373 Debug( LDAP_DEBUG_ANY,
374 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
375 text->bv_val, 0, 0 );
381 snprintf( text->bv_val, text->bv_len,
382 "txn_aborted! %s (%d)",
383 db_strerror(rc), rc );
384 Debug( LDAP_DEBUG_ANY,
385 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
386 text->bv_val, 0, 0 );
393 int bdb_tool_entry_reindex(
397 struct bdb_info *bi = (struct bdb_info *) be->be_private;
404 Debug( LDAP_DEBUG_ARGS,
405 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
408 /* No indexes configured, nothing to do. Could return an
409 * error here to shortcut things.
415 /* Get the first attribute to index */
416 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
417 index_attrs = bi->bi_attrs;
418 bi->bi_attrs = &index_dummy;
419 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
422 e = bdb_tool_entry_get( be, id );
425 Debug( LDAP_DEBUG_ANY,
426 LDAP_XSTRING(bdb_tool_entry_reindex)
427 ": could not locate id=%ld\n",
432 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
434 Debug( LDAP_DEBUG_ANY,
435 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
436 "txn_begin failed: %s (%d)\n",
437 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
448 Debug( LDAP_DEBUG_TRACE,
449 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
450 (long) id, e->e_dn, 0 );
454 op.o_tmpmemctx = NULL;
455 op.o_tmpmfuncs = &ch_mfuncs;
457 #if 0 /* ndef BDB_HIER */
458 /* add dn2id indices */
459 rc = bdb_dn2id_add( &op, tid, NULL, e );
460 if( rc != 0 && rc != DB_KEYEXIST ) {
461 Debug( LDAP_DEBUG_ANY,
462 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
463 ": dn2id_add failed: %s (%d)\n",
464 db_strerror(rc), rc, 0 );
469 rc = bdb_index_entry_add( &op, tid, e );
473 rc = TXN_COMMIT( tid, 0 );
475 Debug( LDAP_DEBUG_ANY,
476 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
477 ": txn_commit failed: %s (%d)\n",
478 db_strerror(rc), rc, 0 );
484 Debug( LDAP_DEBUG_ANY,
485 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
486 ": txn_aborted! %s (%d)\n",
487 db_strerror(rc), rc, 0 );
490 bdb_entry_release( &op, e, 0 );
495 ID bdb_tool_entry_modify(
498 struct berval *text )
501 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
506 assert( be != NULL );
507 assert( slapMode & SLAP_TOOL_MODE );
510 assert( text->bv_val );
511 assert( text->bv_val[0] == '\0' ); /* overconservative? */
513 assert ( e->e_id != NOID );
514 assert ( e->e_id != 0 );
516 Debug( LDAP_DEBUG_TRACE,
517 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
518 (long) e->e_id, e->e_dn, 0 );
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 );
534 op.o_tmpmemctx = NULL;
535 op.o_tmpmfuncs = &ch_mfuncs;
538 rc = bdb_id2entry_update( be, tid, e );
540 snprintf( text->bv_val, text->bv_len,
541 "id2entry_add failed: %s (%d)",
542 db_strerror(rc), rc );
543 Debug( LDAP_DEBUG_ANY,
544 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
545 text->bv_val, 0, 0 );
549 rc = bdb_index_entry_del( &op, tid, e );
551 snprintf( text->bv_val, text->bv_len,
552 "index_entry_del failed: %s (%d)",
553 db_strerror(rc), rc );
554 Debug( LDAP_DEBUG_ANY,
555 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
556 text->bv_val, 0, 0 );
560 rc = bdb_index_entry_add( &op, tid, e );
562 snprintf( text->bv_val, text->bv_len,
563 "index_entry_add failed: %s (%d)",
564 db_strerror(rc), rc );
565 Debug( LDAP_DEBUG_ANY,
566 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
567 text->bv_val, 0, 0 );
573 rc = TXN_COMMIT( tid, 0 );
575 snprintf( text->bv_val, text->bv_len,
576 "txn_commit failed: %s (%d)",
577 db_strerror(rc), rc );
578 Debug( LDAP_DEBUG_ANY,
579 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
580 "%s\n", text->bv_val, 0, 0 );
586 snprintf( text->bv_val, text->bv_len,
587 "txn_aborted! %s (%d)",
588 db_strerror(rc), rc );
589 Debug( LDAP_DEBUG_ANY,
590 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
591 text->bv_val, 0, 0 );