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>
16 static DBC *cursor = NULL;
19 typedef struct dn_id {
24 #define HOLE_SIZE 4096
25 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
26 static unsigned nhmax = HOLE_SIZE;
27 static unsigned nholes;
29 int bdb_tool_entry_open(
30 BackendDB *be, int mode )
32 /* initialize key and data thangs */
35 key.flags = DB_DBT_REALLOC;
36 data.flags = DB_DBT_REALLOC;
41 int bdb_tool_entry_close(
56 cursor->c_close( cursor );
62 fprintf( stderr, "Error, entries missing!\n");
63 for (i=0; i<nholes; i++) {
64 fprintf(stderr, " entry %ld: %s\n",
65 holes[i].id, holes[i].dn.bv_val, 0);
73 ID bdb_tool_entry_next(
78 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
81 assert( slapMode & SLAP_TOOL_MODE );
82 assert( bdb != NULL );
85 rc = bdb->bi_id2entry->bdi_db->cursor(
86 bdb->bi_id2entry->bdi_db, NULL, &cursor,
93 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
99 if( data.data == NULL ) {
103 AC_MEMCPY( &id, key.data, key.size );
107 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
113 assert( be != NULL );
114 assert( slapMode & SLAP_TOOL_MODE );
115 assert( data.data != NULL );
118 DBT2bv( &data, &bv );
120 rc = entry_decode( &bv, &e );
122 if( rc == LDAP_SUCCESS ) {
127 EntryInfo *ei = NULL;
128 rc = bdb_cache_find_id( be, NULL, id, &ei, 0, 0,
130 if ( rc == LDAP_SUCCESS )
137 static int bdb_tool_next_id(
145 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
146 struct berval dn = e->e_nname;
148 EntryInfo *ei = NULL;
151 rc = bdb_cache_find_ndn( be, tid, &dn, &ei, locker, NULL );
152 if ( ei ) bdb_cache_entryinfo_unlock( ei );
153 if ( rc == DB_NOTFOUND ) {
154 if ( be_issuffix( be, &dn ) ) {
157 dnParent( &dn, &pdn );
159 rc = bdb_tool_next_id( be, tid, e, text, 1, locker );
164 rc = bdb_next_id( be, tid, &e->e_id );
166 snprintf( text->bv_val, text->bv_len,
167 "next_id failed: %s (%d)",
168 db_strerror(rc), rc );
170 LDAP_LOG ( TOOLS, ERR,
171 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
173 Debug( LDAP_DEBUG_ANY,
174 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
179 rc = bdb_dn2id_add( be, tid, ei, e, NULL );
181 snprintf( text->bv_val, text->bv_len,
182 "dn2id_add failed: %s (%d)",
183 db_strerror(rc), rc );
185 LDAP_LOG ( TOOLS, ERR,
186 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
188 Debug( LDAP_DEBUG_ANY,
189 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
192 if ( nholes == nhmax - 1 ) {
193 if ( holes == hbuf ) {
194 holes = ch_malloc( nhmax * sizeof(ID) * 2 );
195 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
197 holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
201 ber_dupbv( &holes[nholes].dn, &dn );
202 holes[nholes++].id = e->e_id;
204 } else if ( !hole ) {
207 for ( i=0; i<nholes; i++) {
208 if ( holes[i].id == e->e_id ) {
210 free(holes[i].dn.bv_val);
211 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
215 } else if ( holes[i].id > e->e_id ) {
223 ID bdb_tool_entry_put(
226 struct berval *text )
229 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
234 assert( be != NULL );
235 assert( slapMode & SLAP_TOOL_MODE );
238 assert( text->bv_val );
239 assert( text->bv_val[0] == '\0' ); /* overconservative? */
242 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
243 (long) e->e_id, e->e_dn, 0 );
245 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
246 (long) e->e_id, e->e_dn, 0 );
249 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
250 bdb->bi_db_opflags );
252 snprintf( text->bv_val, text->bv_len,
253 "txn_begin failed: %s (%d)",
254 db_strerror(rc), rc );
256 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
258 Debug( LDAP_DEBUG_ANY,
259 "=> bdb_tool_entry_put: %s\n",
260 text->bv_val, 0, 0 );
265 locker = TXN_ID( tid );
266 /* add dn2id indices */
267 rc = bdb_tool_next_id( be, tid, e, text, 0, locker );
273 rc = bdb_id2entry_add( be, tid, e );
275 snprintf( text->bv_val, text->bv_len,
276 "id2entry_add failed: %s (%d)",
277 db_strerror(rc), rc );
279 LDAP_LOG ( TOOLS, ERR,
280 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
282 Debug( LDAP_DEBUG_ANY,
283 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
289 op.o_tmpmemctx = NULL;
290 op.o_tmpmfuncs = &ch_mfuncs;
291 rc = bdb_index_entry_add( &op, tid, e );
293 snprintf( text->bv_val, text->bv_len,
294 "index_entry_add failed: %s (%d)",
295 db_strerror(rc), rc );
297 LDAP_LOG ( TOOLS, ERR,
298 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
300 Debug( LDAP_DEBUG_ANY,
301 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
308 rc = TXN_COMMIT( tid, 0 );
310 snprintf( text->bv_val, text->bv_len,
311 "txn_commit failed: %s (%d)",
312 db_strerror(rc), rc );
314 LDAP_LOG ( TOOLS, ERR,
315 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
317 Debug( LDAP_DEBUG_ANY,
318 "=> bdb_tool_entry_put: %s\n",
319 text->bv_val, 0, 0 );
326 snprintf( text->bv_val, text->bv_len,
327 "txn_aborted! %s (%d)",
328 db_strerror(rc), rc );
330 LDAP_LOG ( TOOLS, ERR,
331 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
333 Debug( LDAP_DEBUG_ANY,
334 "=> bdb_tool_entry_put: %s\n",
335 text->bv_val, 0, 0 );
343 int bdb_tool_entry_reindex(
347 struct bdb_info *bi = (struct bdb_info *) be->be_private;
354 LDAP_LOG ( TOOLS, ARGS,
355 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
357 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
361 e = bdb_tool_entry_get( be, id );
365 LDAP_LOG ( TOOLS, DETAIL1,
366 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
369 Debug( LDAP_DEBUG_ANY,
370 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
376 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
379 LDAP_LOG ( TOOLS, ERR,
380 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
381 db_strerror(rc), rc, 0 );
383 Debug( LDAP_DEBUG_ANY,
384 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
385 db_strerror(rc), rc, 0 );
391 * just (re)add them for now
392 * assume that some other routine (not yet implemented)
393 * will zap index databases
398 LDAP_LOG ( TOOLS, ERR,
399 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
401 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
402 (long) id, e->e_dn, 0 );
406 /* add dn2id indices */
407 rc = bdb_dn2id_add( be, tid, NULL, e, NULL );
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 );
423 op.o_tmpmemctx = NULL;
424 op.o_tmpmfuncs = &ch_mfuncs;
425 rc = bdb_index_entry_add( &op, tid, e );
429 rc = TXN_COMMIT( tid, 0 );
432 LDAP_LOG ( TOOLS, ERR,
433 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
434 db_strerror(rc), rc, 0 );
436 Debug( LDAP_DEBUG_ANY,
437 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
438 db_strerror(rc), rc, 0 );
446 LDAP_LOG ( TOOLS, DETAIL1,
447 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
448 db_strerror(rc), rc, 0 );
450 Debug( LDAP_DEBUG_ANY,
451 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
452 db_strerror(rc), rc, 0 );