]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
Missed a test in AttributeDescription commit
[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_add(
16         BackendDB *be,
17         DB_TXN *tid,
18         Entry *e )
19 {
20         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
21         DB *db = bdb->bi_id2entry->bdi_db;
22         DBT key, data;
23         struct berval *bv;
24         int rc;
25
26         DBTzero( &key );
27         key.data = (char *) &e->e_id;
28         key.size = sizeof(ID);
29
30         rc = entry_encode( e, &bv );
31         if( rc != LDAP_SUCCESS ) {
32                 return -1;
33         }
34
35         DBTzero( &data );
36         bv2DBT( bv, &data );
37
38         rc = db->put( db, tid, &key, &data, DB_NOOVERWRITE );
39
40         ber_bvfree( bv );
41         return rc;
42 }
43
44 int bdb_id2entry_update(
45         BackendDB *be,
46         DB_TXN *tid,
47         Entry *e )
48 {
49         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
50         DB *db = bdb->bi_id2entry->bdi_db;
51         DBT key, data;
52         struct berval *bv;
53         int rc;
54
55         DBTzero( &key );
56         key.data = (char *) &e->e_id;
57         key.size = sizeof(ID);
58
59         rc = entry_encode( e, &bv );
60         if( rc != LDAP_SUCCESS ) {
61                 return -1;
62         }
63
64         DBTzero( &data );
65         bv2DBT( bv, &data );
66
67         rc = db->put( db, tid, &key, &data, 0 );
68
69         ber_bvfree( bv );
70         return rc;
71 }
72
73 int bdb_id2entry(
74         BackendDB *be,
75         DB_TXN *tid,
76         ID id,
77         Entry **e )
78 {
79         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
80         DB *db = bdb->bi_id2entry->bdi_db;
81         DBT key, data;
82         struct berval bv;
83         int rc;
84
85         *e = NULL;
86
87         DBTzero( &key );
88         key.data = (char *) &id;
89         key.size = sizeof(ID);
90
91         DBTzero( &data );
92         data.flags = DB_DBT_MALLOC;
93
94         /* fetch it */
95         rc = db->get( db, tid, &key, &data, 0 );
96
97         if( rc != 0 ) {
98                 return rc;
99         }
100
101         DBT2bv( &data, &bv );
102
103         rc = entry_decode( &bv, e );
104
105         if( rc == 0 ) {
106                 (*e)->e_id = id;
107         } else {
108                 /* only free on error. On success, the entry was
109                  * decoded in place.
110                  */
111                 ch_free( data.data );
112         }
113         return rc;
114 }
115
116 int bdb_id2entry_delete(
117         BackendDB *be,
118         DB_TXN *tid,
119         ID id )
120 {
121         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
122         DB *db = bdb->bi_id2entry->bdi_db;
123         DBT key;
124         struct berval *bv;
125         int rc;
126
127         DBTzero( &key );
128         key.data = (char *) &id;
129         key.size = sizeof(ID);
130
131         rc = db->del( db, tid, &key, 0 );
132
133         ber_bvfree( bv );
134         return rc;
135 }
136
137 int bdb_entry_return(
138         BackendDB *be,
139         Entry *e )
140 {
141         /* Our entries are almost always contiguous blocks, so a single
142          * free() on the Entry pointer suffices. The exception is when
143          * an entry has been modified, in which case the attr list will
144          * have been alloc'd separately.
145          */
146         if ((void *)e->e_attrs < (void *)e  ||
147                 (void *)e->e_attrs > e->e_private)
148             attrs_free(e->e_attrs);
149
150         ch_free(e);
151
152         return 0;
153 }