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 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 );
141 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_tool_dn2id_get)
142 ": get failed: %s (%d)\n",
143 db_strerror( rc ), rc, 0 );
151 int bdb_tool_id2entry_get(
157 return bdb_id2entry( be, NULL, id, e );
160 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
166 assert( be != NULL );
167 assert( slapMode & SLAP_TOOL_MODE );
168 assert( data.data != NULL );
171 DBT2bv( &data, &bv );
173 rc = entry_decode( &bv, &e );
175 if( rc == LDAP_SUCCESS ) {
180 EntryInfo *ei = NULL;
184 op.o_tmpmemctx = NULL;
185 op.o_tmpmfuncs = &ch_mfuncs;
187 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
188 if ( rc == LDAP_SUCCESS )
195 static int bdb_tool_next_id(
202 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
203 struct berval dn = e->e_name;
204 struct berval ndn = e->e_nname;
205 struct berval pdn, npdn;
206 EntryInfo *ei = NULL;
209 if (ndn.bv_len == 0) return 0;
211 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
212 if ( ei ) bdb_cache_entryinfo_unlock( ei );
213 if ( rc == DB_NOTFOUND ) {
214 if ( !be_issuffix( op->o_bd, &ndn ) ) {
215 dnParent( &dn, &pdn );
216 dnParent( &ndn, &npdn );
219 rc = bdb_tool_next_id( op, tid, e, text, 1 );
226 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
228 snprintf( text->bv_val, text->bv_len,
229 "next_id failed: %s (%d)",
230 db_strerror(rc), rc );
231 Debug( LDAP_DEBUG_ANY,
232 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
235 rc = bdb_dn2id_add( op, tid, ei, e );
237 snprintf( text->bv_val, text->bv_len,
238 "dn2id_add failed: %s (%d)",
239 db_strerror(rc), rc );
240 Debug( LDAP_DEBUG_ANY,
241 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
243 if ( nholes == nhmax - 1 ) {
244 if ( holes == hbuf ) {
245 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
246 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
248 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
252 ber_dupbv( &holes[nholes].dn, &ndn );
253 holes[nholes++].id = e->e_id;
255 } else if ( !hole ) {
258 e->e_id = ei->bei_id;
260 for ( i=0; i<nholes; i++) {
261 if ( holes[i].id == e->e_id ) {
263 free(holes[i].dn.bv_val);
264 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
268 } else if ( holes[i].id > e->e_id ) {
276 ID bdb_tool_entry_put(
279 struct berval *text )
282 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
286 assert( be != NULL );
287 assert( slapMode & SLAP_TOOL_MODE );
290 assert( text->bv_val );
291 assert( text->bv_val[0] == '\0' ); /* overconservative? */
293 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
294 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
296 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
297 bdb->bi_db_opflags );
299 snprintf( text->bv_val, text->bv_len,
300 "txn_begin failed: %s (%d)",
301 db_strerror(rc), rc );
302 Debug( LDAP_DEBUG_ANY,
303 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
304 text->bv_val, 0, 0 );
309 op.o_tmpmemctx = NULL;
310 op.o_tmpmfuncs = &ch_mfuncs;
312 /* add dn2id indices */
313 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
319 rc = bdb_id2entry_add( be, tid, e );
321 snprintf( text->bv_val, text->bv_len,
322 "id2entry_add failed: %s (%d)",
323 db_strerror(rc), rc );
324 Debug( LDAP_DEBUG_ANY,
325 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
326 text->bv_val, 0, 0 );
330 rc = bdb_index_entry_add( &op, tid, e );
332 snprintf( text->bv_val, text->bv_len,
333 "index_entry_add failed: %s (%d)",
334 db_strerror(rc), rc );
335 Debug( LDAP_DEBUG_ANY,
336 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
337 text->bv_val, 0, 0 );
343 rc = TXN_COMMIT( tid, 0 );
345 snprintf( text->bv_val, text->bv_len,
346 "txn_commit 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 );
356 snprintf( text->bv_val, text->bv_len,
357 "txn_aborted! %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 );
368 int bdb_tool_entry_reindex(
372 struct bdb_info *bi = (struct bdb_info *) be->be_private;
378 Debug( LDAP_DEBUG_ARGS,
379 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
382 e = bdb_tool_entry_get( be, id );
385 Debug( LDAP_DEBUG_ANY,
386 LDAP_XSTRING(bdb_tool_entry_reindex)
387 ": could not locate id=%ld\n",
392 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
394 Debug( LDAP_DEBUG_ANY,
395 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
396 "txn_begin failed: %s (%d)\n",
397 db_strerror(rc), rc, 0 );
402 * just (re)add them for now
403 * assume that some other routine (not yet implemented)
404 * will zap index databases
408 Debug( LDAP_DEBUG_TRACE,
409 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
410 (long) id, e->e_dn, 0 );
413 op.o_tmpmemctx = NULL;
414 op.o_tmpmfuncs = &ch_mfuncs;
417 /* add dn2id indices */
418 rc = bdb_dn2id_add( &op, tid, NULL, e );
419 if( rc != 0 && rc != DB_KEYEXIST ) {
420 Debug( LDAP_DEBUG_ANY,
421 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
422 ": dn2id_add failed: %s (%d)\n",
423 db_strerror(rc), rc, 0 );
428 rc = bdb_index_entry_add( &op, tid, e );
432 rc = TXN_COMMIT( tid, 0 );
434 Debug( LDAP_DEBUG_ANY,
435 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
436 ": txn_commit failed: %s (%d)\n",
437 db_strerror(rc), rc, 0 );
443 Debug( LDAP_DEBUG_ANY,
444 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
445 ": txn_aborted! %s (%d)\n",
446 db_strerror(rc), rc, 0 );
449 bdb_entry_release( &op, e, 0 );
454 ID bdb_tool_entry_modify(
457 struct berval *text )
460 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
464 assert( be != NULL );
465 assert( slapMode & SLAP_TOOL_MODE );
468 assert( text->bv_val );
469 assert( text->bv_val[0] == '\0' ); /* overconservative? */
471 assert ( e->e_id != NOID );
472 assert ( e->e_id != 0 );
474 Debug( LDAP_DEBUG_TRACE,
475 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
476 (long) e->e_id, e->e_dn, 0 );
478 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
479 bdb->bi_db_opflags );
481 snprintf( text->bv_val, text->bv_len,
482 "txn_begin failed: %s (%d)",
483 db_strerror(rc), rc );
484 Debug( LDAP_DEBUG_ANY,
485 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
486 text->bv_val, 0, 0 );
491 op.o_tmpmemctx = NULL;
492 op.o_tmpmfuncs = &ch_mfuncs;
495 rc = bdb_id2entry_update( be, tid, e );
497 snprintf( text->bv_val, text->bv_len,
498 "id2entry_add failed: %s (%d)",
499 db_strerror(rc), rc );
500 Debug( LDAP_DEBUG_ANY,
501 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
502 text->bv_val, 0, 0 );
506 rc = bdb_index_entry_del( &op, tid, e );
508 snprintf( text->bv_val, text->bv_len,
509 "index_entry_del failed: %s (%d)",
510 db_strerror(rc), rc );
511 Debug( LDAP_DEBUG_ANY,
512 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
513 text->bv_val, 0, 0 );
517 rc = bdb_index_entry_add( &op, tid, e );
519 snprintf( text->bv_val, text->bv_len,
520 "index_entry_add failed: %s (%d)",
521 db_strerror(rc), rc );
522 Debug( LDAP_DEBUG_ANY,
523 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
524 text->bv_val, 0, 0 );
530 rc = TXN_COMMIT( tid, 0 );
532 snprintf( text->bv_val, text->bv_len,
533 "txn_commit failed: %s (%d)",
534 db_strerror(rc), rc );
535 Debug( LDAP_DEBUG_ANY,
536 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
537 "%s\n", text->bv_val, 0, 0 );
543 snprintf( text->bv_val, text->bv_len,
544 "txn_aborted! %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 );