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(
45 cursor->c_close( cursor );
52 ID bdb_tool_entry_next(
57 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
60 assert( slapMode & SLAP_TOOL_MODE );
61 assert( bdb != NULL );
64 rc = bdb->bi_id2entry->bdi_db->cursor(
65 bdb->bi_id2entry->bdi_db, NULL, &cursor,
72 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
78 if( data.data == NULL ) {
82 AC_MEMCPY( &id, key.data, key.size );
86 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
93 assert( slapMode & SLAP_TOOL_MODE );
94 assert( data.data != NULL );
98 rc = entry_decode( &bv, &e );
100 if( rc == LDAP_SUCCESS ) {
105 bdb_fix_dn(be, id, e);
111 ID bdb_tool_entry_put(
114 struct berval *text )
117 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
121 assert( be != NULL );
122 assert( slapMode & SLAP_TOOL_MODE );
125 assert( text->bv_val );
126 assert( text->bv_val[0] == '\0' );
129 LDAP_LOG (( "tools", LDAP_LEVEL_ARGS,
130 "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
131 (long) e->e_id, e->e_dn ));
133 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
134 (long) e->e_id, e->e_dn, 0 );
137 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
138 bdb->bi_db_opflags );
140 snprintf( text->bv_val, text->bv_len,
141 "txn_begin failed: %s (%d)",
142 db_strerror(rc), rc );
144 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
145 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
147 Debug( LDAP_DEBUG_ANY,
148 "=> bdb_tool_entry_put: %s\n",
149 text->bv_val, 0, 0 );
154 rc = bdb_next_id( be, tid, &e->e_id );
156 snprintf( text->bv_val, text->bv_len,
157 "next_id failed: %s (%d)",
158 db_strerror(rc), rc );
160 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
161 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
163 Debug( LDAP_DEBUG_ANY,
164 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
169 /* add dn2id indices */
170 if ( be_issuffix( be, &e->e_nname ) ) {
173 dnParent( &e->e_nname, &pdn );
175 rc = bdb_dn2id_add( be, tid, &pdn, e );
177 snprintf( text->bv_val, text->bv_len,
178 "dn2id_add failed: %s (%d)",
179 db_strerror(rc), rc );
181 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
182 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
184 Debug( LDAP_DEBUG_ANY,
185 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
191 rc = bdb_id2entry_add( be, tid, e );
193 snprintf( text->bv_val, text->bv_len,
194 "id2entry_add failed: %s (%d)",
195 db_strerror(rc), rc );
197 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
198 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
200 Debug( LDAP_DEBUG_ANY,
201 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
206 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
208 snprintf( text->bv_val, text->bv_len,
209 "index_entry_add failed: %s (%d)",
210 db_strerror(rc), rc );
212 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
213 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
215 Debug( LDAP_DEBUG_ANY,
216 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
223 rc = TXN_COMMIT( tid, 0 );
225 snprintf( text->bv_val, text->bv_len,
226 "txn_commit failed: %s (%d)",
227 db_strerror(rc), rc );
229 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
230 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
232 Debug( LDAP_DEBUG_ANY,
233 "=> bdb_tool_entry_put: %s\n",
234 text->bv_val, 0, 0 );
241 snprintf( text->bv_val, text->bv_len,
242 "txn_aborted! %s (%d)",
243 db_strerror(rc), rc );
245 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
246 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
248 Debug( LDAP_DEBUG_ANY,
249 "=> bdb_tool_entry_put: %s\n",
250 text->bv_val, 0, 0 );
258 int bdb_tool_entry_reindex(
262 struct bdb_info *bi = (struct bdb_info *) be->be_private;
269 LDAP_LOG (( "tools", LDAP_LEVEL_ARGS,
270 "=> bdb_tool_entry_reindex( %ld )\n", (long) id ));
272 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
276 e = bdb_tool_entry_get( be, id );
280 LDAP_LOG (( "tools", LDAP_LEVEL_DETAIL1,
281 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
284 Debug( LDAP_DEBUG_ANY,
285 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
291 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
294 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
295 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
296 db_strerror(rc), rc ));
298 Debug( LDAP_DEBUG_ANY,
299 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
300 db_strerror(rc), rc, 0 );
306 * just (re)add them for now
307 * assume that some other routine (not yet implemented)
308 * will zap index databases
313 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
314 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
315 (long) id, e->e_dn ));
317 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
318 (long) id, e->e_dn, 0 );
321 /* add dn2id indices */
322 if ( be_issuffix( be, &e->e_nname ) ) {
325 dnParent( &e->e_nname, &pdn );
327 rc = bdb_dn2id_add( be, tid, &pdn, e );
328 if( rc != 0 && rc != DB_KEYEXIST ) {
330 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
331 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
332 db_strerror(rc), rc ));
334 Debug( LDAP_DEBUG_ANY,
335 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
336 db_strerror(rc), rc, 0 );
341 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
345 rc = TXN_COMMIT( tid, 0 );
348 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
349 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
350 db_strerror(rc), rc ));
352 Debug( LDAP_DEBUG_ANY,
353 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
354 db_strerror(rc), rc, 0 );
362 LDAP_LOG (( "tools", LDAP_LEVEL_DETAIL1,
363 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
364 db_strerror(rc), rc ));
366 Debug( LDAP_DEBUG_ANY,
367 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
368 db_strerror(rc), rc, 0 );