]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
ITS#1716 is_entry_subentr/ies/y/
[openldap] / servers / slapd / back-bdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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 {
85         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
86         DB *db = bdb->bi_id2entry->bdi_db;
87         DBT key, data;
88         struct berval bv;
89         int rc = 0;
90
91         *e = NULL;
92
93         DBTzero( &key );
94         key.data = (char *) &id;
95         key.size = sizeof(ID);
96
97         DBTzero( &data );
98         data.flags = DB_DBT_MALLOC;
99
100         if ((*e = bdb_cache_find_entry_id(&bdb->bi_cache, id, rw)) != NULL) {
101                 return 0;
102         }
103
104         /* fetch it */
105         rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
106
107         if( rc != 0 ) {
108                 return rc;
109         }
110
111         DBT2bv( &data, &bv );
112
113         rc = entry_decode( &bv, e );
114
115         if( rc == 0 ) {
116                 (*e)->e_id = id;
117         } else {
118                 /* only free on error. On success, the entry was
119                  * decoded in place.
120                  */
121                 ch_free( data.data );
122         }
123
124         if (rc == 0 && bdb_cache_add_entry_rw(&bdb->bi_cache, *e, rw) != 0) {
125                 if ((*e)->e_private != NULL)
126                         free ((*e)->e_private);
127                 (*e)->e_private = NULL;
128                 bdb_entry_return (*e);
129                 if ((*e=bdb_cache_find_entry_id(&bdb->bi_cache,id,rw)) != NULL) {
130                         return 0;
131                 }
132         }
133
134 #ifdef BDB_HIER
135         bdb_fix_dn(be, id, *e);
136 #endif
137
138         if (rc == 0) {
139                 bdb_cache_entry_commit(*e);
140         }
141
142         return rc;
143 }
144
145 int bdb_id2entry_delete(
146         BackendDB *be,
147         DB_TXN *tid,
148         Entry *e )
149 {
150         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
151         DB *db = bdb->bi_id2entry->bdi_db;
152         DBT key;
153         int rc;
154
155         bdb_cache_delete_entry(&bdb->bi_cache, e);
156
157         DBTzero( &key );
158         key.data = (char *) &e->e_id;
159         key.size = sizeof(ID);
160
161         /* delete from database */
162         rc = db->del( db, tid, &key, 0 );
163
164         return rc;
165 }
166
167 int bdb_entry_return(
168         Entry *e )
169 {
170         /* Our entries are allocated in two blocks; the data comes from
171          * the db itself and the Entry structure and associated pointers
172          * are allocated in entry_decode. The db data pointer is saved
173          * in e_bv. Since the Entry structure is allocated as a single
174          * block, e_attrs is always a fixed offset from e. The exception
175          * is when an entry has been modified, in which case we also need
176          * to free e_attrs.
177          */
178         if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
179                 entry_free( e );
180                 return 0;
181         }
182         if( (void *) e->e_attrs != (void *) (e+1)) {
183                 attrs_free( e->e_attrs );
184         }
185
186         /* See if the DNs were changed by modrdn */
187         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
188                 e->e_bv.bv_val + e->e_bv.bv_len ) {
189                 ch_free(e->e_name.bv_val);
190                 ch_free(e->e_nname.bv_val);
191                 e->e_name.bv_val = NULL;
192                 e->e_nname.bv_val = NULL;
193         }
194 #ifdef BDB_HIER
195         /* We had to construct the dn and ndn as well, in a single block */
196         if( e->e_name.bv_val ) {
197                 free( e->e_name.bv_val );
198         }
199 #endif
200         /* In tool mode the e_bv buffer is realloc'd, leave it alone */
201         if( !(slapMode & SLAP_TOOL_MODE) ) {
202                 free( e->e_bv.bv_val );
203         }
204
205         free( e );
206
207         return 0;
208 }
209
210 int bdb_entry_release(
211         BackendDB *be,
212         Connection *c,
213         Operation *o,
214         Entry *e,
215         int rw )
216 {
217         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
218  
219         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
220                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
221  
222         if ( slapMode == SLAP_SERVER_MODE ) {
223                 /* free entry and reader or writer lock */
224                 bdb_cache_return_entry_rw( &bdb->bi_cache, e, rw );
225         } else {
226                 if (e->e_private != NULL)
227                         free (e->e_private);
228                 e->e_private = NULL;
229                 bdb_entry_return ( e );
230         }
231  
232         return 0;
233 }