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 );
134 rc = txn_begin( bdb->bi_dbenv, NULL, &tid,
135 bdb->bi_db_opflags );
137 snprintf( text->bv_val, text->bv_len,
138 "txn_begin failed: %s (%d)",
139 db_strerror(rc), rc );
140 Debug( LDAP_DEBUG_ANY,
141 "=> bdb_tool_entry_put: %s\n",
142 text->bv_val, 0, 0 );
147 rc = bdb_next_id( be, tid, &e->e_id );
149 snprintf( text->bv_val, text->bv_len,
150 "next_id failed: %s (%d)",
151 db_strerror(rc), rc );
152 Debug( LDAP_DEBUG_ANY,
153 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
157 /* add dn2id indices */
158 if ( be_issuffix( be, &e->e_nname ) ) {
161 rc = dnParent( &e->e_nname, &pdn );
162 if ( rc != LDAP_SUCCESS ) {
163 Debug( LDAP_DEBUG_ANY, "=> bdb_tool_entry_put: "
164 "dnParent(\"%s\") failed\n",
165 e->e_nname.bv_val, 0, 0 );
170 rc = bdb_dn2id_add( be, tid, &pdn, e );
172 snprintf( text->bv_val, text->bv_len,
173 "dn2id_add failed: %s (%d)",
174 db_strerror(rc), rc );
175 Debug( LDAP_DEBUG_ANY,
176 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
181 rc = bdb_id2entry_add( be, tid, e );
183 snprintf( text->bv_val, text->bv_len,
184 "id2entry_add failed: %s (%d)",
185 db_strerror(rc), rc );
186 Debug( LDAP_DEBUG_ANY,
187 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
191 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
193 snprintf( text->bv_val, text->bv_len,
194 "index_entry_add failed: %s (%d)",
195 db_strerror(rc), rc );
196 Debug( LDAP_DEBUG_ANY,
197 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
204 rc = txn_commit( tid, 0 );
206 snprintf( text->bv_val, text->bv_len,
207 "txn_commit failed: %s (%d)",
208 db_strerror(rc), rc );
209 Debug( LDAP_DEBUG_ANY,
210 "=> bdb_tool_entry_put: %s\n",
211 text->bv_val, 0, 0 );
217 snprintf( text->bv_val, text->bv_len,
218 "txn_aborted! %s (%d)",
219 db_strerror(rc), rc );
220 Debug( LDAP_DEBUG_ANY,
221 "=> bdb_tool_entry_put: %s\n",
222 text->bv_val, 0, 0 );
230 int bdb_tool_entry_reindex(
234 struct bdb_info *bi = (struct bdb_info *) be->be_private;
240 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
243 e = bdb_tool_entry_get( be, id );
246 Debug( LDAP_DEBUG_ANY,
247 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
253 rc = txn_begin( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
255 Debug( LDAP_DEBUG_ANY,
256 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
257 db_strerror(rc), rc, 0 );
263 * just (re)add them for now
264 * assume that some other routine (not yet implemented)
265 * will zap index databases
269 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
270 (long) id, e->e_dn, 0 );
272 /* add dn2id indices */
273 if ( be_issuffix( be, &e->e_nname ) ) {
276 rc = dnParent( &e->e_nname, &pdn );
277 if ( rc != LDAP_SUCCESS ) {
278 Debug( LDAP_DEBUG_ANY, "=> bdb_tool_entry_reindex: "
279 "dnParent(\"%s\") failed\n",
280 e->e_nname.bv_val, 0, 0 );
284 rc = bdb_dn2id_add( be, tid, &pdn, e );
285 if( rc != 0 && rc != DB_KEYEXIST ) {
286 Debug( LDAP_DEBUG_ANY,
287 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
288 db_strerror(rc), rc, 0 );
292 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
297 rc = txn_commit( tid, 0 );
299 Debug( LDAP_DEBUG_ANY,
300 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
301 db_strerror(rc), rc, 0 );
307 Debug( LDAP_DEBUG_ANY,
308 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
309 db_strerror(rc), rc, 0 );