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 int bdb_tool_next_id(
115 struct berval *text )
117 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
118 struct berval dn = e->e_nname;
122 rc = bdb_dn2id( be, tid, &dn, &e->e_id, 0 );
123 if ( rc == DB_NOTFOUND ) {
124 if ( be_issuffix( be, &dn ) ) {
127 dnParent( &dn, &pdn );
129 rc = bdb_tool_next_id( be, tid, e, text );
134 rc = bdb_next_id( be, tid, &e->e_id );
136 snprintf( text->bv_val, text->bv_len,
137 "next_id failed: %s (%d)",
138 db_strerror(rc), rc );
140 LDAP_LOG ( TOOLS, ERR,
141 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
143 Debug( LDAP_DEBUG_ANY,
144 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
149 rc = bdb_dn2id_add( be, tid, &pdn, e );
151 snprintf( text->bv_val, text->bv_len,
152 "dn2id_add failed: %s (%d)",
153 db_strerror(rc), rc );
155 LDAP_LOG ( TOOLS, ERR,
156 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
158 Debug( LDAP_DEBUG_ANY,
159 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
166 ID bdb_tool_entry_put(
169 struct berval *text )
172 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
176 assert( be != NULL );
177 assert( slapMode & SLAP_TOOL_MODE );
180 assert( text->bv_val );
181 assert( text->bv_val[0] == '\0' ); /* overconservative? */
184 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
185 (long) e->e_id, e->e_dn, 0 );
187 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
188 (long) e->e_id, e->e_dn, 0 );
191 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
192 bdb->bi_db_opflags );
194 snprintf( text->bv_val, text->bv_len,
195 "txn_begin failed: %s (%d)",
196 db_strerror(rc), rc );
198 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
200 Debug( LDAP_DEBUG_ANY,
201 "=> bdb_tool_entry_put: %s\n",
202 text->bv_val, 0, 0 );
207 /* add dn2id indices */
208 rc = bdb_tool_next_id( be, tid, e, text );
214 rc = bdb_id2entry_add( be, tid, e );
216 snprintf( text->bv_val, text->bv_len,
217 "id2entry_add failed: %s (%d)",
218 db_strerror(rc), rc );
220 LDAP_LOG ( TOOLS, ERR,
221 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
223 Debug( LDAP_DEBUG_ANY,
224 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
229 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
231 snprintf( text->bv_val, text->bv_len,
232 "index_entry_add failed: %s (%d)",
233 db_strerror(rc), rc );
235 LDAP_LOG ( TOOLS, ERR,
236 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
238 Debug( LDAP_DEBUG_ANY,
239 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
246 rc = TXN_COMMIT( tid, 0 );
248 snprintf( text->bv_val, text->bv_len,
249 "txn_commit failed: %s (%d)",
250 db_strerror(rc), rc );
252 LDAP_LOG ( TOOLS, ERR,
253 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
255 Debug( LDAP_DEBUG_ANY,
256 "=> bdb_tool_entry_put: %s\n",
257 text->bv_val, 0, 0 );
264 snprintf( text->bv_val, text->bv_len,
265 "txn_aborted! %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",
273 text->bv_val, 0, 0 );
281 int bdb_tool_entry_reindex(
285 struct bdb_info *bi = (struct bdb_info *) be->be_private;
292 LDAP_LOG ( TOOLS, ARGS,
293 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
295 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
299 e = bdb_tool_entry_get( be, id );
303 LDAP_LOG ( TOOLS, DETAIL1,
304 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
307 Debug( LDAP_DEBUG_ANY,
308 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
314 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
317 LDAP_LOG ( TOOLS, ERR,
318 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
319 db_strerror(rc), rc, 0 );
321 Debug( LDAP_DEBUG_ANY,
322 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
323 db_strerror(rc), rc, 0 );
329 * just (re)add them for now
330 * assume that some other routine (not yet implemented)
331 * will zap index databases
336 LDAP_LOG ( TOOLS, ERR,
337 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
339 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
340 (long) id, e->e_dn, 0 );
343 /* add dn2id indices */
344 if ( be_issuffix( be, &e->e_nname ) ) {
347 dnParent( &e->e_nname, &pdn );
349 rc = bdb_dn2id_add( be, tid, &pdn, e );
350 if( rc != 0 && rc != DB_KEYEXIST ) {
352 LDAP_LOG ( TOOLS, ERR,
353 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
354 db_strerror(rc), rc, 0 );
356 Debug( LDAP_DEBUG_ANY,
357 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
358 db_strerror(rc), rc, 0 );
363 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
367 rc = TXN_COMMIT( tid, 0 );
370 LDAP_LOG ( TOOLS, ERR,
371 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
372 db_strerror(rc), rc, 0 );
374 Debug( LDAP_DEBUG_ANY,
375 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
376 db_strerror(rc), rc, 0 );
384 LDAP_LOG ( TOOLS, DETAIL1,
385 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
386 db_strerror(rc), rc, 0 );
388 Debug( LDAP_DEBUG_ANY,
389 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
390 db_strerror(rc), rc, 0 );