1 /* tools.c - tools for slap tools */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
18 static LDBMCursor *cursorp = NULL;
19 static DBCache *id2entry = NULL;
21 int ldbm_tool_entry_open(
22 BackendDB *be, int mode )
24 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
27 assert( slapMode & SLAP_TOOL_MODE );
28 assert( id2entry == NULL );
45 li->li_dbwritesync = 0;
47 if ( (id2entry = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, flags ))
50 LDAP_LOG( BACK_LDBM, CRIT,
51 "Could not open/create id2entry%s\n", LDBM_SUFFIX, 0, 0 );
53 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry" LDBM_SUFFIX "\n",
63 int ldbm_tool_entry_close(
66 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
68 assert( slapMode & SLAP_TOOL_MODE );
69 assert( id2entry != NULL );
71 ldbm_cache_close( be, id2entry );
72 li->li_dbwritesync = 1;
78 ID ldbm_tool_entry_first(
84 assert( slapMode & SLAP_TOOL_MODE );
85 assert( id2entry != NULL );
87 key = ldbm_firstkey( id2entry->dbc_db, &cursorp );
89 if( key.dptr == NULL ) {
93 AC_MEMCPY( &id, key.dptr, key.dsize );
94 #ifndef WORDS_BIGENDIAN
98 ldbm_datum_free( id2entry->dbc_db, key );
103 ID ldbm_tool_entry_next(
109 assert( slapMode & SLAP_TOOL_MODE );
110 assert( id2entry != NULL );
112 /* allow for NEXTID */
113 ldbm_datum_init( key );
115 key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
117 if( key.dptr == NULL ) {
121 AC_MEMCPY( &id, key.dptr, key.dsize );
122 #ifndef WORDS_BIGENDIAN
126 ldbm_datum_free( id2entry->dbc_db, key );
131 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
135 #ifndef WORDS_BIGENDIAN
138 assert( slapMode & SLAP_TOOL_MODE );
139 assert( id2entry != NULL );
141 ldbm_datum_init( key );
143 #ifndef WORDS_BIGENDIAN
145 key.dptr = (char *) &id2;
147 key.dptr = (char *) &id;
149 key.dsize = sizeof(ID);
151 data = ldbm_cache_fetch( id2entry, key );
153 if ( data.dptr == NULL ) {
157 e = str2entry( data.dptr );
158 ldbm_datum_free( id2entry->dbc_db, data );
167 ID ldbm_tool_entry_put(
170 struct berval *text )
172 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
178 assert( slapMode & SLAP_TOOL_MODE );
179 assert( id2entry != NULL );
182 assert( text->bv_val );
183 assert( text->bv_val[0] == '\0' ); /* overconservative? */
185 if ( next_id_get( be, &id ) || id == NOID ) {
186 strncpy( text->bv_val, "unable to get nextid", text->bv_len );
190 e->e_id = li->li_nextid++;
193 LDAP_LOG( BACK_LDBM, ENTRY,
194 "ldbm_tool_entry_put: (%s)%ld\n", e->e_dn, e->e_id ,0 );
196 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
197 e->e_id, e->e_dn, 0 );
200 if ( dn2id( be, &e->e_nname, &id ) ) {
201 /* something bad happened to ldbm cache */
202 strncpy( text->bv_val, "ldbm cache corrupted", text->bv_len );
208 LDAP_LOG( BACK_LDBM, ENTRY,
209 "ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
212 Debug( LDAP_DEBUG_TRACE,
213 "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
216 strncpy( text->bv_val, "already exists", text->bv_len );
221 op.o_tmpmemctx = NULL;
222 op.o_tmpmfuncs = &ch_mfuncs;
224 rc = index_entry_add( &op, e );
226 strncpy( text->bv_val, "index add failed", text->bv_len );
230 rc = dn2id_add( be, &e->e_nname, e->e_id );
232 strncpy( text->bv_val, "dn2id add failed", text->bv_len );
236 ldbm_datum_init( key );
237 ldbm_datum_init( data );
239 #ifndef WORDS_BIGENDIAN
240 id = htonl( e->e_id );
241 key.dptr = (char *) &id;
243 key.dptr = (char *) &e->e_id;
245 key.dsize = sizeof(ID);
247 data.dptr = entry2str( e, &len );
248 data.dsize = len + 1;
251 rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
254 (void) dn2id_delete( be, &e->e_nname, e->e_id );
255 strncpy( text->bv_val, "cache store failed", text->bv_len );
262 int ldbm_tool_entry_reindex(
271 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_tool_entry_reindex: ID=%ld\n",
274 Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
279 e = ldbm_tool_entry_get( be, id );
283 LDAP_LOG( BACK_LDBM, INFO,
284 "ldbm_tool_entry_reindex: could not locate id %ld\n",
287 Debug( LDAP_DEBUG_ANY,
288 "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
296 * just (re)add them for now
297 * assume that some other routine (not yet implemented)
298 * will zap index databases
303 LDAP_LOG( BACK_LDBM, ENTRY,
304 "ldbm_tool_entry_reindex: (%s) %ld\n", e->e_dn, id, 0 );
306 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
310 dn2id_add( be, &e->e_nname, e->e_id );
313 op.o_tmpmemctx = NULL;
314 op.o_tmpmfuncs = &ch_mfuncs;
315 rc = index_entry_add( &op, e );
322 int ldbm_tool_sync( BackendDB *be )
324 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
326 assert( slapMode & SLAP_TOOL_MODE );
328 if ( li->li_nextid != NOID ) {
329 if ( next_id_write( be, li->li_nextid ) ) {