]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
Fix slapd directoryString exact index normalization bug
[openldap] / servers / slapd / back-bdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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 int bdb_id2entry_put(
16         BackendDB *be,
17         DB_TXN *tid,
18         Entry *e,
19         int flag )
20 {
21         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
22         DB *db = bdb->bi_id2entry->bdi_db;
23         DBT key, data;
24         struct berval bv;
25         int rc;
26 #ifdef BDB_HIER
27         char *odn, *ondn;
28
29         /* We only store rdns, and they go in the id2parent database. */
30
31         odn = e->e_dn; ondn = e->e_ndn;
32
33         e->e_dn = ""; e->e_ndn = "";
34 #endif
35         DBTzero( &key );
36         key.data = (char *) &e->e_id;
37         key.size = sizeof(ID);
38
39         rc = entry_encode( e, &bv );
40 #ifdef BDB_HIER
41         e->e_dn = odn; e->e_ndn = ondn;
42 #endif
43         if( rc != LDAP_SUCCESS ) {
44                 return -1;
45         }
46
47         DBTzero( &data );
48         bv2DBT( &bv, &data );
49
50         rc = db->put( db, tid, &key, &data, flag );
51
52         free( bv.bv_val );
53         return rc;
54 }
55
56 /*
57  * This routine adds (or updates) an entry on disk.
58  * The cache should be already be updated.
59  */
60
61
62 int bdb_id2entry_add(
63         BackendDB *be,
64         DB_TXN *tid,
65         Entry *e )
66 {
67         return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
68 }
69
70 int bdb_id2entry_update(
71         BackendDB *be,
72         DB_TXN *tid,
73         Entry *e )
74 {
75         return bdb_id2entry_put(be, tid, e, 0);
76 }
77
78 int bdb_id2entry_rw(
79         BackendDB *be,
80         DB_TXN *tid,
81         ID id,
82         Entry **e,
83         int rw,
84         u_int32_t locker,
85         DB_LOCK *lock )
86 {
87         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
88         DB *db = bdb->bi_id2entry->bdi_db;
89         DBT key, data;
90         struct berval bv;
91         int rc = 0, ret = 0;
92
93         *e = NULL;
94
95         DBTzero( &key );
96         key.data = (char *) &id;
97         key.size = sizeof(ID);
98
99         DBTzero( &data );
100         data.flags = DB_DBT_MALLOC;
101
102         if ((*e = bdb_cache_find_entry_id(bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock)) != NULL) {
103                 return 0;
104         }
105
106         /* fetch it */
107         rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags | ( rw ? DB_RMW : 0 ));
108
109         if( rc != 0 ) {
110                 return rc;
111         }
112
113         DBT2bv( &data, &bv );
114
115         rc = entry_decode( &bv, e );
116
117         if( rc == 0 ) {
118                 (*e)->e_id = id;
119         } else {
120                 /* only free on error. On success, the entry was
121                  * decoded in place.
122                  */
123                 ch_free( data.data );
124         }
125
126         if ( rc == 0 ) {
127 #ifdef BDB_HIER
128                 bdb_fix_dn(be, id, *e);
129 #endif
130                 ret = bdb_cache_add_entry_rw( bdb->bi_dbenv,
131                                 &bdb->bi_cache, *e, rw, locker, lock);
132                 while ( ret == 1 || ret == -1 ) {
133                         Entry *ee;
134                         int add_loop_cnt = 0;
135                         if ( (*e)->e_private != NULL ) {
136                                 free ((*e)->e_private);
137                         }
138                         (*e)->e_private = NULL;
139                         if ( (ee = bdb_cache_find_entry_id
140                                         (bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock) ) != NULL) {
141                                 bdb_entry_return ( *e );
142                                 *e = ee;
143                                 return 0;
144                         }
145                         if ( ++add_loop_cnt == BDB_MAX_ADD_LOOP ) {
146                                 bdb_entry_return ( *e );
147                                 *e = NULL;
148                                 return LDAP_BUSY;
149                         }
150                 }
151                 if ( ret != 0 ) {
152                         if ( (*e)->e_private != NULL )
153                                 free ( (*e)->e_private );
154                         bdb_entry_return( *e );
155                         *e = NULL;
156                 }
157                 rc = ret;
158         }
159
160         if (rc == 0) {
161                 bdb_cache_entry_commit(*e);
162         }
163
164         return rc;
165 }
166
167 int bdb_id2entry_delete(
168         BackendDB *be,
169         DB_TXN *tid,
170         Entry *e )
171 {
172         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
173         DB *db = bdb->bi_id2entry->bdi_db;
174         DBT key;
175         int rc;
176
177         bdb_cache_delete_entry(&bdb->bi_cache, e);
178
179         DBTzero( &key );
180         key.data = (char *) &e->e_id;
181         key.size = sizeof(ID);
182
183         /* delete from database */
184         rc = db->del( db, tid, &key, 0 );
185
186         return rc;
187 }
188
189 int bdb_entry_return(
190         Entry *e )
191 {
192         /* Our entries are allocated in two blocks; the data comes from
193          * the db itself and the Entry structure and associated pointers
194          * are allocated in entry_decode. The db data pointer is saved
195          * in e_bv. Since the Entry structure is allocated as a single
196          * block, e_attrs is always a fixed offset from e. The exception
197          * is when an entry has been modified, in which case we also need
198          * to free e_attrs.
199          */
200         if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
201                 entry_free( e );
202                 return 0;
203         }
204         if( (void *) e->e_attrs != (void *) (e+1)) {
205                 attrs_free( e->e_attrs );
206         }
207 #ifdef SLAP_NVALUES
208         else {
209                 /* nvals are not contiguous with the rest. oh well. */
210                 Attribute *a;
211                 for (a = e->e_attrs; a; a=a->a_next) {
212                         if (a->a_nvals) {
213                                 ber_bvarray_free( a->a_nvals );
214                                 a->a_nvals = NULL;
215                         }
216                 }
217         }
218 #endif
219
220 #ifndef BDB_HIER
221         /* See if the DNs were changed by modrdn */
222         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
223                 e->e_bv.bv_val + e->e_bv.bv_len ) {
224                 ch_free(e->e_name.bv_val);
225                 ch_free(e->e_nname.bv_val);
226                 e->e_name.bv_val = NULL;
227                 e->e_nname.bv_val = NULL;
228         }
229 #else
230         /* We had to construct the dn and ndn as well, in a single block */
231         if( e->e_name.bv_val ) {
232                 free( e->e_name.bv_val );
233         }
234 #endif
235         /* In tool mode the e_bv buffer is realloc'd, leave it alone */
236         if( !(slapMode & SLAP_TOOL_MODE) ) {
237                 free( e->e_bv.bv_val );
238         }
239
240         free( e );
241
242         return 0;
243 }
244
245 int bdb_entry_release(
246         BackendDB *be,
247         Connection *c,
248         Operation *o,
249         Entry *e,
250         int rw )
251 {
252         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
253  
254         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
255                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
256  
257         if ( slapMode == SLAP_SERVER_MODE ) {
258                 /* free entry and reader or writer lock */
259                 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
260         } else {
261                 if (e->e_private != NULL)
262                         free (e->e_private);
263                 e->e_private = NULL;
264                 bdb_entry_return ( e );
265         }
266  
267         return 0;
268 }