]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/id2entry.c
Happy New Year!
[openldap] / servers / slapd / back-mdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2016 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/errno.h>
22
23 #include "back-mdb.h"
24
25 typedef struct Ecount {
26         ber_len_t len;
27         int nattrs;
28         int nvals;
29         int offset;
30 } Ecount;
31
32 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
33         Ecount *eh);
34 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data,
35         Ecount *ec);
36 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
37
38 #define ADD_FLAGS       (MDB_NOOVERWRITE|MDB_APPEND)
39
40 static int mdb_id2entry_put(
41         Operation *op,
42         MDB_txn *txn,
43         MDB_cursor *mc,
44         Entry *e,
45         int flag )
46 {
47         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
48         Ecount ec;
49         MDB_val key, data;
50         int rc;
51
52         /* We only store rdns, and they go in the dn2id database. */
53
54         key.mv_data = &e->e_id;
55         key.mv_size = sizeof(ID);
56
57         rc = mdb_entry_partsize( mdb, txn, e, &ec );
58         if (rc)
59                 return LDAP_OTHER;
60
61         flag |= MDB_RESERVE;
62
63         if (e->e_id < mdb->mi_nextid)
64                 flag &= ~MDB_APPEND;
65
66 again:
67         data.mv_size = ec.len;
68         if ( mc )
69                 rc = mdb_cursor_put( mc, &key, &data, flag );
70         else
71                 rc = mdb_put( txn, mdb->mi_id2entry, &key, &data, flag );
72         if (rc == MDB_SUCCESS) {
73                 rc = mdb_entry_encode( op, e, &data, &ec );
74                 if( rc != LDAP_SUCCESS )
75                         return rc;
76         }
77         if (rc) {
78                 /* Was there a hole from slapadd? */
79                 if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
80                         flag &= ~ADD_FLAGS;
81                         goto again;
82                 }
83                 Debug( LDAP_DEBUG_ANY,
84                         "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
85                         mdb_strerror(rc), rc,
86                         e->e_nname.bv_val );
87                 if ( rc != MDB_KEYEXIST )
88                         rc = LDAP_OTHER;
89         }
90         return rc;
91 }
92
93 /*
94  * This routine adds (or updates) an entry on disk.
95  * The cache should be already be updated.
96  */
97
98
99 int mdb_id2entry_add(
100         Operation *op,
101         MDB_txn *txn,
102         MDB_cursor *mc,
103         Entry *e )
104 {
105         return mdb_id2entry_put(op, txn, mc, e, ADD_FLAGS);
106 }
107
108 int mdb_id2entry_update(
109         Operation *op,
110         MDB_txn *txn,
111         MDB_cursor *mc,
112         Entry *e )
113 {
114         return mdb_id2entry_put(op, txn, mc, e, 0);
115 }
116
117 int mdb_id2edata(
118         Operation *op,
119         MDB_cursor *mc,
120         ID id,
121         MDB_val *data )
122 {
123         MDB_val key;
124         int rc;
125
126         key.mv_data = &id;
127         key.mv_size = sizeof(ID);
128
129         /* fetch it */
130         rc = mdb_cursor_get( mc, &key, data, MDB_SET );
131         /* stubs from missing parents - DB is actually invalid */
132         if ( rc == MDB_SUCCESS && !data->mv_size )
133                 rc = MDB_NOTFOUND;
134         return rc;
135 }
136
137 int mdb_id2entry(
138         Operation *op,
139         MDB_cursor *mc,
140         ID id,
141         Entry **e )
142 {
143         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
144         MDB_val key, data;
145         int rc = 0;
146
147         *e = NULL;
148
149         key.mv_data = &id;
150         key.mv_size = sizeof(ID);
151
152         /* fetch it */
153         rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
154         if ( rc == MDB_NOTFOUND ) {
155                 /* Looking for root entry on an empty-dn suffix? */
156                 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
157                         struct berval gluebv = BER_BVC("glue");
158                         Entry *r = mdb_entry_alloc(op, 2, 4);
159                         Attribute *a = r->e_attrs;
160                         struct berval *bptr;
161
162                         r->e_id = 0;
163                         r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
164                         bptr = a->a_vals;
165                         a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
166                         a->a_desc = slap_schema.si_ad_objectClass;
167                         a->a_nvals = a->a_vals;
168                         a->a_numvals = 1;
169                         *bptr++ = gluebv;
170                         BER_BVZERO(bptr);
171                         bptr++;
172                         a->a_next = a+1;
173                         a = a->a_next;
174                         a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
175                         a->a_desc = slap_schema.si_ad_structuralObjectClass;
176                         a->a_vals = bptr;
177                         a->a_nvals = a->a_vals;
178                         a->a_numvals = 1;
179                         *bptr++ = gluebv;
180                         BER_BVZERO(bptr);
181                         a->a_next = NULL;
182                         *e = r;
183                         return MDB_SUCCESS;
184                 }
185         }
186         /* stubs from missing parents - DB is actually invalid */
187         if ( rc == MDB_SUCCESS && !data.mv_size )
188                 rc = MDB_NOTFOUND;
189         if ( rc ) return rc;
190
191         rc = mdb_entry_decode( op, mdb_cursor_txn( mc ), &data, e );
192         if ( rc ) return rc;
193
194         (*e)->e_id = id;
195         (*e)->e_name.bv_val = NULL;
196         (*e)->e_nname.bv_val = NULL;
197
198         return rc;
199 }
200
201 int mdb_id2entry_delete(
202         BackendDB *be,
203         MDB_txn *tid,
204         Entry *e )
205 {
206         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
207         MDB_dbi dbi = mdb->mi_id2entry;
208         MDB_val key;
209         int rc;
210
211         key.mv_data = &e->e_id;
212         key.mv_size = sizeof(ID);
213
214         /* delete from database */
215         rc = mdb_del( tid, dbi, &key, NULL );
216
217         return rc;
218 }
219
220 static Entry * mdb_entry_alloc(
221         Operation *op,
222         int nattrs,
223         int nvals )
224 {
225         Entry *e = op->o_tmpalloc( sizeof(Entry) +
226                 nattrs * sizeof(Attribute) +
227                 nvals * sizeof(struct berval), op->o_tmpmemctx );
228         BER_BVZERO(&e->e_bv);
229         e->e_private = e;
230         if (nattrs) {
231                 e->e_attrs = (Attribute *)(e+1);
232                 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
233         } else {
234                 e->e_attrs = NULL;
235         }
236
237         return e;
238 }
239
240 int mdb_entry_return(
241         Operation *op,
242         Entry *e
243 )
244 {
245         if ( !e )
246                 return 0;
247         if ( e->e_private ) {
248                 if ( op->o_hdr ) {
249                         op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
250                         op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
251                         op->o_tmpfree( e, op->o_tmpmemctx );
252                 } else {
253                         ch_free( e->e_nname.bv_val );
254                         ch_free( e->e_name.bv_val );
255                         ch_free( e );
256                 }
257         } else {
258                 entry_free( e );
259         }
260         return 0;
261 }
262
263 int mdb_entry_release(
264         Operation *op,
265         Entry *e,
266         int rw )
267 {
268         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
269         struct mdb_op_info *moi = NULL;
270         int rc;
271  
272         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
273                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
274  
275         int release = 1;
276         if ( slapMode & SLAP_SERVER_MODE ) {
277                 OpExtra *oex;
278                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
279                         release = 0;
280                         if ( oex->oe_key == mdb ) {
281                                 mdb_entry_return( op, e );
282                                 moi = (mdb_op_info *)oex;
283                                 /* If it was setup by entry_get we should probably free it */
284                                 if ( moi->moi_flag & MOI_FREEIT ) {
285                                         moi->moi_ref--;
286                                         if ( moi->moi_ref < 1 ) {
287                                                 mdb_txn_reset( moi->moi_txn );
288                                                 moi->moi_ref = 0;
289                                                 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
290                                                 op->o_tmpfree( moi, op->o_tmpmemctx );
291                                         }
292                                 }
293                                 break;
294                         }
295                 }
296         }
297
298         if (release)
299                 mdb_entry_return( op, e );
300  
301         return 0;
302 }
303
304 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
305  */
306 int mdb_entry_get(
307         Operation *op,
308         struct berval *ndn,
309         ObjectClass *oc,
310         AttributeDescription *at,
311         int rw,
312         Entry **ent )
313 {
314         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
315         struct mdb_op_info *moi = NULL;
316         MDB_txn *txn = NULL;
317         Entry *e = NULL;
318         int     rc;
319         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
320
321         Debug( LDAP_DEBUG_ARGS,
322                 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
323         Debug( LDAP_DEBUG_ARGS,
324                 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
325                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
326
327         rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
328         if ( rc )
329                 return LDAP_OTHER;
330         txn = moi->moi_txn;
331
332         /* can we find entry */
333         rc = mdb_dn2entry( op, txn, NULL, ndn, &e, NULL, 0 );
334         switch( rc ) {
335         case MDB_NOTFOUND:
336         case 0:
337                 break;
338         default:
339                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
340         }
341         if (e == NULL) {
342                 Debug( LDAP_DEBUG_ACL,
343                         "=> mdb_entry_get: cannot find entry: \"%s\"\n",
344                                 ndn->bv_val, 0, 0 ); 
345                 rc = LDAP_NO_SUCH_OBJECT;
346                 goto return_results;
347         }
348         
349         Debug( LDAP_DEBUG_ACL,
350                 "=> mdb_entry_get: found entry: \"%s\"\n",
351                 ndn->bv_val, 0, 0 ); 
352
353         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
354                 Debug( LDAP_DEBUG_ACL,
355                         "<= mdb_entry_get: failed to find objectClass %s\n",
356                         oc->soc_cname.bv_val, 0, 0 ); 
357                 rc = LDAP_NO_SUCH_ATTRIBUTE;
358                 goto return_results;
359         }
360
361         /* NOTE: attr_find() or attrs_find()? */
362         if ( at && attr_find( e->e_attrs, at ) == NULL ) {
363                 Debug( LDAP_DEBUG_ACL,
364                         "<= mdb_entry_get: failed to find attribute %s\n",
365                         at->ad_cname.bv_val, 0, 0 ); 
366                 rc = LDAP_NO_SUCH_ATTRIBUTE;
367                 goto return_results;
368         }
369
370 return_results:
371         if( rc != LDAP_SUCCESS ) {
372                 /* free entry */
373                 mdb_entry_release( op, e, rw );
374         } else {
375                 *ent = e;
376         }
377
378         Debug( LDAP_DEBUG_TRACE,
379                 "mdb_entry_get: rc=%d\n",
380                 rc, 0, 0 ); 
381         return(rc);
382 }
383
384 static void
385 mdb_reader_free( void *key, void *data )
386 {
387         MDB_txn *txn = data;
388
389         if ( txn ) mdb_txn_abort( txn );
390 }
391
392 /* free up any keys used by the main thread */
393 void
394 mdb_reader_flush( MDB_env *env )
395 {
396         void *data;
397         void *ctx = ldap_pvt_thread_pool_context();
398
399         if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
400                 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
401                 mdb_reader_free( env, data );
402         }
403 }
404
405 extern MDB_txn *mdb_tool_txn;
406
407 int
408 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
409 {
410         int rc, renew = 0;
411         void *data;
412         void *ctx;
413         mdb_op_info *moi = NULL;
414         OpExtra *oex;
415
416         assert( op != NULL );
417
418         if ( !mdb || !moip ) return -1;
419
420         /* If no op was provided, try to find the ctx anyway... */
421         if ( op ) {
422                 ctx = op->o_threadctx;
423         } else {
424                 ctx = ldap_pvt_thread_pool_context();
425         }
426
427         if ( op ) {
428                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
429                         if ( oex->oe_key == mdb ) break;
430                 }
431                 moi = (mdb_op_info *)oex;
432         }
433
434         if ( !moi ) {
435                 moi = *moip;
436
437                 if ( !moi ) {
438                         if ( op ) {
439                                 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
440                         } else {
441                                 moi = ch_malloc(sizeof(mdb_op_info));
442                         }
443                         moi->moi_flag = MOI_FREEIT;
444                         *moip = moi;
445                 }
446                 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
447                 moi->moi_oe.oe_key = mdb;
448                 moi->moi_ref = 0;
449                 moi->moi_txn = NULL;
450         }
451
452         if ( !rdonly ) {
453                 /* This op started as a reader, but now wants to write. */
454                 if ( moi->moi_flag & MOI_READER ) {
455                         moi = *moip;
456                         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
457                 } else {
458                 /* This op is continuing an existing write txn */
459                         *moip = moi;
460                 }
461                 moi->moi_ref++;
462                 if ( !moi->moi_txn ) {
463                         if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
464                                 moi->moi_txn = mdb_tool_txn;
465                         } else {
466                                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &moi->moi_txn );
467                                 if (rc) {
468                                         Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
469                                                 mdb_strerror(rc), rc, 0 );
470                                 }
471                                 return rc;
472                         }
473                 }
474                 return 0;
475         }
476
477         /* OK, this is a reader */
478         if ( !moi->moi_txn ) {
479                 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
480                         moi->moi_txn = mdb_tool_txn;
481                         goto ok;
482                 }
483                 if ( !ctx ) {
484                         /* Shouldn't happen unless we're single-threaded */
485                         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
486                         if (rc) {
487                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
488                                         mdb_strerror(rc), rc, 0 );
489                         }
490                         return rc;
491                 }
492                 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
493                         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
494                         if (rc) {
495                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
496                                         mdb_strerror(rc), rc, 0 );
497                                 return rc;
498                         }
499                         data = moi->moi_txn;
500                         if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
501                                 data, mdb_reader_free, NULL, NULL ) ) ) {
502                                 mdb_txn_abort( moi->moi_txn );
503                                 moi->moi_txn = NULL;
504                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
505                                         rc, 0, 0 );
506                                 return rc;
507                         }
508                 } else {
509                         moi->moi_txn = data;
510                         renew = 1;
511                 }
512                 moi->moi_flag |= MOI_READER;
513         }
514 ok:
515         if ( moi->moi_ref < 1 ) {
516                 moi->moi_ref = 0;
517         }
518         if ( renew ) {
519                 rc = mdb_txn_renew( moi->moi_txn );
520                 assert(!rc);
521         }
522         moi->moi_ref++;
523         if ( *moip != moi )
524                 *moip = moi;
525
526         return 0;
527 }
528
529 /* Count up the sizes of the components of an entry */
530 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
531         Ecount *eh)
532 {
533         ber_len_t len;
534         int i, nat = 0, nval = 0, nnval = 0;
535         Attribute *a;
536
537         len = 4*sizeof(int);    /* nattrs, nvals, ocflags, offset */
538         for (a=e->e_attrs; a; a=a->a_next) {
539                 /* For AttributeDesc, we only store the attr index */
540                 nat++;
541                 if (a->a_desc->ad_index >= MDB_MAXADS) {
542                         Debug( LDAP_DEBUG_ANY, "mdb_entry_partsize: too many AttributeDescriptions used\n",
543                                 0, 0, 0 );
544                         return LDAP_OTHER;
545                 }
546                 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
547                         int rc = mdb_ad_get(mdb, txn, a->a_desc);
548                         if (rc)
549                                 return rc;
550                 }
551                 len += 2*sizeof(int);   /* AD index, numvals */
552                 nval += a->a_numvals + 1;       /* empty berval at end */
553                 for (i=0; i<a->a_numvals; i++) {
554                         len += a->a_vals[i].bv_len + 1 + sizeof(int);   /* len */
555                 }
556                 if (a->a_nvals != a->a_vals) {
557                         nval += a->a_numvals + 1;
558                         nnval++;
559                         for (i=0; i<a->a_numvals; i++) {
560                                 len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
561                         }
562                 }
563         }
564         /* padding */
565         len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
566         eh->len = len;
567         eh->nattrs = nat;
568         eh->nvals = nval;
569         eh->offset = nat + nval - nnval;
570         return 0;
571 }
572
573 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
574
575 /* Flatten an Entry into a buffer. The buffer starts with the count of the
576  * number of attributes in the entry, the total number of values in the
577  * entry, and the e_ocflags. It then contains a list of integers for each
578  * attribute. For each attribute the first integer gives the index of the
579  * matching AttributeDescription, followed by the number of values in the
580  * attribute. If the high bit of the attr index is set, the attribute's
581  * values are already sorted.
582  * If the high bit of numvals is set, the attribute also has normalized
583  * values present. (Note - a_numvals is an unsigned int, so this means
584  * it's possible to receive an attribute that we can't encode due to size
585  * overflow. In practice, this should not be an issue.) Then the length
586  * of each value is listed. If there are normalized values, their lengths
587  * come next. This continues for each attribute. After all of the lengths
588  * for the last attribute, the actual values are copied, with a NUL
589  * terminator after each value. The buffer is padded to the sizeof(ID).
590  * The entire buffer size is precomputed so that a single malloc can be
591  * performed.
592  */
593 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
594 {
595         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
596         ber_len_t len, i;
597         int rc;
598         Attribute *a;
599         unsigned char *ptr;
600         unsigned int *lp, l;
601
602         Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
603                 (long) e->e_id, e->e_dn, 0 );
604
605         /* make sure e->e_ocflags is set */
606         if (is_entry_referral(e))
607                 ;       /* empty */
608
609         lp = (unsigned int *)data->mv_data;
610         *lp++ = eh->nattrs;
611         *lp++ = eh->nvals;
612         *lp++ = (unsigned int)e->e_ocflags;
613         *lp++ = eh->offset;
614         ptr = (unsigned char *)(lp + eh->offset);
615
616         for (a=e->e_attrs; a; a=a->a_next) {
617                 if (!a->a_desc->ad_index)
618                         return LDAP_UNDEFINED_TYPE;
619                 l = mdb->mi_adxs[a->a_desc->ad_index];
620                 if (a->a_flags & SLAP_ATTR_SORTED_VALS)
621                         l |= HIGH_BIT;
622                 *lp++ = l;
623                 l = a->a_numvals;
624                 if (a->a_nvals != a->a_vals)
625                         l |= HIGH_BIT;
626                 *lp++ = l;
627                 if (a->a_vals) {
628                         for (i=0; a->a_vals[i].bv_val; i++);
629                         assert( i == a->a_numvals );
630                         for (i=0; i<a->a_numvals; i++) {
631                                 *lp++ = a->a_vals[i].bv_len;
632                                 memcpy(ptr, a->a_vals[i].bv_val,
633                                         a->a_vals[i].bv_len);
634                                 ptr += a->a_vals[i].bv_len;
635                                 *ptr++ = '\0';
636                         }
637                         if (a->a_nvals != a->a_vals) {
638                                 for (i=0; i<a->a_numvals; i++) {
639                                         *lp++ = a->a_nvals[i].bv_len;
640                                         memcpy(ptr, a->a_nvals[i].bv_val,
641                                                 a->a_nvals[i].bv_len);
642                                         ptr += a->a_nvals[i].bv_len;
643                                         *ptr++ = '\0';
644                                 }
645                         }
646                 }
647         }
648
649         Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
650                 (long) e->e_id, e->e_dn, 0 );
651
652         return 0;
653 }
654
655 /* Retrieve an Entry that was stored using entry_encode above.
656  *
657  * Note: everything is stored in a single contiguous block, so
658  * you can not free individual attributes or names from this
659  * structure. Attempting to do so will likely corrupt memory.
660  */
661
662 int mdb_entry_decode(Operation *op, MDB_txn *txn, MDB_val *data, Entry **e)
663 {
664         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
665         int i, j, nattrs, nvals;
666         int rc;
667         Attribute *a;
668         Entry *x;
669         const char *text;
670         AttributeDescription *ad;
671         unsigned int *lp = (unsigned int *)data->mv_data;
672         unsigned char *ptr;
673         BerVarray bptr;
674
675         Debug( LDAP_DEBUG_TRACE,
676                 "=> mdb_entry_decode:\n",
677                 0, 0, 0 );
678
679         nattrs = *lp++;
680         nvals = *lp++;
681         x = mdb_entry_alloc(op, nattrs, nvals);
682         x->e_ocflags = *lp++;
683         if (!nvals) {
684                 goto done;
685         }
686         a = x->e_attrs;
687         bptr = a->a_vals;
688         i = *lp++;
689         ptr = (unsigned char *)(lp + i);
690
691         for (;nattrs>0; nattrs--) {
692                 int have_nval = 0;
693                 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
694                 i = *lp++;
695                 if (i & HIGH_BIT) {
696                         i ^= HIGH_BIT;
697                         a->a_flags |= SLAP_ATTR_SORTED_VALS;
698                 }
699                 if (i > mdb->mi_numads) {
700                         rc = mdb_ad_read(mdb, txn);
701                         if (rc)
702                                 return rc;
703                         if (i > mdb->mi_numads) {
704                                 Debug( LDAP_DEBUG_ANY,
705                                         "mdb_entry_decode: attribute index %d not recognized\n",
706                                         i, 0, 0 );
707                                 return LDAP_OTHER;
708                         }
709                 }
710                 a->a_desc = mdb->mi_ads[i];
711                 a->a_numvals = *lp++;
712                 if (a->a_numvals & HIGH_BIT) {
713                         a->a_numvals ^= HIGH_BIT;
714                         have_nval = 1;
715                 }
716                 a->a_vals = bptr;
717                 for (i=0; i<a->a_numvals; i++) {
718                         bptr->bv_len = *lp++;;
719                         bptr->bv_val = (char *)ptr;
720                         ptr += bptr->bv_len+1;
721                         bptr++;
722                 }
723                 bptr->bv_val = NULL;
724                 bptr->bv_len = 0;
725                 bptr++;
726
727                 if (have_nval) {
728                         a->a_nvals = bptr;
729                         for (i=0; i<a->a_numvals; i++) {
730                                 bptr->bv_len = *lp++;
731                                 bptr->bv_val = (char *)ptr;
732                                 ptr += bptr->bv_len+1;
733                                 bptr++;
734                         }
735                         bptr->bv_val = NULL;
736                         bptr->bv_len = 0;
737                         bptr++;
738                 } else {
739                         a->a_nvals = a->a_vals;
740                 }
741                 /* FIXME: This is redundant once a sorted entry is saved into the DB */
742                 if (( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
743                         && !(a->a_flags & SLAP_ATTR_SORTED_VALS)) {
744                         rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
745                         if ( rc == LDAP_SUCCESS ) {
746                                 a->a_flags |= SLAP_ATTR_SORTED_VALS;
747                         } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
748                                 /* should never happen */
749                                 Debug( LDAP_DEBUG_ANY,
750                                         "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
751                                         a->a_desc->ad_cname.bv_val, j, 0 );
752                                 return rc;
753                         }
754                 }
755                 a->a_next = a+1;
756                 a = a->a_next;
757         }
758         a[-1].a_next = NULL;
759 done:
760
761         Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",
762                 0, 0, 0 );
763         *e = x;
764         return 0;
765 }