]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
Update copyright statements
[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 int bdb_id2entry_add(
57         BackendDB *be,
58         DB_TXN *tid,
59         Entry *e )
60 {
61         return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
62 }
63
64 int bdb_id2entry_update(
65         BackendDB *be,
66         DB_TXN *tid,
67         Entry *e )
68 {
69         return bdb_id2entry_put(be, tid, e, 0);
70 }
71
72 int bdb_id2entry(
73         BackendDB *be,
74         DB_TXN *tid,
75         ID id,
76         Entry **e )
77 {
78         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
79         DB *db = bdb->bi_id2entry->bdi_db;
80         DBT key, data;
81         struct berval bv;
82         int rc;
83
84         *e = NULL;
85
86         DBTzero( &key );
87         key.data = (char *) &id;
88         key.size = sizeof(ID);
89
90         DBTzero( &data );
91         data.flags = DB_DBT_MALLOC;
92
93         /* fetch it */
94         rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
95
96         if( rc != 0 ) {
97                 return rc;
98         }
99
100         DBT2bv( &data, &bv );
101
102         rc = entry_decode( &bv, e );
103
104         if( rc == 0 ) {
105                 (*e)->e_id = id;
106         } else {
107                 /* only free on error. On success, the entry was
108                  * decoded in place.
109                  */
110                 ch_free( data.data );
111         }
112 #ifdef BDB_HIER
113         bdb_fix_dn(be, id, *e);
114 #endif
115         return rc;
116 }
117
118 int bdb_id2entry_delete(
119         BackendDB *be,
120         DB_TXN *tid,
121         ID id )
122 {
123         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
124         DB *db = bdb->bi_id2entry->bdi_db;
125         DBT key;
126         int rc;
127
128         DBTzero( &key );
129         key.data = (char *) &id;
130         key.size = sizeof(ID);
131
132         rc = db->del( db, tid, &key, 0 );
133
134         return rc;
135 }
136
137 int bdb_entry_return(
138         BackendDB *be,
139         Entry *e )
140 {
141         /* Our entries are allocated in two blocks; the data comes from
142          * the db itself and the Entry structure and associated pointers
143          * are allocated in entry_decode. The db data pointer is saved
144          * in e_private. Since the Entry structure is allocated as a single
145          * block, e_attrs is always a fixed offset from e. The exception
146          * is when an entry has been modified, in which case we also need
147          * to free e_attrs.
148          */
149         if( (void *) e->e_attrs != (void *) (e+1)) {
150                 attrs_free( e->e_attrs );
151         }
152 #ifdef BDB_HIER
153         /* We had to construct the dn and ndn as well, in a single block */
154         free( e->e_dn );
155 #endif
156         /* In tool mode the e_private buffer is realloc'd, leave it alone */
157         if( e->e_private && !(slapMode & SLAP_TOOL_MODE) ) {
158                 free( e->e_private );
159         }
160
161         free( e );
162
163         return 0;
164 }
165
166 int bdb_entry_release(
167         BackendDB *be,
168         Connection *c,
169         Operation *o,
170         Entry *e,
171         int rw )
172 {
173         int retval = 0;
174
175         if (o && o->o_tag == LDAP_REQ_ADD)
176                 entry_free(e);
177         else
178                 retval = bdb_entry_return( be, e );
179
180         return retval;
181 }