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