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 ))
49 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry" LDBM_SUFFIX "\n",
57 int ldbm_tool_entry_close(
60 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
62 assert( slapMode & SLAP_TOOL_MODE );
63 assert( id2entry != NULL );
65 ldbm_cache_close( be, id2entry );
66 li->li_dbwritesync = 1;
72 ID ldbm_tool_entry_first(
78 assert( slapMode & SLAP_TOOL_MODE );
79 assert( id2entry != NULL );
81 key = ldbm_firstkey( id2entry->dbc_db, &cursorp );
83 if( key.dptr == NULL ) {
87 AC_MEMCPY( &id, key.dptr, key.dsize );
89 ldbm_datum_free( id2entry->dbc_db, key );
94 ID ldbm_tool_entry_next(
100 assert( slapMode & SLAP_TOOL_MODE );
101 assert( id2entry != NULL );
103 /* allow for NEXTID */
104 ldbm_datum_init( key );
106 key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
108 if( key.dptr == NULL ) {
112 AC_MEMCPY( &id, key.dptr, key.dsize );
114 ldbm_datum_free( id2entry->dbc_db, key );
119 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
123 assert( slapMode & SLAP_TOOL_MODE );
124 assert( id2entry != NULL );
126 ldbm_datum_init( key );
128 key.dptr = (char *) &id;
129 key.dsize = sizeof(ID);
131 data = ldbm_cache_fetch( id2entry, key );
133 if ( data.dptr == NULL ) {
137 e = str2entry( data.dptr );
138 ldbm_datum_free( id2entry->dbc_db, data );
147 ID ldbm_tool_entry_put(
151 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
156 assert( slapMode & SLAP_TOOL_MODE );
157 assert( id2entry != NULL );
159 if( next_id_get( be ) == NOID ) {
163 e->e_id = li->li_nextid++;
165 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
166 e->e_id, e->e_dn, 0 );
168 id = dn2id( be, e->e_ndn );
170 Debug( LDAP_DEBUG_TRACE,
171 "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
176 rc = index_entry_add( be, e, e->e_attrs );
181 rc = dn2id_add( be, e->e_ndn, e->e_id );
186 ldbm_datum_init( key );
187 ldbm_datum_init( data );
189 key.dptr = (char *) &e->e_id;
190 key.dsize = sizeof(ID);
192 data.dptr = entry2str( e, &len );
193 data.dsize = len + 1;
196 rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
199 (void) dn2id_delete( be, e->e_ndn, e->e_id );
206 int ldbm_tool_entry_reindex(
213 Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
216 e = ldbm_tool_entry_get( be, id );
219 Debug( LDAP_DEBUG_ANY,
220 "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
226 * just (re)add them for now
227 * assume that some other routine (not yet implemented)
228 * will zap index databases
232 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
235 rc = index_entry_add( be, e, e->e_attrs );
242 int ldbm_tool_sync( BackendDB *be )
244 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
246 assert( slapMode & SLAP_TOOL_MODE );
248 if ( li->li_nextid != NOID ) {
249 next_id_write( be, li->li_nextid );