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