]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Fix previous commit
[openldap] / servers / slapd / back-bdb / 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 #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         return e;
107 }
108
109 ID bdb_tool_entry_put(
110         BackendDB *be,
111         Entry *e )
112 {
113         int rc;
114         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
115         DB_TXN *tid = NULL;
116         char *pdn;
117
118         assert( be != NULL );
119         assert( slapMode & SLAP_TOOL_MODE );
120
121         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
122                 (long) e->e_id, e->e_dn, 0 );
123
124         if( bdb->bi_txn ) {
125                 rc = txn_begin( bdb->bi_dbenv, NULL, &tid, 
126                         bdb->bi_db_opflags );
127                 if( rc != 0 ) {
128                         Debug( LDAP_DEBUG_ANY,
129                                 "=> bdb_tool_entry_put: txn_begin failed: %s (%d)\n",
130                                 db_strerror(rc), rc, 0 );
131                         return NOID;
132                 }
133         }
134
135         rc = bdb_next_id( be, tid, &e->e_id );
136         if( rc != 0 ) {
137                 Debug( LDAP_DEBUG_ANY,
138                         "=> bdb_tool_entry_put: next_id failed: %s (%d)\n",
139                         db_strerror(rc), rc, 0 );
140                 goto done;
141         }
142
143         /* add dn2id indices */
144         pdn = dn_parent( be, e->e_ndn );
145         rc = bdb_dn2id_add( be, tid, pdn, e );
146         if( pdn ) free( pdn );
147         if( rc != 0 ) {
148                 Debug( LDAP_DEBUG_ANY,
149                         "=> bdb_tool_entry_put: dn2id_add failed: %s (%d)\n",
150                         db_strerror(rc), rc, 0 );
151                 goto done;
152         }
153
154         /* id2entry index */
155         rc = bdb_id2entry_add( be, tid, e );
156         if( rc != 0 ) {
157                 Debug( LDAP_DEBUG_ANY,
158                         "=> bdb_tool_entry_put: id2entry_add failed: %s (%d)\n",
159                         db_strerror(rc), rc, 0 );
160                 goto done;
161         }
162
163         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
164         if( rc != 0 ) {
165                 Debug( LDAP_DEBUG_ANY,
166                         "=> bdb_tool_entry_put: index_entry_add failed: %s (%d)\n",
167                         db_strerror(rc), rc, 0 );
168                 goto done;
169         }
170
171 done:
172         if( bdb->bi_txn ) {
173                 if( rc == 0 ) {
174                         rc = txn_commit( tid, 0 );
175                         if( rc != 0 ) {
176                                 Debug( LDAP_DEBUG_ANY,
177                                         "=> bdb_tool_entry_put: txn_commit failed: %s (%d)\n",
178                                         db_strerror(rc), rc, 0 );
179                                 e->e_id = NOID;
180                         }
181
182                 } else {
183                         txn_abort( tid );
184                         Debug( LDAP_DEBUG_ANY,
185                                 "=> bdb_tool_entry_put: txn_aborted! %s (%d)\n",
186                                 db_strerror(rc), rc, 0 );
187                         e->e_id = NOID;
188                 }
189         }
190
191         return e->e_id;
192 }
193
194 int bdb_tool_entry_reindex(
195         BackendDB *be,
196         ID id )
197 {
198         struct bdb_info *bi = (struct bdb_info *) be->be_private;
199         int rc;
200         Entry *e;
201         DB_TXN *tid = NULL;
202         char *pdn;
203
204         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
205                 (long) id, 0, 0 );
206
207         e = bdb_tool_entry_get( be, id );
208
209         if( e == NULL ) {
210                 Debug( LDAP_DEBUG_ANY,
211                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
212                         (long) id, 0, 0 );
213                 return -1;
214         }
215
216         if( bi->bi_txn ) {
217                 rc = txn_begin( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
218                 if( rc != 0 ) {
219                         Debug( LDAP_DEBUG_ANY,
220                                 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
221                                 db_strerror(rc), rc, 0 );
222                         goto done;
223                 }
224         }
225         
226         /*
227          * just (re)add them for now
228          * assume that some other routine (not yet implemented)
229          * will zap index databases
230          *
231          */
232
233         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
234                 (long) id, e->e_dn, 0 );
235
236         /* add dn2id indices */
237         pdn = dn_parent( be, e->e_ndn );
238         rc = bdb_dn2id_add( be, tid, pdn, e );
239         if( pdn ) free( pdn );
240         if( rc != 0 && rc != DB_KEYEXIST ) {
241                 Debug( LDAP_DEBUG_ANY,
242                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
243                         db_strerror(rc), rc, 0 );
244                 goto done;
245         }
246
247         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
248
249 done:
250         if( bi->bi_txn ) {
251                 if( rc == 0 ) {
252                         rc = txn_commit( tid, 0 );
253                         if( rc != 0 ) {
254                                 Debug( LDAP_DEBUG_ANY,
255                                         "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
256                                         db_strerror(rc), rc, 0 );
257                                 e->e_id = NOID;
258                         }
259
260                 } else {
261                         txn_abort( tid );
262                         Debug( LDAP_DEBUG_ANY,
263                                 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
264                                 db_strerror(rc), rc, 0 );
265                         e->e_id = NOID;
266                 }
267         }
268
269         return rc;
270 }