1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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 ))
59 LDAP_LOG( BACK_LDBM, CRIT,
60 "Could not open/create id2entry%s\n", LDBM_SUFFIX, 0, 0 );
62 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry" LDBM_SUFFIX "\n",
72 int ldbm_tool_entry_close(
75 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
77 assert( slapMode & SLAP_TOOL_MODE );
78 assert( id2entry != NULL );
80 ldbm_cache_close( be, id2entry );
81 li->li_dbwritesync = 1;
87 ID ldbm_tool_entry_first(
93 assert( slapMode & SLAP_TOOL_MODE );
94 assert( id2entry != NULL );
96 key = ldbm_firstkey( id2entry->dbc_db, &cursorp );
98 if( key.dptr == NULL ) {
102 AC_MEMCPY( &id, key.dptr, key.dsize );
103 #ifndef WORDS_BIGENDIAN
107 ldbm_datum_free( id2entry->dbc_db, key );
112 ID ldbm_tool_entry_next(
118 assert( slapMode & SLAP_TOOL_MODE );
119 assert( id2entry != NULL );
121 /* allow for NEXTID */
122 ldbm_datum_init( key );
124 key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
126 if( key.dptr == NULL ) {
130 AC_MEMCPY( &id, key.dptr, key.dsize );
131 #ifndef WORDS_BIGENDIAN
135 ldbm_datum_free( id2entry->dbc_db, key );
140 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
144 #ifndef WORDS_BIGENDIAN
147 assert( slapMode & SLAP_TOOL_MODE );
148 assert( id2entry != NULL );
150 ldbm_datum_init( key );
152 #ifndef WORDS_BIGENDIAN
154 key.dptr = (char *) &id2;
156 key.dptr = (char *) &id;
158 key.dsize = sizeof(ID);
160 data = ldbm_cache_fetch( id2entry, key );
162 if ( data.dptr == NULL ) {
166 e = str2entry( data.dptr );
167 ldbm_datum_free( id2entry->dbc_db, data );
176 ID ldbm_tool_entry_put(
179 struct berval *text )
181 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
187 assert( slapMode & SLAP_TOOL_MODE );
188 assert( id2entry != NULL );
191 assert( text->bv_val );
192 assert( text->bv_val[0] == '\0' ); /* overconservative? */
194 if ( next_id_get( be, &id ) || id == NOID ) {
195 strncpy( text->bv_val, "unable to get nextid", text->bv_len );
199 e->e_id = li->li_nextid++;
202 LDAP_LOG( BACK_LDBM, ENTRY,
203 "ldbm_tool_entry_put: (%s)%ld\n", e->e_dn, e->e_id ,0 );
205 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
206 e->e_id, e->e_dn, 0 );
209 if ( dn2id( be, &e->e_nname, &id ) ) {
210 /* something bad happened to ldbm cache */
211 strncpy( text->bv_val, "ldbm cache corrupted", text->bv_len );
217 LDAP_LOG( BACK_LDBM, ENTRY,
218 "ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
221 Debug( LDAP_DEBUG_TRACE,
222 "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
225 strncpy( text->bv_val, "already exists", text->bv_len );
230 op.o_tmpmemctx = NULL;
231 op.o_tmpmfuncs = &ch_mfuncs;
233 rc = index_entry_add( &op, e );
235 strncpy( text->bv_val, "index add failed", text->bv_len );
239 rc = dn2id_add( be, &e->e_nname, e->e_id );
241 strncpy( text->bv_val, "dn2id add failed", text->bv_len );
245 ldbm_datum_init( key );
246 ldbm_datum_init( data );
248 #ifndef WORDS_BIGENDIAN
249 id = htonl( e->e_id );
250 key.dptr = (char *) &id;
252 key.dptr = (char *) &e->e_id;
254 key.dsize = sizeof(ID);
256 data.dptr = entry2str( e, &len );
257 data.dsize = len + 1;
260 rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
263 (void) dn2id_delete( be, &e->e_nname, e->e_id );
264 strncpy( text->bv_val, "cache store failed", text->bv_len );
271 int ldbm_tool_entry_reindex(
280 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_tool_entry_reindex: ID=%ld\n",
283 Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
288 e = ldbm_tool_entry_get( be, id );
292 LDAP_LOG( BACK_LDBM, INFO,
293 "ldbm_tool_entry_reindex: could not locate id %ld\n",
296 Debug( LDAP_DEBUG_ANY,
297 "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
305 * just (re)add them for now
306 * assume that some other routine (not yet implemented)
307 * will zap index databases
312 LDAP_LOG( BACK_LDBM, ENTRY,
313 "ldbm_tool_entry_reindex: (%s) %ld\n", e->e_dn, id, 0 );
315 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
319 dn2id_add( be, &e->e_nname, e->e_id );
322 op.o_tmpmemctx = NULL;
323 op.o_tmpmfuncs = &ch_mfuncs;
324 rc = index_entry_add( &op, e );
331 int ldbm_tool_sync( BackendDB *be )
333 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
335 assert( slapMode & SLAP_TOOL_MODE );
337 if ( li->li_nextid != NOID ) {
338 if ( next_id_write( be, li->li_nextid ) ) {