1 /* tools.c - tools for slap tools */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
15 static DBC *cursor = NULL;
18 int bdb_tool_entry_open(
19 BackendDB *be, int mode )
21 /* initialize key and data thangs */
24 key.flags = DB_DBT_REALLOC;
25 data.flags = DB_DBT_REALLOC;
30 int bdb_tool_entry_close(
33 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
47 cursor->c_close( cursor );
54 ID bdb_tool_entry_next(
59 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
62 assert( slapMode & SLAP_TOOL_MODE );
63 assert( bdb != NULL );
66 rc = bdb->bi_id2entry->bdi_db->cursor(
67 bdb->bi_id2entry->bdi_db, NULL, &cursor,
74 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
80 if( data.data == NULL ) {
84 AC_MEMCPY( &id, key.data, key.size );
88 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
95 assert( slapMode & SLAP_TOOL_MODE );
96 assert( data.data != NULL );
100 rc = entry_decode( &bv, &e );
102 if( rc == LDAP_SUCCESS ) {
107 bdb_fix_dn(be, id, e);
113 ID bdb_tool_entry_put(
116 struct berval *text )
119 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
123 assert( be != NULL );
124 assert( slapMode & SLAP_TOOL_MODE );
127 assert( text->bv_val );
128 assert( text->bv_val[0] == '\0' );
131 LDAP_LOG (( "tools", LDAP_LEVEL_ARGS,
132 "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
133 (long) e->e_id, e->e_dn ));
135 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
136 (long) e->e_id, e->e_dn, 0 );
139 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
140 bdb->bi_db_opflags );
142 snprintf( text->bv_val, text->bv_len,
143 "txn_begin failed: %s (%d)",
144 db_strerror(rc), rc );
146 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
147 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
149 Debug( LDAP_DEBUG_ANY,
150 "=> bdb_tool_entry_put: %s\n",
151 text->bv_val, 0, 0 );
156 rc = bdb_next_id( be, tid, &e->e_id );
158 snprintf( text->bv_val, text->bv_len,
159 "next_id failed: %s (%d)",
160 db_strerror(rc), rc );
162 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
163 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
165 Debug( LDAP_DEBUG_ANY,
166 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
171 /* add dn2id indices */
172 if ( be_issuffix( be, &e->e_nname ) ) {
175 dnParent( &e->e_nname, &pdn );
177 rc = bdb_dn2id_add( be, tid, &pdn, e );
179 snprintf( text->bv_val, text->bv_len,
180 "dn2id_add failed: %s (%d)",
181 db_strerror(rc), rc );
183 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
184 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
186 Debug( LDAP_DEBUG_ANY,
187 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
193 rc = bdb_id2entry_add( be, tid, e );
195 snprintf( text->bv_val, text->bv_len,
196 "id2entry_add failed: %s (%d)",
197 db_strerror(rc), rc );
199 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
200 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
202 Debug( LDAP_DEBUG_ANY,
203 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
208 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
210 snprintf( text->bv_val, text->bv_len,
211 "index_entry_add failed: %s (%d)",
212 db_strerror(rc), rc );
214 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
215 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
217 Debug( LDAP_DEBUG_ANY,
218 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
225 rc = TXN_COMMIT( tid, 0 );
227 snprintf( text->bv_val, text->bv_len,
228 "txn_commit failed: %s (%d)",
229 db_strerror(rc), rc );
231 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
232 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
234 Debug( LDAP_DEBUG_ANY,
235 "=> bdb_tool_entry_put: %s\n",
236 text->bv_val, 0, 0 );
243 snprintf( text->bv_val, text->bv_len,
244 "txn_aborted! %s (%d)",
245 db_strerror(rc), rc );
247 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
248 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
250 Debug( LDAP_DEBUG_ANY,
251 "=> bdb_tool_entry_put: %s\n",
252 text->bv_val, 0, 0 );
260 int bdb_tool_entry_reindex(
264 struct bdb_info *bi = (struct bdb_info *) be->be_private;
271 LDAP_LOG (( "tools", LDAP_LEVEL_ARGS,
272 "=> bdb_tool_entry_reindex( %ld )\n", (long) id ));
274 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
278 e = bdb_tool_entry_get( be, id );
282 LDAP_LOG (( "tools", LDAP_LEVEL_DETAIL1,
283 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
286 Debug( LDAP_DEBUG_ANY,
287 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
293 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
296 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
297 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
298 db_strerror(rc), rc ));
300 Debug( LDAP_DEBUG_ANY,
301 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
302 db_strerror(rc), rc, 0 );
308 * just (re)add them for now
309 * assume that some other routine (not yet implemented)
310 * will zap index databases
315 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
316 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
317 (long) id, e->e_dn ));
319 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
320 (long) id, e->e_dn, 0 );
323 /* add dn2id indices */
324 if ( be_issuffix( be, &e->e_nname ) ) {
327 dnParent( &e->e_nname, &pdn );
329 rc = bdb_dn2id_add( be, tid, &pdn, e );
330 if( rc != 0 && rc != DB_KEYEXIST ) {
332 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
333 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
334 db_strerror(rc), rc ));
336 Debug( LDAP_DEBUG_ANY,
337 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
338 db_strerror(rc), rc, 0 );
343 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
347 rc = TXN_COMMIT( tid, 0 );
350 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
351 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
352 db_strerror(rc), rc ));
354 Debug( LDAP_DEBUG_ANY,
355 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
356 db_strerror(rc), rc, 0 );
364 LDAP_LOG (( "tools", LDAP_LEVEL_DETAIL1,
365 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
366 db_strerror(rc), rc ));
368 Debug( LDAP_DEBUG_ANY,
369 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
370 db_strerror(rc), rc, 0 );