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