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(
138 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
139 struct berval dn = e->e_nname;
143 rc = bdb_dn2id( be, tid, &dn, &e->e_id, 0 );
144 if ( rc == DB_NOTFOUND ) {
145 if ( be_issuffix( be, &dn ) ) {
148 dnParent( &dn, &pdn );
150 rc = bdb_tool_next_id( be, tid, e, text, 1 );
155 rc = bdb_next_id( be, tid, &e->e_id );
157 snprintf( text->bv_val, text->bv_len,
158 "next_id failed: %s (%d)",
159 db_strerror(rc), rc );
161 LDAP_LOG ( TOOLS, ERR,
162 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
164 Debug( LDAP_DEBUG_ANY,
165 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
170 rc = bdb_dn2id_add( be, tid, &pdn, e );
172 snprintf( text->bv_val, text->bv_len,
173 "dn2id_add failed: %s (%d)",
174 db_strerror(rc), rc );
176 LDAP_LOG ( TOOLS, ERR,
177 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
179 Debug( LDAP_DEBUG_ANY,
180 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
183 if ( nholes == nhmax - 1 ) {
184 if ( holes == hbuf ) {
185 holes = ch_malloc( nhmax * sizeof(ID) * 2 );
186 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
188 holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
192 ber_dupbv( &holes[nholes].dn, &dn );
193 holes[nholes++].id = e->e_id;
195 } else if ( !hole ) {
198 for ( i=0; i<nholes; i++) {
199 if ( holes[i].id == e->e_id ) {
201 free(holes[i].dn.bv_val);
202 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
206 } else if ( holes[i].id > e->e_id ) {
214 ID bdb_tool_entry_put(
217 struct berval *text )
220 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
224 assert( be != NULL );
225 assert( slapMode & SLAP_TOOL_MODE );
228 assert( text->bv_val );
229 assert( text->bv_val[0] == '\0' ); /* overconservative? */
232 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
233 (long) e->e_id, e->e_dn, 0 );
235 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
236 (long) e->e_id, e->e_dn, 0 );
239 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
240 bdb->bi_db_opflags );
242 snprintf( text->bv_val, text->bv_len,
243 "txn_begin failed: %s (%d)",
244 db_strerror(rc), rc );
246 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
248 Debug( LDAP_DEBUG_ANY,
249 "=> bdb_tool_entry_put: %s\n",
250 text->bv_val, 0, 0 );
255 /* add dn2id indices */
256 rc = bdb_tool_next_id( be, tid, e, text, 0 );
262 rc = bdb_id2entry_add( be, tid, e );
264 snprintf( text->bv_val, text->bv_len,
265 "id2entry_add failed: %s (%d)",
266 db_strerror(rc), rc );
268 LDAP_LOG ( TOOLS, ERR,
269 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
271 Debug( LDAP_DEBUG_ANY,
272 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
277 rc = bdb_index_entry_add( be, tid, e );
279 snprintf( text->bv_val, text->bv_len,
280 "index_entry_add failed: %s (%d)",
281 db_strerror(rc), rc );
283 LDAP_LOG ( TOOLS, ERR,
284 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
286 Debug( LDAP_DEBUG_ANY,
287 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
294 rc = TXN_COMMIT( tid, 0 );
296 snprintf( text->bv_val, text->bv_len,
297 "txn_commit failed: %s (%d)",
298 db_strerror(rc), rc );
300 LDAP_LOG ( TOOLS, ERR,
301 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
303 Debug( LDAP_DEBUG_ANY,
304 "=> bdb_tool_entry_put: %s\n",
305 text->bv_val, 0, 0 );
312 snprintf( text->bv_val, text->bv_len,
313 "txn_aborted! %s (%d)",
314 db_strerror(rc), rc );
316 LDAP_LOG ( TOOLS, ERR,
317 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
319 Debug( LDAP_DEBUG_ANY,
320 "=> bdb_tool_entry_put: %s\n",
321 text->bv_val, 0, 0 );
329 int bdb_tool_entry_reindex(
333 struct bdb_info *bi = (struct bdb_info *) be->be_private;
340 LDAP_LOG ( TOOLS, ARGS,
341 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
343 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
347 e = bdb_tool_entry_get( be, id );
351 LDAP_LOG ( TOOLS, DETAIL1,
352 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
355 Debug( LDAP_DEBUG_ANY,
356 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
362 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
365 LDAP_LOG ( TOOLS, ERR,
366 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
367 db_strerror(rc), rc, 0 );
369 Debug( LDAP_DEBUG_ANY,
370 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
371 db_strerror(rc), rc, 0 );
377 * just (re)add them for now
378 * assume that some other routine (not yet implemented)
379 * will zap index databases
384 LDAP_LOG ( TOOLS, ERR,
385 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
387 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
388 (long) id, e->e_dn, 0 );
391 /* add dn2id indices */
392 if ( be_issuffix( be, &e->e_nname ) ) {
395 dnParent( &e->e_nname, &pdn );
397 rc = bdb_dn2id_add( be, tid, &pdn, e );
398 if( rc != 0 && rc != DB_KEYEXIST ) {
400 LDAP_LOG ( TOOLS, ERR,
401 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
402 db_strerror(rc), rc, 0 );
404 Debug( LDAP_DEBUG_ANY,
405 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
406 db_strerror(rc), rc, 0 );
411 rc = bdb_index_entry_add( be, tid, e );
415 rc = TXN_COMMIT( tid, 0 );
418 LDAP_LOG ( TOOLS, ERR,
419 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
420 db_strerror(rc), rc, 0 );
422 Debug( LDAP_DEBUG_ANY,
423 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
424 db_strerror(rc), rc, 0 );
432 LDAP_LOG ( TOOLS, DETAIL1,
433 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
434 db_strerror(rc), rc, 0 );
436 Debug( LDAP_DEBUG_ANY,
437 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
438 db_strerror(rc), rc, 0 );