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' );
130 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
131 (long) e->e_id, e->e_dn, 0 );
133 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
134 bdb->bi_db_opflags );
136 snprintf( text->bv_val, text->bv_len,
137 "txn_begin failed: %s (%d)",
138 db_strerror(rc), rc );
139 Debug( LDAP_DEBUG_ANY,
140 "=> bdb_tool_entry_put: %s\n",
141 text->bv_val, 0, 0 );
145 rc = bdb_next_id( be, tid, &e->e_id );
147 snprintf( text->bv_val, text->bv_len,
148 "next_id failed: %s (%d)",
149 db_strerror(rc), rc );
150 Debug( LDAP_DEBUG_ANY,
151 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
155 /* add dn2id indices */
156 if ( be_issuffix( be, &e->e_nname ) ) {
159 dnParent( &e->e_nname, &pdn );
161 rc = bdb_dn2id_add( be, tid, &pdn, e );
163 snprintf( text->bv_val, text->bv_len,
164 "dn2id_add failed: %s (%d)",
165 db_strerror(rc), rc );
166 Debug( LDAP_DEBUG_ANY,
167 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
172 rc = bdb_id2entry_add( be, tid, e );
174 snprintf( text->bv_val, text->bv_len,
175 "id2entry_add failed: %s (%d)",
176 db_strerror(rc), rc );
177 Debug( LDAP_DEBUG_ANY,
178 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
182 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
184 snprintf( text->bv_val, text->bv_len,
185 "index_entry_add failed: %s (%d)",
186 db_strerror(rc), rc );
187 Debug( LDAP_DEBUG_ANY,
188 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
194 rc = TXN_COMMIT( tid, 0 );
196 snprintf( text->bv_val, text->bv_len,
197 "txn_commit failed: %s (%d)",
198 db_strerror(rc), rc );
199 Debug( LDAP_DEBUG_ANY,
200 "=> bdb_tool_entry_put: %s\n",
201 text->bv_val, 0, 0 );
207 snprintf( text->bv_val, text->bv_len,
208 "txn_aborted! %s (%d)",
209 db_strerror(rc), rc );
210 Debug( LDAP_DEBUG_ANY,
211 "=> bdb_tool_entry_put: %s\n",
212 text->bv_val, 0, 0 );
219 int bdb_tool_entry_reindex(
223 struct bdb_info *bi = (struct bdb_info *) be->be_private;
229 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
232 e = bdb_tool_entry_get( be, id );
235 Debug( LDAP_DEBUG_ANY,
236 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
241 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
243 Debug( LDAP_DEBUG_ANY,
244 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
245 db_strerror(rc), rc, 0 );
250 * just (re)add them for now
251 * assume that some other routine (not yet implemented)
252 * will zap index databases
256 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
257 (long) id, e->e_dn, 0 );
259 /* add dn2id indices */
260 if ( be_issuffix( be, &e->e_nname ) ) {
263 dnParent( &e->e_nname, &pdn );
265 rc = bdb_dn2id_add( be, tid, &pdn, e );
266 if( rc != 0 && rc != DB_KEYEXIST ) {
267 Debug( LDAP_DEBUG_ANY,
268 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
269 db_strerror(rc), rc, 0 );
273 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
277 rc = TXN_COMMIT( tid, 0 );
279 Debug( LDAP_DEBUG_ANY,
280 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
281 db_strerror(rc), rc, 0 );
287 Debug( LDAP_DEBUG_ANY,
288 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
289 db_strerror(rc), rc, 0 );