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 ))
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;
182 assert( slapMode & SLAP_TOOL_MODE );
183 assert( id2entry != NULL );
186 assert( text->bv_val );
187 assert( text->bv_val[0] == '\0' ); /* overconservative? */
189 if ( next_id_get( be, &id ) || id == NOID ) {
190 strncpy( text->bv_val, "unable to get nextid", text->bv_len );
194 e->e_id = li->li_nextid++;
196 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
197 e->e_id, e->e_dn, 0 );
199 if ( dn2id( be, &e->e_nname, &id ) ) {
200 /* something bad happened to ldbm cache */
201 strncpy( text->bv_val, "ldbm cache corrupted", text->bv_len );
206 Debug( LDAP_DEBUG_TRACE,
207 "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
209 strncpy( text->bv_val, "already exists", text->bv_len );
214 op.o_tmpmemctx = NULL;
215 op.o_tmpmfuncs = &ch_mfuncs;
217 rc = index_entry_add( &op, e );
219 strncpy( text->bv_val, "index add failed", text->bv_len );
223 rc = dn2id_add( be, &e->e_nname, e->e_id );
225 strncpy( text->bv_val, "dn2id add failed", text->bv_len );
229 ldbm_datum_init( key );
230 ldbm_datum_init( data );
232 #ifndef WORDS_BIGENDIAN
233 id = htonl( e->e_id );
234 key.dptr = (char *) &id;
236 key.dptr = (char *) &e->e_id;
238 key.dsize = sizeof(ID);
240 data.dptr = entry2str( e, &len );
241 data.dsize = len + 1;
244 rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
247 (void) dn2id_delete( be, &e->e_nname, e->e_id );
248 strncpy( text->bv_val, "cache store failed", text->bv_len );
255 int ldbm_tool_entry_reindex(
263 Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
267 e = ldbm_tool_entry_get( be, id );
270 Debug( LDAP_DEBUG_ANY,
271 "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
278 * just (re)add them for now
279 * assume that some other routine (not yet implemented)
280 * will zap index databases
284 Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
287 dn2id_add( be, &e->e_nname, e->e_id );
290 op.o_tmpmemctx = NULL;
291 op.o_tmpmfuncs = &ch_mfuncs;
292 rc = index_entry_add( &op, e );
299 int ldbm_tool_sync( BackendDB *be )
301 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
303 assert( slapMode & SLAP_TOOL_MODE );
305 if ( li->li_nextid != NOID ) {
306 if ( next_id_write( be, li->li_nextid ) ) {