1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/string.h>
22 #include <ac/socket.h>
25 #include "back-ldbm.h"
27 static LDBMCursor *cursorp = NULL;
28 static DBCache *id2entry = NULL;
30 int ldbm_tool_entry_open(
31 BackendDB *be, int mode )
33 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
36 assert( slapMode & SLAP_TOOL_MODE );
37 assert( id2entry == NULL );
54 li->li_dbwritesync = 0;
56 if ( (id2entry = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, flags ))
58 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry" LDBM_SUFFIX "\n",
67 int ldbm_tool_entry_close(
70 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
72 assert( slapMode & SLAP_TOOL_MODE );
73 assert( id2entry != NULL );
75 ldbm_cache_close( be, id2entry );
76 li->li_dbwritesync = 1;
82 ID ldbm_tool_entry_first(
88 assert( slapMode & SLAP_TOOL_MODE );
89 assert( id2entry != NULL );
91 key = ldbm_firstkey( id2entry->dbc_db, &cursorp );
93 if( key.dptr == NULL ) {
97 AC_MEMCPY( &id, key.dptr, key.dsize );
98 #ifndef WORDS_BIGENDIAN
102 ldbm_datum_free( id2entry->dbc_db, key );
107 ID ldbm_tool_entry_next(
113 assert( slapMode & SLAP_TOOL_MODE );
114 assert( id2entry != NULL );
116 /* allow for NEXTID */
117 ldbm_datum_init( key );
119 key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
121 if( key.dptr == NULL ) {
125 AC_MEMCPY( &id, key.dptr, key.dsize );
126 #ifndef WORDS_BIGENDIAN
130 ldbm_datum_free( id2entry->dbc_db, key );
135 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
139 #ifndef WORDS_BIGENDIAN
142 assert( slapMode & SLAP_TOOL_MODE );
143 assert( id2entry != NULL );
145 ldbm_datum_init( key );
147 #ifndef WORDS_BIGENDIAN
149 key.dptr = (char *) &id2;
151 key.dptr = (char *) &id;
153 key.dsize = sizeof(ID);
155 data = ldbm_cache_fetch( id2entry, key );
157 if ( data.dptr == NULL ) {
161 e = str2entry( data.dptr );
162 ldbm_datum_free( id2entry->dbc_db, data );
171 ID ldbm_tool_entry_put(
174 struct berval *text )
176 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
183 assert( slapMode & SLAP_TOOL_MODE );
184 assert( id2entry != NULL );
187 assert( text->bv_val );
188 assert( text->bv_val[0] == '\0' ); /* overconservative? */
190 if ( next_id_get( be, &id ) || id == NOID ) {
191 strncpy( text->bv_val, "unable to get nextid", text->bv_len );
195 e->e_id = li->li_nextid++;
197 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
198 e->e_id, e->e_dn, 0 );
200 if ( dn2id( be, &e->e_nname, &id ) ) {
201 /* something bad happened to ldbm cache */
202 strncpy( text->bv_val, "ldbm cache corrupted", text->bv_len );
207 Debug( LDAP_DEBUG_TRACE,
208 "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
210 strncpy( text->bv_val, "already exists", text->bv_len );
216 op.o_tmpmemctx = NULL;
217 op.o_tmpmfuncs = &ch_mfuncs;
219 rc = index_entry_add( &op, e );
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(
266 Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
270 e = ldbm_tool_entry_get( be, id );
273 Debug( LDAP_DEBUG_ANY,
274 "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
281 * just (re)add them for now
282 * assume that some other routine (not yet implemented)
283 * will zap index databases
287 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
290 dn2id_add( be, &e->e_nname, e->e_id );
294 op.o_tmpmemctx = NULL;
295 op.o_tmpmfuncs = &ch_mfuncs;
296 rc = index_entry_add( &op, e );
303 int ldbm_tool_sync( BackendDB *be )
305 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
307 assert( slapMode & SLAP_TOOL_MODE );
309 if ( li->li_nextid != NOID ) {
310 if ( next_id_write( be, li->li_nextid ) ) {