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, &pdn, e );
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;
229 assert( be != NULL );
230 assert( slapMode & SLAP_TOOL_MODE );
233 assert( text->bv_val );
234 assert( text->bv_val[0] == '\0' ); /* overconservative? */
237 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
238 (long) e->e_id, e->e_dn, 0 );
240 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
241 (long) e->e_id, e->e_dn, 0 );
244 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
245 bdb->bi_db_opflags );
247 snprintf( text->bv_val, text->bv_len,
248 "txn_begin failed: %s (%d)",
249 db_strerror(rc), rc );
251 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
253 Debug( LDAP_DEBUG_ANY,
254 "=> bdb_tool_entry_put: %s\n",
255 text->bv_val, 0, 0 );
260 locker = TXN_ID( tid );
261 /* add dn2id indices */
262 rc = bdb_tool_next_id( be, tid, e, text, 0, locker );
268 rc = bdb_id2entry_add( be, tid, e );
270 snprintf( text->bv_val, text->bv_len,
271 "id2entry_add failed: %s (%d)",
272 db_strerror(rc), rc );
274 LDAP_LOG ( TOOLS, ERR,
275 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
277 Debug( LDAP_DEBUG_ANY,
278 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
284 op.o_tmpmemctx = NULL;
285 op.o_tmpmfuncs = &ch_mfuncs;
286 rc = bdb_index_entry_add( &op, tid, e );
288 snprintf( text->bv_val, text->bv_len,
289 "index_entry_add failed: %s (%d)",
290 db_strerror(rc), rc );
292 LDAP_LOG ( TOOLS, ERR,
293 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
295 Debug( LDAP_DEBUG_ANY,
296 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
303 rc = TXN_COMMIT( tid, 0 );
305 snprintf( text->bv_val, text->bv_len,
306 "txn_commit failed: %s (%d)",
307 db_strerror(rc), rc );
309 LDAP_LOG ( TOOLS, ERR,
310 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
312 Debug( LDAP_DEBUG_ANY,
313 "=> bdb_tool_entry_put: %s\n",
314 text->bv_val, 0, 0 );
321 snprintf( text->bv_val, text->bv_len,
322 "txn_aborted! %s (%d)",
323 db_strerror(rc), rc );
325 LDAP_LOG ( TOOLS, ERR,
326 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
328 Debug( LDAP_DEBUG_ANY,
329 "=> bdb_tool_entry_put: %s\n",
330 text->bv_val, 0, 0 );
338 int bdb_tool_entry_reindex(
342 struct bdb_info *bi = (struct bdb_info *) be->be_private;
350 LDAP_LOG ( TOOLS, ARGS,
351 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
353 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
357 e = bdb_tool_entry_get( be, id );
361 LDAP_LOG ( TOOLS, DETAIL1,
362 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
365 Debug( LDAP_DEBUG_ANY,
366 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
372 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
375 LDAP_LOG ( TOOLS, ERR,
376 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
377 db_strerror(rc), rc, 0 );
379 Debug( LDAP_DEBUG_ANY,
380 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
381 db_strerror(rc), rc, 0 );
387 * just (re)add them for now
388 * assume that some other routine (not yet implemented)
389 * will zap index databases
394 LDAP_LOG ( TOOLS, ERR,
395 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
397 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
398 (long) id, e->e_dn, 0 );
401 /* add dn2id indices */
402 if ( be_issuffix( be, &e->e_nname ) ) {
405 dnParent( &e->e_nname, &pdn );
407 rc = bdb_dn2id_add( be, tid, &pdn, e );
408 if( rc != 0 && rc != DB_KEYEXIST ) {
410 LDAP_LOG ( TOOLS, ERR,
411 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
412 db_strerror(rc), rc, 0 );
414 Debug( LDAP_DEBUG_ANY,
415 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
416 db_strerror(rc), rc, 0 );
422 op.o_tmpmemctx = NULL;
423 op.o_tmpmfuncs = &ch_mfuncs;
424 rc = bdb_index_entry_add( &op, tid, e );
428 rc = TXN_COMMIT( tid, 0 );
431 LDAP_LOG ( TOOLS, ERR,
432 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
433 db_strerror(rc), rc, 0 );
435 Debug( LDAP_DEBUG_ANY,
436 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
437 db_strerror(rc), rc, 0 );
445 LDAP_LOG ( TOOLS, DETAIL1,
446 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
447 db_strerror(rc), rc, 0 );
449 Debug( LDAP_DEBUG_ANY,
450 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
451 db_strerror(rc), rc, 0 );