]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
Missed a spot where root DN_SUBTREE index was still getting written.
[openldap] / servers / slapd / back-bdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
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 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
27         DBTzero( &key );
28         key.data = (char *) &e->e_id;
29         key.size = sizeof(ID);
30
31         rc = entry_encode( e, &bv );
32         if( rc != LDAP_SUCCESS ) {
33                 return -1;
34         }
35
36         DBTzero( &data );
37         bv2DBT( &bv, &data );
38
39         rc = db->put( db, tid, &key, &data, flag );
40
41         free( bv.bv_val );
42         return rc;
43 }
44
45 int bdb_id2entry_add(
46         BackendDB *be,
47         DB_TXN *tid,
48         Entry *e )
49 {
50         return bdb_id2entry_put( be, tid, e, DB_NOOVERWRITE );
51 }
52
53 int bdb_id2entry_update(
54         BackendDB *be,
55         DB_TXN *tid,
56         Entry *e )
57 {
58         return bdb_id2entry_put( be, tid, e, 0 );
59 }
60
61 int bdb_id2entry(
62         BackendDB *be,
63         DB_TXN *tid,
64         ID id,
65         Entry **e )
66 {
67         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
68         DB *db = bdb->bi_id2entry->bdi_db;
69         DBT key, data;
70         struct berval bv;
71         int rc;
72
73         *e = NULL;
74
75         DBTzero( &key );
76         key.data = (char *) &id;
77         key.size = sizeof(ID);
78
79         DBTzero( &data );
80         data.flags = DB_DBT_MALLOC;
81
82         /* fetch it */
83         rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
84
85         if( rc != 0 ) {
86                 return rc;
87         }
88
89         DBT2bv( &data, &bv );
90
91         rc = entry_decode( &bv, e );
92
93         if( rc == 0 ) {
94                 (*e)->e_id = id;
95         } else {
96                 /* only free on error. On success, the entry was
97                  * decoded in place.
98                  */
99                 ch_free( data.data );
100         }
101         return rc;
102 }
103
104 int bdb_id2entry_delete(
105         BackendDB *be,
106         DB_TXN *tid,
107         ID id )
108 {
109         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
110         DB *db = bdb->bi_id2entry->bdi_db;
111         DBT key;
112         int rc;
113
114         DBTzero( &key );
115         key.data = (char *) &id;
116         key.size = sizeof(ID);
117
118         rc = db->del( db, tid, &key, 0 );
119
120         return rc;
121 }
122
123 int bdb_entry_return(
124         BackendDB *be,
125         Entry *e )
126 {
127         /* Our entries are allocated in two blocks; the data comes from
128          * the db itself and the Entry structure and associated pointers
129          * are allocated in entry_decode. The db data pointer is saved
130          * in e_private. Since the Entry structure is allocated as a single
131          * block, e_attrs is always a fixed offset from e. The exception
132          * is when an entry has been modified, in which case we also need
133          * to free e_attrs.
134          */
135         if( (void *) e->e_attrs != (void *) (e+1)) {
136                 attrs_free( e->e_attrs );
137         }
138         if( e->e_private ) {
139                 free( e->e_private );
140         }
141
142         free( e );
143
144         return 0;
145 }
146 int bdb_entry_release(
147         BackendDB *be,
148         Connection *c,
149         Operation *o,
150         Entry *e,
151         int rw )
152 {
153         /* A tool will call this with NULL Connection and Operation
154          * pointers. We don't need to free the e_private in that case,
155          * because the tool is getting entries into a realloc'd
156          * buffer.
157          */
158         if( c && o ) {
159                 return bdb_entry_return( be, e );
160         } else {
161                 free( e );
162         }
163 }