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;
177 assert( slapMode & SLAP_TOOL_MODE );
178 assert( id2entry != NULL );
181 assert( text->bv_val );
182 assert( text->bv_val[0] == '\0' ); /* overconservative? */
184 if ( next_id_get( be, &id ) || id == NOID ) {
185 strncpy( text->bv_val, "unable to get nextid", text->bv_len );
189 e->e_id = li->li_nextid++;
192 LDAP_LOG( BACK_LDBM, ENTRY,
193 "ldbm_tool_entry_put: (%s)%ld\n", e->e_dn, e->e_id ,0 );
195 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
196 e->e_id, e->e_dn, 0 );
199 if ( dn2id( be, &e->e_nname, &id ) ) {
200 /* something bad happened to ldbm cache */
201 strncpy( text->bv_val, "ldbm cache corrupted", text->bv_len );
207 LDAP_LOG( BACK_LDBM, ENTRY,
208 "ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
211 Debug( LDAP_DEBUG_TRACE,
212 "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
215 strncpy( text->bv_val, "already exists", text->bv_len );
219 rc = index_entry_add( be, e, e->e_attrs );
221 strncpy( text->bv_val, "index add failed", text->bv_len );
225 rc = dn2id_add( be, &e->e_nname, e->e_id );
227 strncpy( text->bv_val, "dn2id add failed", text->bv_len );
231 ldbm_datum_init( key );
232 ldbm_datum_init( data );
234 #ifndef WORDS_BIGENDIAN
235 id = htonl( e->e_id );
236 key.dptr = (char *) &id;
238 key.dptr = (char *) &e->e_id;
240 key.dsize = sizeof(ID);
242 data.dptr = entry2str( e, &len );
243 data.dsize = len + 1;
246 rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
249 (void) dn2id_delete( be, &e->e_nname, e->e_id );
250 strncpy( text->bv_val, "cache store failed", text->bv_len );
257 int ldbm_tool_entry_reindex(
265 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_tool_entry_reindex: ID=%ld\n",
268 Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
273 e = ldbm_tool_entry_get( be, id );
277 LDAP_LOG( BACK_LDBM, INFO,
278 "ldbm_tool_entry_reindex: could not locate id %ld\n",
281 Debug( LDAP_DEBUG_ANY,
282 "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
290 * just (re)add them for now
291 * assume that some other routine (not yet implemented)
292 * will zap index databases
297 LDAP_LOG( BACK_LDBM, ENTRY,
298 "ldbm_tool_entry_reindex: (%s) %ld\n", e->e_dn, id, 0 );
300 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
304 dn2id_add( be, &e->e_nname, e->e_id );
305 rc = index_entry_add( be, e, e->e_attrs );
312 int ldbm_tool_sync( BackendDB *be )
314 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
316 assert( slapMode & SLAP_TOOL_MODE );
318 if ( li->li_nextid != NOID ) {
319 if ( next_id_write( be, li->li_nextid ) ) {