1 /* tools.c - tools for slap tools */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
14 #include "back-ldbm.h"
16 static LDBMCursor *cursorp = NULL;
17 static DBCache *id2entry = NULL;
19 int ldbm_tool_entry_open(
20 BackendDB *be, int mode )
22 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25 assert( slapMode & SLAP_TOOL_MODE );
26 assert( id2entry == NULL );
43 li->li_dbcachewsync = 0;
45 if ( (id2entry = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, flags ))
47 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry" LDBM_SUFFIX "\n",
55 int ldbm_tool_entry_close(
58 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
60 assert( slapMode & SLAP_TOOL_MODE );
61 assert( id2entry != NULL );
63 ldbm_cache_close( be, id2entry );
64 li->li_dbcachewsync = 1;
70 ID ldbm_tool_entry_first(
76 assert( slapMode & SLAP_TOOL_MODE );
77 assert( id2entry != NULL );
79 key = ldbm_firstkey( id2entry->dbc_db, &cursorp );
81 if( key.dptr == NULL ) {
85 memcpy( &id, key.dptr, key.dsize );
87 ldbm_datum_free( id2entry->dbc_db, key );
92 ID ldbm_tool_entry_next(
99 assert( slapMode & SLAP_TOOL_MODE );
100 assert( id2entry != NULL );
102 /* allow for NEXTID */
103 ldbm_datum_init( key );
105 key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
107 if( key.dptr == NULL ) {
111 memcpy( &id, key.dptr, key.dsize );
113 ldbm_datum_free( id2entry->dbc_db, key );
118 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
122 assert( slapMode & SLAP_TOOL_MODE );
123 assert( id2entry != NULL );
125 ldbm_datum_init( key );
127 key.dptr = (char *) &id;
128 key.dsize = sizeof(ID);
130 data = ldbm_cache_fetch( id2entry, key );
132 if ( data.dptr == NULL ) {
136 e = str2entry( data.dptr );
137 ldbm_datum_free( id2entry->dbc_db, data );
142 ID ldbm_tool_entry_put(
146 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
150 assert( slapMode & SLAP_TOOL_MODE );
151 assert( id2entry != NULL );
153 if( next_id_get( be ) == NOID ) {
157 e->e_id = li->li_nextid++;
159 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
160 e->e_id, e->e_dn, 0 );
162 rc = index_add_entry( be, e );
168 rc = dn2id_add( be, e->e_ndn, e->e_id );
174 ldbm_datum_init( key );
175 ldbm_datum_init( data );
177 key.dptr = (char *) &e->e_id;
178 key.dsize = sizeof(ID);
180 data.dptr = entry2str( e, &len );
181 data.dsize = len + 1;
184 rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
187 (void) dn2id_delete( be, e->e_ndn );
194 int ldbm_tool_index_attr(
198 static DBCache *db = NULL;
199 int indexmask, syntaxmask;
202 assert( slapMode & SLAP_TOOL_MODE );
204 attr_masks( be->be_private, type, &indexmask, &syntaxmask );
206 attr_normalize( type );
207 at_cn = at_canonical_name( type );
209 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_NEWDB ))
211 Debug( LDAP_DEBUG_ANY,
212 "<= index_read NULL (could not open %s%s)\n", at_cn,
217 ldbm_cache_close( be, db );
219 return indexmask != 0;
222 int ldbm_tool_index_change(
229 assert( slapMode & SLAP_TOOL_MODE );
231 index_change_values( be,
237 int ldbm_tool_sync( BackendDB *be )
239 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
241 assert( slapMode & SLAP_TOOL_MODE );
243 if ( li->li_nextid != NOID ) {
244 next_id_write( be, li->li_nextid );