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 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' ); /* overconservative? */
129 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
130 (long) e->e_id, e->e_dn, 0 );
132 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
133 (long) e->e_id, e->e_dn, 0 );
136 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
137 bdb->bi_db_opflags );
139 snprintf( text->bv_val, text->bv_len,
140 "txn_begin failed: %s (%d)",
141 db_strerror(rc), rc );
143 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
145 Debug( LDAP_DEBUG_ANY,
146 "=> bdb_tool_entry_put: %s\n",
147 text->bv_val, 0, 0 );
152 rc = bdb_next_id( be, tid, &e->e_id );
154 snprintf( text->bv_val, text->bv_len,
155 "next_id failed: %s (%d)",
156 db_strerror(rc), rc );
158 LDAP_LOG ( TOOLS, ERR,
159 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
161 Debug( LDAP_DEBUG_ANY,
162 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
167 /* add dn2id indices */
168 if ( be_issuffix( be, &e->e_nname ) ) {
171 dnParent( &e->e_nname, &pdn );
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 );
189 rc = bdb_id2entry_add( be, tid, e );
191 snprintf( text->bv_val, text->bv_len,
192 "id2entry_add failed: %s (%d)",
193 db_strerror(rc), rc );
195 LDAP_LOG ( TOOLS, ERR,
196 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
198 Debug( LDAP_DEBUG_ANY,
199 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
204 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
206 snprintf( text->bv_val, text->bv_len,
207 "index_entry_add failed: %s (%d)",
208 db_strerror(rc), rc );
210 LDAP_LOG ( TOOLS, ERR,
211 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
213 Debug( LDAP_DEBUG_ANY,
214 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
221 rc = TXN_COMMIT( tid, 0 );
223 snprintf( text->bv_val, text->bv_len,
224 "txn_commit failed: %s (%d)",
225 db_strerror(rc), rc );
227 LDAP_LOG ( TOOLS, ERR,
228 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
230 Debug( LDAP_DEBUG_ANY,
231 "=> bdb_tool_entry_put: %s\n",
232 text->bv_val, 0, 0 );
239 snprintf( text->bv_val, text->bv_len,
240 "txn_aborted! %s (%d)",
241 db_strerror(rc), rc );
243 LDAP_LOG ( TOOLS, ERR,
244 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
246 Debug( LDAP_DEBUG_ANY,
247 "=> bdb_tool_entry_put: %s\n",
248 text->bv_val, 0, 0 );
256 int bdb_tool_entry_reindex(
260 struct bdb_info *bi = (struct bdb_info *) be->be_private;
267 LDAP_LOG ( TOOLS, ARGS,
268 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
270 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
274 e = bdb_tool_entry_get( be, id );
278 LDAP_LOG ( TOOLS, DETAIL1,
279 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
282 Debug( LDAP_DEBUG_ANY,
283 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
289 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
292 LDAP_LOG ( TOOLS, ERR,
293 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
294 db_strerror(rc), rc, 0 );
296 Debug( LDAP_DEBUG_ANY,
297 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
298 db_strerror(rc), rc, 0 );
304 * just (re)add them for now
305 * assume that some other routine (not yet implemented)
306 * will zap index databases
311 LDAP_LOG ( TOOLS, ERR,
312 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
314 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
315 (long) id, e->e_dn, 0 );
318 /* add dn2id indices */
319 if ( be_issuffix( be, &e->e_nname ) ) {
322 dnParent( &e->e_nname, &pdn );
324 rc = bdb_dn2id_add( be, tid, &pdn, e );
325 if( rc != 0 && rc != DB_KEYEXIST ) {
327 LDAP_LOG ( TOOLS, ERR,
328 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
329 db_strerror(rc), rc, 0 );
331 Debug( LDAP_DEBUG_ANY,
332 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
333 db_strerror(rc), rc, 0 );
338 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
342 rc = TXN_COMMIT( tid, 0 );
345 LDAP_LOG ( TOOLS, ERR,
346 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
347 db_strerror(rc), rc, 0 );
349 Debug( LDAP_DEBUG_ANY,
350 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
351 db_strerror(rc), rc, 0 );
359 LDAP_LOG ( TOOLS, DETAIL1,
360 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
361 db_strerror(rc), rc, 0 );
363 Debug( LDAP_DEBUG_ANY,
364 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
365 db_strerror(rc), rc, 0 );