]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
Fix previous commit
[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         return rc;
142 }
143
144 int bdb_id2entry_delete(
145         BackendDB *be,
146         DB_TXN *tid,
147         Entry *e )
148 {
149         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
150         DB *db = bdb->bi_id2entry->bdi_db;
151         DBT key;
152         int rc;
153
154         bdb_cache_delete_entry(&bdb->bi_cache, e);
155
156         DBTzero( &key );
157         key.data = (char *) &e->e_id;
158         key.size = sizeof(ID);
159
160         /* delete from database */
161         rc = db->del( db, tid, &key, 0 );
162
163         return rc;
164 }
165
166 int bdb_entry_return(
167         Entry *e )
168 {
169         /* Our entries are allocated in two blocks; the data comes from
170          * the db itself and the Entry structure and associated pointers
171          * are allocated in entry_decode. The db data pointer is saved
172          * in e_bv. Since the Entry structure is allocated as a single
173          * block, e_attrs is always a fixed offset from e. The exception
174          * is when an entry has been modified, in which case we also need
175          * to free e_attrs.
176          */
177         if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
178                 entry_free( e );
179                 return 0;
180         }
181         if( (void *) e->e_attrs != (void *) (e+1)) {
182                 attrs_free( e->e_attrs );
183         }
184
185         /* See if the DNs were changed by modrdn */
186         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
187                 e->e_bv.bv_val + e->e_bv.bv_len ) {
188                 ch_free(e->e_name.bv_val);
189                 ch_free(e->e_nname.bv_val);
190                 e->e_name.bv_val = NULL;
191                 e->e_nname.bv_val = NULL;
192         }
193 #ifdef BDB_HIER
194         /* We had to construct the dn and ndn as well, in a single block */
195         if( e->e_name.bv_val ) {
196                 free( e->e_name.bv_val );
197         }
198 #endif
199         /* In tool mode the e_bv buffer is realloc'd, leave it alone */
200         if( !(slapMode & SLAP_TOOL_MODE) ) {
201                 free( e->e_bv.bv_val );
202         }
203
204         free( e );
205
206         return 0;
207 }
208
209 int bdb_entry_release(
210         BackendDB *be,
211         Connection *c,
212         Operation *o,
213         Entry *e,
214         int rw )
215 {
216         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
217  
218         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
219                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
220  
221         if ( slapMode == SLAP_SERVER_MODE ) {
222                 /* free entry and reader or writer lock */
223                 bdb_cache_return_entry_rw( &bdb->bi_cache, e, rw );
224         } else {
225                 if (e->e_private != NULL)
226                         free (e->e_private);
227                 e->e_private = NULL;
228                 bdb_entry_return ( e );
229         }
230  
231         return 0;
232 }