]> git.sur5r.net Git - openldap/blob - servers/slapd/back-wt/dn2entry.c
Happy New Year
[openldap] / servers / slapd / back-wt / dn2entry.c
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2002-2018 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 /* ACKNOWLEDGEMENTS:
17  * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
18  * based on back-bdb for inclusion in OpenLDAP Software.
19  * WiredTiger is a product of MongoDB Inc.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26 #include "back-wt.h"
27 #include "config.h"
28
29 /*
30  * dn2entry - look up dn in the db and return the corresponding entry.
31  * No longer return closest ancestor, see wt_dn2pentry().
32  */
33 int wt_dn2entry( BackendDB *be,
34                                  wt_ctx *wc,
35                                  struct berval *ndn,
36                                  Entry **ep ){
37         uint64_t id;
38         WT_CURSOR *cursor = NULL;
39         WT_ITEM item;
40         EntryHeader eh;
41         int rc;
42         int eoff;
43         Entry *e = NULL;
44         WT_SESSION *session = wc->session;
45
46         if( ndn->bv_len == 0 ){
47                 /* parent of root dn */
48                 return WT_NOTFOUND;
49         }
50
51         rc = session->open_cursor(session,
52                                                           WT_INDEX_DN"(id, entry)",
53                                                           NULL, NULL, &cursor);
54         if ( rc ) {
55                 Debug( LDAP_DEBUG_ANY,
56                            LDAP_XSTRING(wt_dn2entry)
57                            ": open_cursor failed: %s (%d)\n",
58                            wiredtiger_strerror(rc), rc, 0 );
59                 goto done;
60         }
61
62         cursor->set_key(cursor, ndn->bv_val);
63         rc = cursor->search(cursor);
64         switch( rc ){
65         case 0:
66                 break;
67         case WT_NOTFOUND:
68                 goto done;
69         default:
70                 Debug( LDAP_DEBUG_ANY,
71                            LDAP_XSTRING(wt_dn2entry)
72                            ": search failed: %s (%d)\n",
73                            wiredtiger_strerror(rc), rc, 0 );
74                 goto done;
75         }
76         cursor->get_value(cursor, &id, &item);
77         rc = wt_entry_header( &item,  &eh );
78
79         eoff = eh.data - (char *)item.data;
80         eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size;
81         eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
82         memset(eh.bv.bv_val, 0xff, eh.bv.bv_len);
83         eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
84         memcpy(eh.data, item.data, item.size);
85         eh.data += eoff;
86         rc = entry_decode( &eh, &e );
87         if ( rc ) {
88                 Debug( LDAP_DEBUG_ANY,
89                            LDAP_XSTRING(wt_dn2entry)
90                            ": entry decode error: %s (%d)\n",
91                            rc, 0, 0 );
92                 goto done;
93         }
94
95         e->e_id = id;
96         *ep = e;
97
98 done:
99         if(cursor){
100                 cursor->close(cursor);
101         }
102         return rc;
103 }
104
105 /* dn2pentry - return parent entry */
106 int wt_dn2pentry( BackendDB *be,
107                                   wt_ctx *wc,
108                                   struct berval *ndn,
109                                   Entry **ep ){
110         Entry *e = NULL;
111         struct berval pdn;
112         int rc;
113
114         if (be_issuffix( be, ndn )) {
115                 *ep = NULL;
116                 return WT_NOTFOUND;
117         }
118
119         dnParent( ndn, &pdn );
120         rc = wt_dn2entry(be, wc, &pdn, &e);
121         *ep = e;
122         return rc;
123 }
124
125 /*
126  * Local variables:
127  * indent-tabs-mode: t
128  * tab-width: 4
129  * c-basic-offset: 4
130  * End:
131  */