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 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 rc = entry_decode( &bv, &e );
181 if( rc == LDAP_SUCCESS ) {
186 EntryInfo *ei = NULL;
192 op.o_tmpmemctx = NULL;
193 op.o_tmpmfuncs = &ch_mfuncs;
195 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
196 if ( rc == LDAP_SUCCESS )
203 static int bdb_tool_next_id(
210 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
211 struct berval dn = e->e_name;
212 struct berval ndn = e->e_nname;
213 struct berval pdn, npdn;
214 EntryInfo *ei = NULL;
217 if (ndn.bv_len == 0) return 0;
219 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
220 if ( ei ) bdb_cache_entryinfo_unlock( ei );
221 if ( rc == DB_NOTFOUND ) {
222 if ( !be_issuffix( op->o_bd, &ndn ) ) {
223 dnParent( &dn, &pdn );
224 dnParent( &ndn, &npdn );
227 rc = bdb_tool_next_id( op, tid, e, text, 1 );
234 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
236 snprintf( text->bv_val, text->bv_len,
237 "next_id failed: %s (%d)",
238 db_strerror(rc), rc );
239 Debug( LDAP_DEBUG_ANY,
240 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
243 rc = bdb_dn2id_add( op, tid, ei, e );
245 snprintf( text->bv_val, text->bv_len,
246 "dn2id_add failed: %s (%d)",
247 db_strerror(rc), rc );
248 Debug( LDAP_DEBUG_ANY,
249 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
251 if ( nholes == nhmax - 1 ) {
252 if ( holes == hbuf ) {
253 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
254 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
256 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
260 ber_dupbv( &holes[nholes].dn, &ndn );
261 holes[nholes++].id = e->e_id;
263 } else if ( !hole ) {
266 e->e_id = ei->bei_id;
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;
295 assert( be != NULL );
296 assert( slapMode & SLAP_TOOL_MODE );
299 assert( text->bv_val );
300 assert( text->bv_val[0] == '\0' ); /* overconservative? */
302 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
303 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
305 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
306 bdb->bi_db_opflags );
308 snprintf( text->bv_val, text->bv_len,
309 "txn_begin failed: %s (%d)",
310 db_strerror(rc), rc );
311 Debug( LDAP_DEBUG_ANY,
312 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
313 text->bv_val, 0, 0 );
319 op.o_tmpmemctx = NULL;
320 op.o_tmpmfuncs = &ch_mfuncs;
322 /* add dn2id indices */
323 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
329 rc = bdb_id2entry_add( be, tid, e );
331 snprintf( text->bv_val, text->bv_len,
332 "id2entry_add failed: %s (%d)",
333 db_strerror(rc), rc );
334 Debug( LDAP_DEBUG_ANY,
335 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
336 text->bv_val, 0, 0 );
340 if ( !bdb->bi_linear_index )
341 rc = bdb_index_entry_add( &op, tid, e );
343 snprintf( text->bv_val, text->bv_len,
344 "index_entry_add failed: %s (%d)",
345 db_strerror(rc), rc );
346 Debug( LDAP_DEBUG_ANY,
347 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
348 text->bv_val, 0, 0 );
354 rc = TXN_COMMIT( tid, 0 );
356 snprintf( text->bv_val, text->bv_len,
357 "txn_commit failed: %s (%d)",
358 db_strerror(rc), rc );
359 Debug( LDAP_DEBUG_ANY,
360 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
361 text->bv_val, 0, 0 );
367 snprintf( text->bv_val, text->bv_len,
368 "txn_aborted! %s (%d)",
369 db_strerror(rc), rc );
370 Debug( LDAP_DEBUG_ANY,
371 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
372 text->bv_val, 0, 0 );
379 int bdb_tool_entry_reindex(
383 struct bdb_info *bi = (struct bdb_info *) be->be_private;
390 Debug( LDAP_DEBUG_ARGS,
391 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
394 /* No indexes configured, nothing to do. Could return an
395 * error here to shortcut things.
401 /* Get the first attribute to index */
402 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
403 index_attrs = bi->bi_attrs;
404 bi->bi_attrs = &index_dummy;
405 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
408 e = bdb_tool_entry_get( be, id );
411 Debug( LDAP_DEBUG_ANY,
412 LDAP_XSTRING(bdb_tool_entry_reindex)
413 ": could not locate id=%ld\n",
418 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
420 Debug( LDAP_DEBUG_ANY,
421 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
422 "txn_begin failed: %s (%d)\n",
423 db_strerror(rc), rc, 0 );
428 * just (re)add them for now
429 * assume that some other routine (not yet implemented)
430 * will zap index databases
434 Debug( LDAP_DEBUG_TRACE,
435 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
436 (long) id, e->e_dn, 0 );
440 op.o_tmpmemctx = NULL;
441 op.o_tmpmfuncs = &ch_mfuncs;
443 #if 0 /* ndef BDB_HIER */
444 /* add dn2id indices */
445 rc = bdb_dn2id_add( &op, tid, NULL, e );
446 if( rc != 0 && rc != DB_KEYEXIST ) {
447 Debug( LDAP_DEBUG_ANY,
448 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
449 ": dn2id_add failed: %s (%d)\n",
450 db_strerror(rc), rc, 0 );
455 rc = bdb_index_entry_add( &op, tid, e );
459 rc = TXN_COMMIT( tid, 0 );
461 Debug( LDAP_DEBUG_ANY,
462 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
463 ": txn_commit failed: %s (%d)\n",
464 db_strerror(rc), rc, 0 );
470 Debug( LDAP_DEBUG_ANY,
471 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
472 ": txn_aborted! %s (%d)\n",
473 db_strerror(rc), rc, 0 );
476 bdb_entry_release( &op, e, 0 );
481 ID bdb_tool_entry_modify(
484 struct berval *text )
487 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
492 assert( be != NULL );
493 assert( slapMode & SLAP_TOOL_MODE );
496 assert( text->bv_val );
497 assert( text->bv_val[0] == '\0' ); /* overconservative? */
499 assert ( e->e_id != NOID );
500 assert ( e->e_id != 0 );
502 Debug( LDAP_DEBUG_TRACE,
503 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
504 (long) e->e_id, e->e_dn, 0 );
506 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
507 bdb->bi_db_opflags );
509 snprintf( text->bv_val, text->bv_len,
510 "txn_begin failed: %s (%d)",
511 db_strerror(rc), rc );
512 Debug( LDAP_DEBUG_ANY,
513 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
514 text->bv_val, 0, 0 );
520 op.o_tmpmemctx = NULL;
521 op.o_tmpmfuncs = &ch_mfuncs;
524 rc = bdb_id2entry_update( be, tid, e );
526 snprintf( text->bv_val, text->bv_len,
527 "id2entry_add failed: %s (%d)",
528 db_strerror(rc), rc );
529 Debug( LDAP_DEBUG_ANY,
530 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
531 text->bv_val, 0, 0 );
535 rc = bdb_index_entry_del( &op, tid, e );
537 snprintf( text->bv_val, text->bv_len,
538 "index_entry_del failed: %s (%d)",
539 db_strerror(rc), rc );
540 Debug( LDAP_DEBUG_ANY,
541 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
542 text->bv_val, 0, 0 );
546 rc = bdb_index_entry_add( &op, tid, e );
548 snprintf( text->bv_val, text->bv_len,
549 "index_entry_add failed: %s (%d)",
550 db_strerror(rc), rc );
551 Debug( LDAP_DEBUG_ANY,
552 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
553 text->bv_val, 0, 0 );
559 rc = TXN_COMMIT( tid, 0 );
561 snprintf( text->bv_val, text->bv_len,
562 "txn_commit failed: %s (%d)",
563 db_strerror(rc), rc );
564 Debug( LDAP_DEBUG_ANY,
565 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
566 "%s\n", text->bv_val, 0, 0 );
572 snprintf( text->bv_val, text->bv_len,
573 "txn_aborted! %s (%d)",
574 db_strerror(rc), rc );
575 Debug( LDAP_DEBUG_ANY,
576 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
577 text->bv_val, 0, 0 );