]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
892db3ee73d859b8996cc3a3ae54f39b5ed5eb33
[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, 0 );
68                 if( rc != 0 ) {
69                         return NOID;
70                 }
71         }
72
73         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
74
75         if( rc != 0 ) {
76                 return NOID;
77         }
78
79         if( data.data == NULL ) {
80                 return NOID;
81         }
82
83         AC_MEMCPY( &id, key.data, key.size );
84         return id;
85 }
86
87 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
88 {
89         int rc;
90         Entry *e;
91         struct berval bv;
92
93         assert( be != NULL );
94         assert( slapMode & SLAP_TOOL_MODE );
95         assert( data.data != NULL );
96
97         DBT2bv( &data, &bv );
98
99         rc = entry_decode( &bv, &e );
100
101         if( rc == LDAP_SUCCESS ) {
102                 e->e_id = id;
103         }
104
105         return e;
106 }
107
108 ID bdb_tool_entry_put(
109         BackendDB *be,
110         Entry *e )
111 {
112         int rc;
113         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
114         DB_TXN *tid = NULL;
115
116         assert( be != NULL );
117         assert( slapMode & SLAP_TOOL_MODE );
118
119         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
120                 (long) e->e_id, e->e_dn, 0 );
121
122         if( bdb->bi_txn ) {
123                 rc = txn_begin( bdb->bi_dbenv, NULL, &tid, 0 );
124                 if( rc != 0 ) {
125                         Debug( LDAP_DEBUG_ANY,
126                                 "=> bdb_tool_entry_put: txn_begin failed: %s (%d)\n",
127                                 db_strerror(rc), rc, 0 );
128                         return NOID;
129                 }
130         }
131
132         rc = bdb_next_id( be, tid, &e->e_id );
133         if( rc != 0 ) {
134                 Debug( LDAP_DEBUG_ANY,
135                         "=> bdb_tool_entry_put: next_id failed: %s (%d)\n",
136                         db_strerror(rc), rc, 0 );
137                 goto done;
138         }
139
140         /* add dn2id indices */
141         rc = bdb_dn2id_add( be, tid, e->e_ndn, e->e_id );
142         if( rc != 0 ) {
143                 Debug( LDAP_DEBUG_ANY,
144                         "=> bdb_tool_entry_put: dn2id_add failed: %s (%d)\n",
145                         db_strerror(rc), rc, 0 );
146                 goto done;
147         }
148
149         /* id2entry index */
150         rc = bdb_id2entry_add( be, tid, e );
151         if( rc != 0 ) {
152                 Debug( LDAP_DEBUG_ANY,
153                         "=> bdb_tool_entry_put: id2entry_add failed: %s (%d)\n",
154                         db_strerror(rc), rc, 0 );
155                 goto done;
156         }
157
158         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
159         if( rc != 0 ) {
160                 Debug( LDAP_DEBUG_ANY,
161                         "=> bdb_tool_entry_put: index_entry_add failed: %s (%d)\n",
162                         db_strerror(rc), rc, 0 );
163                 goto done;
164         }
165
166 done:
167         if( bdb->bi_txn ) {
168                 if( rc == 0 ) {
169                         rc = txn_commit( tid, 0 );
170                         if( rc != 0 ) {
171                                 Debug( LDAP_DEBUG_ANY,
172                                         "=> bdb_tool_entry_put: txn_commit failed: %s (%d)\n",
173                                         db_strerror(rc), rc, 0 );
174                                 e->e_id = NOID;
175                         }
176
177                 } else {
178                         txn_abort( tid );
179                         Debug( LDAP_DEBUG_ANY,
180                                 "=> bdb_tool_entry_put: txn_aborted! %s (%d)\n",
181                                 db_strerror(rc), rc, 0 );
182                         e->e_id = NOID;
183                 }
184         }
185
186         return e->e_id;
187 }
188
189 int bdb_tool_entry_reindex(
190         BackendDB *be,
191         ID id )
192 {
193         struct bdb_info *bi = (struct bdb_info *) be->be_private;
194         int rc;
195         Entry *e;
196         DB_TXN *tid = NULL;
197
198         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
199                 (long) id, 0, 0 );
200
201         e = bdb_tool_entry_get( be, id );
202
203         if( e == NULL ) {
204                 Debug( LDAP_DEBUG_ANY,
205                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
206                         (long) id, 0, 0 );
207                 return -1;
208         }
209
210         if( bi->bi_txn ) {
211                 rc = txn_begin( bi->bi_dbenv, NULL, &tid, 0 );
212                 if( rc != 0 ) {
213                         Debug( LDAP_DEBUG_ANY,
214                                 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
215                                 db_strerror(rc), rc, 0 );
216                         goto done;
217                 }
218         }
219         
220         /*
221          * just (re)add them for now
222          * assume that some other routine (not yet implemented)
223          * will zap index databases
224          *
225          */
226
227         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
228                 (long) id, e->e_dn, 0 );
229
230         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
231
232         if( bi->bi_txn ) {
233                 if( rc == 0 ) {
234                         rc = txn_commit( tid, 0 );
235                         if( rc != 0 ) {
236                                 Debug( LDAP_DEBUG_ANY,
237                                         "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
238                                         db_strerror(rc), rc, 0 );
239                                 e->e_id = NOID;
240                         }
241
242                 } else {
243                         txn_abort( tid );
244                         Debug( LDAP_DEBUG_ANY,
245                                 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
246                                 db_strerror(rc), rc, 0 );
247                         e->e_id = NOID;
248                 }
249         }
250
251 done:
252         return rc;
253 }