1 /* tools.c - tools for slap tools */
4 * Copyright 1998-1999 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 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 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 );
143 ID ldbm_tool_entry_put(
147 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
151 assert( slapMode & SLAP_TOOL_MODE );
152 assert( id2entry != NULL );
154 if( next_id_get( be ) == NOID ) {
158 e->e_id = li->li_nextid++;
160 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
161 e->e_id, e->e_dn, 0 );
163 rc = index_add_entry( be, e );
169 rc = dn2id_add( be, e->e_ndn, e->e_id );
175 ldbm_datum_init( key );
176 ldbm_datum_init( data );
178 key.dptr = (char *) &e->e_id;
179 key.dsize = sizeof(ID);
181 data.dptr = entry2str( e, &len );
182 data.dsize = len + 1;
185 rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
188 (void) dn2id_delete( be, e->e_ndn, e->e_id );
195 int ldbm_tool_index_attr(
199 static DBCache *db = NULL;
200 int indexmask, syntaxmask;
203 assert( slapMode & SLAP_TOOL_MODE );
205 attr_masks( be->be_private, type, &indexmask, &syntaxmask );
207 #ifdef SLAPD_SCHEMA_COMPAT
208 attr_normalize( type );
209 at_cn = at_canonical_name( type );
212 Debug( LDAP_DEBUG_ANY, "<= index_attr NULL (attribute type %s has no canonical name)\n",
218 AttributeType *at = at_find( type );
221 Debug( LDAP_DEBUG_ANY,
222 "<= index_attr NULL (could not find attribute type %s)\n",
227 at_cn = at_canonical_name( at );
231 Debug( LDAP_DEBUG_ANY, "<= index_attr NULL (attribute type %s (%s) has no canonical name)\n",
232 at->sat_oid, type, 0 );
237 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_NEWDB ))
240 Debug( LDAP_DEBUG_ANY,
241 "<= index_attr NULL (could not open %s%s)\n", at_cn,
246 ldbm_cache_close( be, db );
248 return indexmask != 0;
251 int ldbm_tool_index_change(
258 assert( slapMode & SLAP_TOOL_MODE );
260 index_change_values( be,
266 int ldbm_tool_sync( BackendDB *be )
268 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
270 assert( slapMode & SLAP_TOOL_MODE );
272 if ( li->li_nextid != NOID ) {
273 next_id_write( be, li->li_nextid );