]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Latest changes from HEAD.
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 static DBC *cursor = NULL;
16 static DBT key, data;
17
18 int bdb_tool_entry_open(
19         BackendDB *be, int mode )
20 {
21         /* initialize key and data thangs */
22         DBTzero( &key );
23         DBTzero( &data );
24         key.flags = DB_DBT_REALLOC;
25         data.flags = DB_DBT_REALLOC;
26
27         return 0;
28 }
29
30 int bdb_tool_entry_close(
31         BackendDB *be )
32 {
33         assert( be != NULL );
34
35         if( key.data ) {
36                 ch_free( key.data );
37                 key.data = NULL;
38         }
39         if( data.data ) {
40                 ch_free( data.data );
41                 data.data = NULL;
42         }
43
44         if( cursor ) {
45                 cursor->c_close( cursor );
46                 cursor = NULL;
47         }
48
49         return 0;
50 }
51
52 ID bdb_tool_entry_next(
53         BackendDB *be )
54 {
55         int rc;
56         ID id;
57         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
58
59         assert( be != NULL );
60         assert( slapMode & SLAP_TOOL_MODE );
61         assert( bdb != NULL );
62         
63         if (cursor == NULL) {
64                 rc = bdb->bi_id2entry->bdi_db->cursor(
65                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
66                         bdb->bi_db_opflags );
67                 if( rc != 0 ) {
68                         return NOID;
69                 }
70         }
71
72         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
73
74         if( rc != 0 ) {
75                 return NOID;
76         }
77
78         if( data.data == NULL ) {
79                 return NOID;
80         }
81
82         AC_MEMCPY( &id, key.data, key.size );
83         return id;
84 }
85
86 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
87 {
88         int rc;
89         Entry *e;
90         struct berval bv;
91
92         assert( be != NULL );
93         assert( slapMode & SLAP_TOOL_MODE );
94         assert( data.data != NULL );
95
96         DBT2bv( &data, &bv );
97
98         rc = entry_decode( &bv, &e );
99
100         if( rc == LDAP_SUCCESS ) {
101                 e->e_id = id;
102         }
103
104 #ifdef BDB_HIER
105         bdb_fix_dn(be, id, e);
106 #endif
107
108         return e;
109 }
110
111 ID bdb_tool_entry_put(
112         BackendDB *be,
113         Entry *e,
114         struct berval *text )
115 {
116         int rc;
117         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
118         DB_TXN *tid = NULL;
119         struct berval pdn;
120
121         assert( be != NULL );
122         assert( slapMode & SLAP_TOOL_MODE );
123
124         assert( text );
125         assert( text->bv_val );
126         assert( text->bv_val[0] == '\0' );
127
128 #ifdef NEW_LOGGING
129         LDAP_LOG (( "tools", LDAP_LEVEL_ARGS,
130                 "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
131                 (long) e->e_id, e->e_dn ));
132 #else
133         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
134                 (long) e->e_id, e->e_dn, 0 );
135 #endif
136
137         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
138                 bdb->bi_db_opflags );
139         if( rc != 0 ) {
140                 snprintf( text->bv_val, text->bv_len,
141                         "txn_begin failed: %s (%d)",
142                         db_strerror(rc), rc );
143 #ifdef NEW_LOGGING
144         LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
145                 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
146 #else
147                 Debug( LDAP_DEBUG_ANY,
148                         "=> bdb_tool_entry_put: %s\n",
149                          text->bv_val, 0, 0 );
150 #endif
151                 return NOID;
152         }
153
154         rc = bdb_next_id( be, tid, &e->e_id );
155         if( rc != 0 ) {
156                 snprintf( text->bv_val, text->bv_len,
157                                 "next_id failed: %s (%d)",
158                                 db_strerror(rc), rc );
159 #ifdef NEW_LOGGING
160                 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
161                         "=> bdb_tool_entry_put: %s\n", text->bv_val ));
162 #else
163                 Debug( LDAP_DEBUG_ANY,
164                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
165 #endif
166                 goto done;
167         }
168
169         /* add dn2id indices */
170         if ( be_issuffix( be, &e->e_nname ) ) {
171                 pdn = slap_empty_bv;
172         } else {
173                 dnParent( &e->e_nname, &pdn );
174         }
175         rc = bdb_dn2id_add( be, tid, &pdn, e );
176         if( rc != 0 ) {
177                 snprintf( text->bv_val, text->bv_len, 
178                                 "dn2id_add failed: %s (%d)",
179                                 db_strerror(rc), rc );
180 #ifdef NEW_LOGGING
181                 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
182                         "=> bdb_tool_entry_put: %s\n", text->bv_val ));
183 #else
184                 Debug( LDAP_DEBUG_ANY,
185                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
186 #endif
187                 goto done;
188         }
189
190         /* id2entry index */
191         rc = bdb_id2entry_add( be, tid, e );
192         if( rc != 0 ) {
193                 snprintf( text->bv_val, text->bv_len,
194                                 "id2entry_add failed: %s (%d)",
195                                 db_strerror(rc), rc );
196 #ifdef NEW_LOGGING
197                 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
198                         "=> bdb_tool_entry_put: %s\n", text->bv_val ));
199 #else
200                 Debug( LDAP_DEBUG_ANY,
201                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
202 #endif
203                 goto done;
204         }
205
206         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
207         if( rc != 0 ) {
208                 snprintf( text->bv_val, text->bv_len,
209                                 "index_entry_add failed: %s (%d)",
210                                 db_strerror(rc), rc );
211 #ifdef NEW_LOGGING
212                 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
213                         "=> bdb_tool_entry_put: %s\n", text->bv_val ));
214 #else
215                 Debug( LDAP_DEBUG_ANY,
216                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
217 #endif
218                 goto done;
219         }
220
221 done:
222         if( rc == 0 ) {
223                 rc = TXN_COMMIT( tid, 0 );
224                 if( rc != 0 ) {
225                         snprintf( text->bv_val, text->bv_len,
226                                         "txn_commit failed: %s (%d)",
227                                         db_strerror(rc), rc );
228 #ifdef NEW_LOGGING
229                         LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
230                                 "=> bdb_tool_entry_put: %s\n", text->bv_val ));
231 #else
232                         Debug( LDAP_DEBUG_ANY,
233                                 "=> bdb_tool_entry_put: %s\n",
234                                 text->bv_val, 0, 0 );
235 #endif
236                         e->e_id = NOID;
237                 }
238
239         } else {
240                 TXN_ABORT( tid );
241                 snprintf( text->bv_val, text->bv_len,
242                         "txn_aborted! %s (%d)",
243                         db_strerror(rc), rc );
244 #ifdef NEW_LOGGING
245                 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
246                         "=> bdb_tool_entry_put: %s\n", text->bv_val ));
247 #else
248                 Debug( LDAP_DEBUG_ANY,
249                         "=> bdb_tool_entry_put: %s\n",
250                         text->bv_val, 0, 0 );
251 #endif
252                 e->e_id = NOID;
253         }
254
255         return e->e_id;
256 }
257
258 int bdb_tool_entry_reindex(
259         BackendDB *be,
260         ID id )
261 {
262         struct bdb_info *bi = (struct bdb_info *) be->be_private;
263         int rc;
264         Entry *e;
265         DB_TXN *tid = NULL;
266         struct berval pdn;
267
268 #ifdef NEW_LOGGING
269         LDAP_LOG (( "tools", LDAP_LEVEL_ARGS,
270                 "=> bdb_tool_entry_reindex( %ld )\n", (long) id ));
271 #else
272         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
273                 (long) id, 0, 0 );
274 #endif
275
276         e = bdb_tool_entry_get( be, id );
277
278         if( e == NULL ) {
279 #ifdef NEW_LOGGING
280                 LDAP_LOG (( "tools", LDAP_LEVEL_DETAIL1,
281                         "bdb_tool_entry_reindex:: could not locate id=%ld\n", 
282                         (long) id ));
283 #else
284                 Debug( LDAP_DEBUG_ANY,
285                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
286                         (long) id, 0, 0 );
287 #endif
288                 return -1;
289         }
290
291         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
292         if( rc != 0 ) {
293 #ifdef NEW_LOGGING
294                 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
295                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n", 
296                         db_strerror(rc), rc ));
297 #else
298                 Debug( LDAP_DEBUG_ANY,
299                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
300                         db_strerror(rc), rc, 0 );
301 #endif
302                 goto done;
303         }
304         
305         /*
306          * just (re)add them for now
307          * assume that some other routine (not yet implemented)
308          * will zap index databases
309          *
310          */
311
312 #ifdef NEW_LOGGING
313         LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
314                 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", 
315                         (long) id, e->e_dn ));
316 #else
317         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
318                 (long) id, e->e_dn, 0 );
319 #endif
320
321         /* add dn2id indices */
322         if ( be_issuffix( be, &e->e_nname ) ) {
323                 pdn = slap_empty_bv;
324         } else {
325                 dnParent( &e->e_nname, &pdn );
326         }
327         rc = bdb_dn2id_add( be, tid, &pdn, e );
328         if( rc != 0 && rc != DB_KEYEXIST ) {
329 #ifdef NEW_LOGGING
330                 LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
331                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n", 
332                         db_strerror(rc), rc ));
333 #else
334                 Debug( LDAP_DEBUG_ANY,
335                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
336                         db_strerror(rc), rc, 0 );
337 #endif
338                 goto done;
339         }
340
341         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
342
343 done:
344         if( rc == 0 ) {
345                 rc = TXN_COMMIT( tid, 0 );
346                 if( rc != 0 ) {
347 #ifdef NEW_LOGGING
348                         LDAP_LOG (( "tools", LDAP_LEVEL_ERR,
349                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n", 
350                                 db_strerror(rc), rc ));
351 #else
352                         Debug( LDAP_DEBUG_ANY,
353                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
354                                 db_strerror(rc), rc, 0 );
355 #endif
356                         e->e_id = NOID;
357                 }
358
359         } else {
360                 TXN_ABORT( tid );
361 #ifdef NEW_LOGGING
362                 LDAP_LOG (( "tools", LDAP_LEVEL_DETAIL1,
363                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n", 
364                         db_strerror(rc), rc ));
365 #else
366                 Debug( LDAP_DEBUG_ANY,
367                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
368                         db_strerror(rc), rc, 0 );
369 #endif
370                 e->e_id = NOID;
371         }
372
373         return rc;
374 }