1 /* tools.c - tools for slap tools */
4 * Copyright 1998-2000 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(( "backend", LDAP_LEVEL_CRIT,
51 "Could not open/create id2entry%s\n", LDBM_SUFFIX ));
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 );
95 ldbm_datum_free( id2entry->dbc_db, key );
100 ID ldbm_tool_entry_next(
106 assert( slapMode & SLAP_TOOL_MODE );
107 assert( id2entry != NULL );
109 /* allow for NEXTID */
110 ldbm_datum_init( key );
112 key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
114 if( key.dptr == NULL ) {
118 AC_MEMCPY( &id, key.dptr, key.dsize );
120 ldbm_datum_free( id2entry->dbc_db, key );
125 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
129 assert( slapMode & SLAP_TOOL_MODE );
130 assert( id2entry != NULL );
132 ldbm_datum_init( key );
134 key.dptr = (char *) &id;
135 key.dsize = sizeof(ID);
137 data = ldbm_cache_fetch( id2entry, key );
139 if ( data.dptr == NULL ) {
143 e = str2entry( data.dptr );
144 ldbm_datum_free( id2entry->dbc_db, data );
153 ID ldbm_tool_entry_put(
157 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
162 assert( slapMode & SLAP_TOOL_MODE );
163 assert( id2entry != NULL );
165 if( next_id_get( be ) == NOID ) {
169 e->e_id = li->li_nextid++;
172 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
173 "ldbm_tool_entry_put: (%s)%ld\n", e->e_dn, e->e_id ));
175 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
176 e->e_id, e->e_dn, 0 );
179 id = dn2id( be, e->e_ndn );
182 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
183 "ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
186 Debug( LDAP_DEBUG_TRACE,
187 "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
194 rc = index_entry_add( be, e, e->e_attrs );
199 rc = dn2id_add( be, e->e_ndn, e->e_id );
204 ldbm_datum_init( key );
205 ldbm_datum_init( data );
207 key.dptr = (char *) &e->e_id;
208 key.dsize = sizeof(ID);
210 data.dptr = entry2str( e, &len );
211 data.dsize = len + 1;
214 rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
217 (void) dn2id_delete( be, e->e_ndn, e->e_id );
224 int ldbm_tool_entry_reindex(
232 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
233 "ldbm_tool_entry_reindex: ID=%ld\n", (long)id ));
235 Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
240 e = ldbm_tool_entry_get( be, id );
244 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
245 "ldbm_tool_entry_reindex: could not locate id %ld\n",
248 Debug( LDAP_DEBUG_ANY,
249 "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
257 * just (re)add them for now
258 * assume that some other routine (not yet implemented)
259 * will zap index databases
264 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
265 "ldbm_tool_entry_reindex: (%s) %ld\n", e->e_dn, id ));
267 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
272 rc = index_entry_add( be, e, e->e_attrs );
279 int ldbm_tool_sync( BackendDB *be )
281 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
283 assert( slapMode & SLAP_TOOL_MODE );
285 if ( li->li_nextid != NOID ) {
286 next_id_write( be, li->li_nextid );