1 /* id2entry.c - routines to deal with the id2entry database */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
15 #ifdef BDB_USE_BINARY_RW
17 /* A cache of every AttributeDescription we ever see. We don't want
18 * to churn thru malloc/free on them all the time since most will be
19 * encountered repeatedly.
21 static Avlnode *adcache;
26 AttributeDescription *ad
29 return strcasecmp(desc, ad->ad_cname->bv_val);
34 AttributeDescription *a1,
35 AttributeDescription *a2
38 return strcasecmp(a1->ad_cname->bv_val, a2->ad_cname->bv_val);
41 AttributeDescription *
42 bdb_str2ad(const char *desc)
44 AttributeDescription *a;
46 a = (AttributeDescription *) avl_find(adcache, desc,
47 (AVL_CMP) ad_type_cmp);
52 rc = slap_str2ad(desc, &a, &text);
53 if (rc != LDAP_SUCCESS) {
56 rc = avl_insert(&adcache, (caddr_t)a, (AVL_CMP) ad_info_cmp,
57 (AVL_DUP) avl_dup_error);
62 /* Flatten an Entry into a buffer. The buffer contents become a direct
63 * copy of the entry, with all pointers converted to offsets from the
64 * beginning of the buffer. We do this by first walking through all
65 * the fields of the Entry, adding up their sizes. Then a single chunk
66 * of memory is malloc'd and the entry is copied. We differentiate between
67 * fixed size fields and variable-length content when tallying up the
68 * entry size, so that we can stick all of the variable-length stuff
69 * into the back half of the buffer.
71 int bdb_encode(Entry *e, struct berval **bv)
73 int siz = sizeof(Entry);
78 struct berval **bvl, *bz;
79 char *ptr, *base, *data;
81 *bv = ch_malloc(sizeof(struct berval));
82 /* Compress any white space in the DN */
84 dnlen = strlen(e->e_dn);
85 /* The dn and ndn are always the same length */
86 len = dnlen + dnlen + 2; /* two trailing NUL bytes */
87 for (a=e->e_attrs; a; a=a->a_next) {
88 /* For AttributeDesc, we only store the attr name */
89 siz += sizeof(Attribute);
90 len += a->a_desc->ad_cname->bv_len+1;
91 for (i=0; a->a_vals[i]; i++) {
92 siz += sizeof(struct berval *);
93 siz += sizeof(struct berval);
94 len += a->a_vals[i]->bv_len + 1;
96 siz += sizeof(struct berval *); /* NULL pointer at end */
98 (*bv)->bv_len = siz + len;
99 (*bv)->bv_val = ch_malloc(siz+len);
100 base = (*bv)->bv_val;
103 data = (char *)(f+1);
105 f->e_dn = (char *)(ptr-base);
106 memcpy(ptr, e->e_dn, dnlen);
109 f->e_ndn = (char *)(ptr-base);
110 memcpy(ptr, e->e_ndn, dnlen);
113 f->e_attrs = e->e_attrs ? (Attribute *)sizeof(Entry) : NULL;
115 for (a=e->e_attrs; a; a=a->a_next) {
116 b = (Attribute *)data;
117 data = (char *)(b+1);
118 b->a_desc = (AttributeDescription *)(ptr-base);
119 memcpy(ptr, a->a_desc->ad_cname->bv_val,
120 a->a_desc->ad_cname->bv_len);
121 ptr += a->a_desc->ad_cname->bv_len;
124 bvl = (struct berval **)data;
125 b->a_vals = (struct berval **)(data-base);
126 for (i=0; a->a_vals[i]; i++);
127 data = (char *)(bvl+i+1);
128 bz = (struct berval *)data;
129 for (j=0; j<i; j++) {
130 bz->bv_len = a->a_vals[j]->bv_len;
131 if (a->a_vals[j]->bv_val) {
132 bz->bv_val = (char *)(ptr-base);
133 memcpy(ptr, a->a_vals[j]->bv_val, bz->bv_len);
139 bvl[j] = (struct berval *)(data-base);
149 b->a_next = (Attribute *)(data-base);
156 /* Retrieve an Entry that was stored using bdb_encode above.
157 * All we have to do is add the buffer address to all of the
158 * stored offsets. We also use the stored attribute names to
159 * pull AttributeDescriptions from our ad_cache. To detect if
160 * the attributes of an Entry are later modified, we also store
161 * the address of the end of this block in e_private. Since
162 * modify_internal always allocs a new list of attrs to work
163 * with, we need to free that separately.
165 int bdb_decode(struct berval *bv, Entry **e)
170 Entry *x = (Entry *)bv->bv_val;
172 base = (long)bv->bv_val;
175 x->e_private = bv->bv_val + bv->bv_len;
177 x->e_attrs = (Attribute *)((long)x->e_attrs+base);
178 for (a=x->e_attrs; a; a=a->a_next) {
180 a->a_next = (Attribute *)((long)a->a_next+base);
181 a->a_desc=bdb_str2ad((char *)a->a_desc+base);
182 if (!a->a_desc) return -1;
184 a->a_vals = (struct berval **)((long)a->a_vals+base);
185 for (i=0; a->a_vals[i]; i++) {
186 a->a_vals[i] = (struct berval *)
187 ((long)a->a_vals[i]+base);
188 if (a->a_vals[i]->bv_val)
189 a->a_vals[i]->bv_val += base;
197 #define entry_encode(a, b) bdb_encode(a,b)
198 #define entry_decode(a, b) bdb_decode(a,b)
200 #endif /* BDB_USE_BINARY_RW */
202 int bdb_id2entry_add(
207 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
208 DB *db = bdb->bi_id2entry->bdi_db;
214 key.data = (char *) &e->e_id;
215 key.size = sizeof(ID);
217 rc = entry_encode( e, &bv );
218 if( rc != LDAP_SUCCESS ) {
225 rc = db->put( db, tid, &key, &data, DB_NOOVERWRITE );
231 int bdb_id2entry_update(
236 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
237 DB *db = bdb->bi_id2entry->bdi_db;
243 key.data = (char *) &e->e_id;
244 key.size = sizeof(ID);
246 rc = entry_encode( e, &bv );
247 if( rc != LDAP_SUCCESS ) {
254 rc = db->put( db, tid, &key, &data, 0 );
266 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
267 DB *db = bdb->bi_id2entry->bdi_db;
275 key.data = (char *) &id;
276 key.size = sizeof(ID);
279 data.flags = DB_DBT_MALLOC;
282 rc = db->get( db, tid, &key, &data, 0 );
288 DBT2bv( &data, &bv );
290 rc = entry_decode( &bv, e );
296 #ifndef BDB_USE_BINARY_RW
297 ch_free( data.data );
302 int bdb_id2entry_delete(
307 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
308 DB *db = bdb->bi_id2entry->bdi_db;
314 key.data = (char *) &id;
315 key.size = sizeof(ID);
317 rc = db->del( db, tid, &key, 0 );
323 int bdb_entry_return(
327 #ifdef BDB_USE_BINARY_RW
328 /* bdb_modify_internal always operates on a dup'd set of attrs. */
329 if ((void *)e->e_attrs < (void *)e ||
330 (void *)e->e_attrs > e->e_private)
331 attrs_free(e->e_attrs);