1 /* tools.c - tools for slap tools */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
15 static DBC *cursor = NULL;
18 typedef struct dn_id {
23 #define HOLE_SIZE 4096
24 dn_id hbuf[HOLE_SIZE], *holes = hbuf;
25 unsigned nhmax = HOLE_SIZE;
28 int bdb_tool_entry_open(
29 BackendDB *be, int mode )
31 /* initialize key and data thangs */
34 key.flags = DB_DBT_REALLOC;
35 data.flags = DB_DBT_REALLOC;
40 int bdb_tool_entry_close(
55 cursor->c_close( cursor );
61 fprintf( stderr, "Error, entries missing!\n");
62 for (i=0; i<nholes; i++) {
63 fprintf(stderr, " entry %d: %s\n",
64 holes[i].id, holes[i].dn.bv_val, 0);
72 ID bdb_tool_entry_next(
77 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
80 assert( slapMode & SLAP_TOOL_MODE );
81 assert( bdb != NULL );
84 rc = bdb->bi_id2entry->bdi_db->cursor(
85 bdb->bi_id2entry->bdi_db, NULL, &cursor,
92 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
98 if( data.data == NULL ) {
102 AC_MEMCPY( &id, key.data, key.size );
106 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
112 assert( be != NULL );
113 assert( slapMode & SLAP_TOOL_MODE );
114 assert( data.data != NULL );
116 DBT2bv( &data, &bv );
118 rc = entry_decode( &bv, &e );
120 if( rc == LDAP_SUCCESS ) {
125 bdb_fix_dn(be, id, e);
131 int bdb_tool_next_id(
139 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
140 struct berval dn = e->e_nname;
142 EntryInfo *ei = NULL;
145 rc = bdb_cache_find_entry_ndn2id( be, tid, &dn, &ei, locker, NULL );
146 if ( ei ) bdb_cache_entryinfo_unlock( ei );
147 if ( rc == DB_NOTFOUND ) {
148 if ( be_issuffix( be, &dn ) ) {
151 dnParent( &dn, &pdn );
153 rc = bdb_tool_next_id( be, tid, e, text, 1, locker );
158 rc = bdb_next_id( be, tid, &e->e_id );
160 snprintf( text->bv_val, text->bv_len,
161 "next_id failed: %s (%d)",
162 db_strerror(rc), rc );
164 LDAP_LOG ( TOOLS, ERR,
165 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
167 Debug( LDAP_DEBUG_ANY,
168 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
173 rc = bdb_dn2id_add( be, tid, ei, e, NULL );
175 snprintf( text->bv_val, text->bv_len,
176 "dn2id_add failed: %s (%d)",
177 db_strerror(rc), rc );
179 LDAP_LOG ( TOOLS, ERR,
180 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
182 Debug( LDAP_DEBUG_ANY,
183 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
186 if ( nholes == nhmax - 1 ) {
187 if ( holes == hbuf ) {
188 holes = ch_malloc( nhmax * sizeof(ID) * 2 );
189 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
191 holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
195 ber_dupbv( &holes[nholes].dn, &dn );
196 holes[nholes++].id = e->e_id;
198 } else if ( !hole ) {
201 for ( i=0; i<nholes; i++) {
202 if ( holes[i].id == e->e_id ) {
204 free(holes[i].dn.bv_val);
205 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
209 } else if ( holes[i].id > e->e_id ) {
217 ID bdb_tool_entry_put(
220 struct berval *text )
223 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
228 assert( be != NULL );
229 assert( slapMode & SLAP_TOOL_MODE );
232 assert( text->bv_val );
233 assert( text->bv_val[0] == '\0' ); /* overconservative? */
236 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
237 (long) e->e_id, e->e_dn, 0 );
239 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
240 (long) e->e_id, e->e_dn, 0 );
243 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
244 bdb->bi_db_opflags );
246 snprintf( text->bv_val, text->bv_len,
247 "txn_begin failed: %s (%d)",
248 db_strerror(rc), rc );
250 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
252 Debug( LDAP_DEBUG_ANY,
253 "=> bdb_tool_entry_put: %s\n",
254 text->bv_val, 0, 0 );
259 locker = TXN_ID( tid );
260 /* add dn2id indices */
261 rc = bdb_tool_next_id( be, tid, e, text, 0, locker );
267 rc = bdb_id2entry_add( be, tid, e );
269 snprintf( text->bv_val, text->bv_len,
270 "id2entry_add failed: %s (%d)",
271 db_strerror(rc), rc );
273 LDAP_LOG ( TOOLS, ERR,
274 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
276 Debug( LDAP_DEBUG_ANY,
277 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
283 op.o_tmpmemctx = NULL;
284 op.o_tmpmfuncs = &ch_mfuncs;
285 rc = bdb_index_entry_add( &op, tid, e );
287 snprintf( text->bv_val, text->bv_len,
288 "index_entry_add failed: %s (%d)",
289 db_strerror(rc), rc );
291 LDAP_LOG ( TOOLS, ERR,
292 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
294 Debug( LDAP_DEBUG_ANY,
295 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
302 rc = TXN_COMMIT( tid, 0 );
304 snprintf( text->bv_val, text->bv_len,
305 "txn_commit failed: %s (%d)",
306 db_strerror(rc), rc );
308 LDAP_LOG ( TOOLS, ERR,
309 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
311 Debug( LDAP_DEBUG_ANY,
312 "=> bdb_tool_entry_put: %s\n",
313 text->bv_val, 0, 0 );
320 snprintf( text->bv_val, text->bv_len,
321 "txn_aborted! %s (%d)",
322 db_strerror(rc), rc );
324 LDAP_LOG ( TOOLS, ERR,
325 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
327 Debug( LDAP_DEBUG_ANY,
328 "=> bdb_tool_entry_put: %s\n",
329 text->bv_val, 0, 0 );
337 int bdb_tool_entry_reindex(
341 struct bdb_info *bi = (struct bdb_info *) be->be_private;
348 LDAP_LOG ( TOOLS, ARGS,
349 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
351 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
355 e = bdb_tool_entry_get( be, id );
359 LDAP_LOG ( TOOLS, DETAIL1,
360 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
363 Debug( LDAP_DEBUG_ANY,
364 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
370 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
373 LDAP_LOG ( TOOLS, ERR,
374 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
375 db_strerror(rc), rc, 0 );
377 Debug( LDAP_DEBUG_ANY,
378 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
379 db_strerror(rc), rc, 0 );
385 * just (re)add them for now
386 * assume that some other routine (not yet implemented)
387 * will zap index databases
392 LDAP_LOG ( TOOLS, ERR,
393 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
395 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
396 (long) id, e->e_dn, 0 );
400 /* add dn2id indices */
401 rc = bdb_dn2id_add( be, tid, NULL, e, NULL );
402 if( rc != 0 && rc != DB_KEYEXIST ) {
404 LDAP_LOG ( TOOLS, ERR,
405 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
406 db_strerror(rc), rc, 0 );
408 Debug( LDAP_DEBUG_ANY,
409 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
410 db_strerror(rc), rc, 0 );
417 op.o_tmpmemctx = NULL;
418 op.o_tmpmfuncs = &ch_mfuncs;
419 rc = bdb_index_entry_add( &op, tid, e );
423 rc = TXN_COMMIT( tid, 0 );
426 LDAP_LOG ( TOOLS, ERR,
427 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
428 db_strerror(rc), rc, 0 );
430 Debug( LDAP_DEBUG_ANY,
431 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
432 db_strerror(rc), rc, 0 );
440 LDAP_LOG ( TOOLS, DETAIL1,
441 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
442 db_strerror(rc), rc, 0 );
444 Debug( LDAP_DEBUG_ANY,
445 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
446 db_strerror(rc), rc, 0 );