]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/tools.c
64d930a610b84f15326ce1110180e478829d866a
[openldap] / servers / slapd / back-ldbm / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17
18 static LDBMCursor *cursorp = NULL;
19 static DBCache *id2entry = NULL;
20
21 int ldbm_tool_entry_open(
22         BackendDB *be, int mode )
23 {
24         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25         int flags;
26
27         assert( slapMode & SLAP_TOOL_MODE );
28         assert( id2entry == NULL );
29
30         switch( mode ) {
31         case 1:
32                 flags = LDBM_WRCREAT;
33                 break;
34         case 2:
35 #ifdef TRUNCATE_MODE
36                 flags = LDBM_NEWDB;
37 #else
38                 flags = LDBM_WRCREAT;
39 #endif
40                 break;
41         default:
42                 flags = LDBM_READER;
43         }
44
45         li->li_dbwritesync = 0;
46
47         if ( (id2entry = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, flags ))
48             == NULL ) {
49 #ifdef NEW_LOGGING
50                 LDAP_LOG( BACK_LDBM, CRIT,
51                            "Could not open/create id2entry%s\n", LDBM_SUFFIX, 0, 0 );
52 #else
53                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry" LDBM_SUFFIX "\n",
54                     0, 0, 0 );
55 #endif
56
57                 return( -1 );
58         }
59
60         return 0;
61 }
62
63 int ldbm_tool_entry_close(
64         BackendDB *be )
65 {
66         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
67
68         assert( slapMode & SLAP_TOOL_MODE );
69         assert( id2entry != NULL );
70
71         ldbm_cache_close( be, id2entry );
72         li->li_dbwritesync = 1;
73         id2entry = NULL;
74
75         return 0;
76 }
77
78 ID ldbm_tool_entry_first(
79         BackendDB *be )
80 {
81         Datum key;
82         ID id;
83
84         assert( slapMode & SLAP_TOOL_MODE );
85         assert( id2entry != NULL );
86
87         key = ldbm_firstkey( id2entry->dbc_db, &cursorp );
88
89         if( key.dptr == NULL ) {
90                 return NOID;
91         }
92
93         AC_MEMCPY( &id, key.dptr, key.dsize );
94 #ifndef WORDS_BIGENDIAN
95         id = ntohl( id );
96 #endif
97
98         ldbm_datum_free( id2entry->dbc_db, key );
99
100         return id;
101 }
102
103 ID ldbm_tool_entry_next(
104         BackendDB *be )
105 {
106         Datum key;
107         ID id;
108
109         assert( slapMode & SLAP_TOOL_MODE );
110         assert( id2entry != NULL );
111
112         /* allow for NEXTID */
113         ldbm_datum_init( key );
114
115         key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
116
117         if( key.dptr == NULL ) {
118                 return NOID;
119         }
120
121         AC_MEMCPY( &id, key.dptr, key.dsize );
122 #ifndef WORDS_BIGENDIAN
123         id = ntohl( id );
124 #endif
125
126         ldbm_datum_free( id2entry->dbc_db, key );
127
128         return id;
129 }
130
131 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
132 {
133         Entry *e;
134         Datum key, data;
135 #ifndef WORDS_BIGENDIAN
136         ID id2;
137 #endif
138         assert( slapMode & SLAP_TOOL_MODE );
139         assert( id2entry != NULL );
140
141         ldbm_datum_init( key );
142
143 #ifndef WORDS_BIGENDIAN
144         id2 = htonl( id );
145         key.dptr = (char *) &id2;
146 #else
147         key.dptr = (char *) &id;
148 #endif
149         key.dsize = sizeof(ID);
150
151         data = ldbm_cache_fetch( id2entry, key );
152
153         if ( data.dptr == NULL ) {
154                 return NULL;
155         }
156
157         e = str2entry( data.dptr );
158         ldbm_datum_free( id2entry->dbc_db, data );
159
160         if( e != NULL ) {
161                 e->e_id = id;
162         }
163
164         return e;
165 }
166
167 ID ldbm_tool_entry_put(
168         BackendDB *be,
169         Entry *e,
170         struct berval *text )
171 {
172         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
173         Datum key, data;
174         int rc, len;
175         ID id;
176         Operation op = {0};
177
178         assert( slapMode & SLAP_TOOL_MODE );
179         assert( id2entry != NULL );
180
181         assert( text );
182         assert( text->bv_val );
183         assert( text->bv_val[0] == '\0' );      /* overconservative? */
184
185         if ( next_id_get( be, &id ) || id == NOID ) {
186                 strncpy( text->bv_val, "unable to get nextid", text->bv_len );
187                 return NOID;
188         }
189
190         e->e_id = li->li_nextid++;
191
192 #ifdef NEW_LOGGING
193         LDAP_LOG( BACK_LDBM, ENTRY,
194                 "ldbm_tool_entry_put: (%s)%ld\n", e->e_dn, e->e_id ,0 );
195 #else
196         Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
197                 e->e_id, e->e_dn, 0 );
198 #endif
199
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 );
203                 return NOID;
204         }
205
206         if( id != NOID ) {
207 #ifdef NEW_LOGGING
208                 LDAP_LOG( BACK_LDBM, ENTRY,
209                         "ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
210                         e->e_dn, id, 0 );
211 #else
212                 Debug( LDAP_DEBUG_TRACE,
213                         "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
214                         e->e_ndn, id, 0 );
215 #endif
216                 strncpy( text->bv_val, "already exists", text->bv_len );
217                 return NOID;
218         }
219
220         op.o_bd = be;
221         op.o_tmpmemctx = NULL;
222         op.o_tmpmfuncs = &ch_mfuncs;
223
224         rc = index_entry_add( &op, e );
225         if( rc != 0 ) {
226                 strncpy( text->bv_val, "index add failed", text->bv_len );
227                 return NOID;
228         }
229
230         rc = dn2id_add( be, &e->e_nname, e->e_id );
231         if( rc != 0 ) {
232                 strncpy( text->bv_val, "dn2id add failed", text->bv_len );
233                 return NOID;
234         }
235
236         ldbm_datum_init( key );
237         ldbm_datum_init( data );
238
239 #ifndef WORDS_BIGENDIAN
240         id = htonl( e->e_id );
241         key.dptr = (char *) &id;
242 #else
243         key.dptr = (char *) &e->e_id;
244 #endif
245         key.dsize = sizeof(ID);
246
247         data.dptr = entry2str( e, &len );
248         data.dsize = len + 1;
249
250         /* store it */
251         rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
252
253         if( rc != 0 ) {
254                 (void) dn2id_delete( be, &e->e_nname, e->e_id );
255                 strncpy( text->bv_val, "cache store failed", text->bv_len );
256                 return NOID;
257         }
258
259         return e->e_id;
260 }
261
262 int ldbm_tool_entry_reindex(
263         BackendDB *be,
264         ID id )
265 {
266         int rc;
267         Entry *e;
268         Operation op = {0};
269
270 #ifdef NEW_LOGGING
271         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_tool_entry_reindex: ID=%ld\n", 
272                 (long)id, 0, 0 );
273 #else
274         Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
275                 (long) id, 0, 0 );
276 #endif
277
278
279         e = ldbm_tool_entry_get( be, id );
280
281         if( e == NULL ) {
282 #ifdef NEW_LOGGING
283                 LDAP_LOG( BACK_LDBM, INFO,
284                    "ldbm_tool_entry_reindex: could not locate id %ld\n", 
285                    (long)id, 0, 0  );
286 #else
287                 Debug( LDAP_DEBUG_ANY,
288                         "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
289                         (long) id, 0, 0 );
290 #endif
291
292                 return -1;
293         }
294
295         /*
296          * just (re)add them for now
297          * assume that some other routine (not yet implemented)
298          * will zap index databases
299          *
300          */
301
302 #ifdef NEW_LOGGING
303         LDAP_LOG( BACK_LDBM, ENTRY,
304                    "ldbm_tool_entry_reindex: (%s) %ld\n", e->e_dn, id, 0 );
305 #else
306         Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
307                 id, e->e_dn, 0 );
308 #endif
309
310         dn2id_add( be, &e->e_nname, e->e_id );
311
312         op.o_bd = be;
313         op.o_tmpmemctx = NULL;
314         op.o_tmpmfuncs = &ch_mfuncs;
315         rc = index_entry_add( &op, e );
316
317         entry_free( e );
318
319         return rc;
320 }
321
322 int ldbm_tool_sync( BackendDB *be )
323 {
324         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
325
326         assert( slapMode & SLAP_TOOL_MODE );
327
328         if ( li->li_nextid != NOID ) {
329                 if ( next_id_write( be, li->li_nextid ) ) {
330                         return( -1 );
331                 }
332         }
333
334         return 0;
335 }