]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/tools.c
Add error handling to BDB_INDEX code
[openldap] / servers / slapd / back-ldbm / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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(( "backend", LDAP_LEVEL_CRIT,
51                            "Could not open/create id2entry%s\n", LDBM_SUFFIX ));
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
95         ldbm_datum_free( id2entry->dbc_db, key );
96
97         return id;
98 }
99
100 ID ldbm_tool_entry_next(
101         BackendDB *be )
102 {
103         Datum key;
104         ID id;
105
106         assert( slapMode & SLAP_TOOL_MODE );
107         assert( id2entry != NULL );
108
109         /* allow for NEXTID */
110         ldbm_datum_init( key );
111
112         key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
113
114         if( key.dptr == NULL ) {
115                 return NOID;
116         }
117
118         AC_MEMCPY( &id, key.dptr, key.dsize );
119
120         ldbm_datum_free( id2entry->dbc_db, key );
121
122         return id;
123 }
124
125 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
126 {
127         Entry *e;
128         Datum key, data;
129         assert( slapMode & SLAP_TOOL_MODE );
130         assert( id2entry != NULL );
131
132         ldbm_datum_init( key );
133
134         key.dptr = (char *) &id;
135         key.dsize = sizeof(ID);
136
137         data = ldbm_cache_fetch( id2entry, key );
138
139         if ( data.dptr == NULL ) {
140                 return NULL;
141         }
142
143         e = str2entry( data.dptr );
144         ldbm_datum_free( id2entry->dbc_db, data );
145
146         if( e != NULL ) {
147                 e->e_id = id;
148         }
149
150         return e;
151 }
152
153 ID ldbm_tool_entry_put(
154         BackendDB *be,
155         Entry *e )
156 {
157         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
158         Datum key, data;
159         int rc, len;
160         ID id;
161
162         assert( slapMode & SLAP_TOOL_MODE );
163         assert( id2entry != NULL );
164
165         if ( next_id_get( be, &id ) || id == NOID ) {
166                 return NOID;
167         }
168
169         e->e_id = li->li_nextid++;
170
171 #ifdef NEW_LOGGING
172         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
173                 "ldbm_tool_entry_put: (%s)%ld\n", e->e_dn, e->e_id ));
174 #else
175         Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
176                 e->e_id, e->e_dn, 0 );
177 #endif
178
179         if ( dn2id( be, e->e_ndn, &id ) ) {
180                 /* something bad happened to ldbm cache */
181                 return NOID;
182         }
183
184         if( id != NOID ) {
185 #ifdef NEW_LOGGING
186                 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
187                         "ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
188                         e->e_dn, id ));
189 #else
190                 Debug( LDAP_DEBUG_TRACE,
191                         "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
192                         e->e_ndn, id, 0 );
193 #endif
194                 return NOID;
195         }
196
197         rc = index_entry_add( be, e, e->e_attrs );
198         if( rc != 0 ) {
199                 return NOID;
200         }
201
202         rc = dn2id_add( be, e->e_ndn, e->e_id );
203         if( rc != 0 ) {
204                 return NOID;
205         }
206
207         ldbm_datum_init( key );
208         ldbm_datum_init( data );
209
210         key.dptr = (char *) &e->e_id;
211         key.dsize = sizeof(ID);
212
213         data.dptr = entry2str( e, &len );
214         data.dsize = len + 1;
215
216         /* store it */
217         rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
218
219         if( rc != 0 ) {
220                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
221                 return NOID;
222         }
223
224         return e->e_id;
225 }
226
227 int ldbm_tool_entry_reindex(
228         BackendDB *be,
229         ID id )
230 {
231         int rc;
232         Entry *e;
233
234 #ifdef NEW_LOGGING
235         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
236                    "ldbm_tool_entry_reindex: ID=%ld\n", (long)id ));
237 #else
238         Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
239                 (long) id, 0, 0 );
240 #endif
241
242
243         e = ldbm_tool_entry_get( be, id );
244
245         if( e == NULL ) {
246 #ifdef NEW_LOGGING
247                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
248                            "ldbm_tool_entry_reindex: could not locate id %ld\n", 
249                            (long)id ));
250 #else
251                 Debug( LDAP_DEBUG_ANY,
252                         "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
253                         (long) id, 0, 0 );
254 #endif
255
256                 return -1;
257         }
258
259         /*
260          * just (re)add them for now
261          * assume that some other routine (not yet implemented)
262          * will zap index databases
263          *
264          */
265
266 #ifdef NEW_LOGGING
267         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
268                    "ldbm_tool_entry_reindex: (%s) %ld\n", e->e_dn, id ));
269 #else
270         Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
271                 id, e->e_dn, 0 );
272 #endif
273
274
275         rc = index_entry_add( be, e, e->e_attrs );
276
277         entry_free( e );
278
279         return rc;
280 }
281
282 int ldbm_tool_sync( BackendDB *be )
283 {
284         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
285
286         assert( slapMode & SLAP_TOOL_MODE );
287
288         if ( li->li_nextid != NOID ) {
289                 if ( next_id_write( be, li->li_nextid ) ) {
290                         return( -1 );
291                 }
292         }
293
294         return 0;
295 }