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