1 /* dn2id.c - routines to deal with the dn2id index */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
24 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
25 DB *db = bdb->bi_dn2id->bdi_db;
27 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
32 key.size = strlen( dn ) + 2;
33 key.data = ch_malloc( key.size );
34 ((char *)key.data)[0] = DN_BASE_PREFIX;
35 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
38 data.data = (char *) &id;
39 data.size = sizeof( id );
41 /* store it -- don't override */
42 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
44 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
45 db_strerror(rc), rc, 0 );
50 char *pdn = dn_parent( NULL, dn );
51 ((char *)(key.data))[0] = DN_ONE_PREFIX;
54 key.size = strlen( pdn ) + 2;
55 AC_MEMCPY( &((char*)key.data)[1],
58 rc = bdb_idl_insert_key( be, db, txn, &key, id );
61 Debug( LDAP_DEBUG_ANY,
62 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
72 char **subtree = dn_subtree( NULL, dn );
74 if( subtree != NULL ) {
76 ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
77 for( i=0; subtree[i] != NULL; i++ ) {
78 key.size = strlen( subtree[i] ) + 2;
79 AC_MEMCPY( &((char *)key.data)[1],
80 subtree[i], key.size - 1 );
82 rc = bdb_idl_insert_key( be, db, txn, &key, id );
85 Debug( LDAP_DEBUG_ANY,
86 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
92 charray_free( subtree );
98 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
111 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
112 DB *db = bdb->bi_dn2id->bdi_db;
114 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
118 key.size = strlen( dn ) + 2;
119 key.data = ch_malloc( key.size );
120 ((char *)key.data)[0] = DN_BASE_PREFIX;
121 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
123 /* store it -- don't override */
124 rc = db->del( db, txn, &key, 0 );
126 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
127 db_strerror(rc), rc, 0 );
132 char *pdn = dn_parent( NULL, dn );
133 ((char *)(key.data))[0] = DN_ONE_PREFIX;
136 key.size = strlen( pdn ) + 2;
137 AC_MEMCPY( &((char*)key.data)[1],
140 rc = bdb_idl_delete_key( be, db, txn, &key, id );
143 Debug( LDAP_DEBUG_ANY,
144 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
154 char **subtree = dn_subtree( NULL, dn );
156 if( subtree != NULL ) {
158 ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
159 for( i=0; subtree[i] != NULL; i++ ) {
160 key.size = strlen( subtree[i] ) + 2;
161 AC_MEMCPY( &((char *)key.data)[1],
162 subtree[i], key.size - 1 );
164 rc = bdb_idl_delete_key( be, db, txn, &key, id );
167 Debug( LDAP_DEBUG_ANY,
168 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
170 charray_free( subtree );
175 charray_free( subtree );
181 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
194 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
195 DB *db = bdb->bi_dn2id->bdi_db;
197 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
200 key.size = strlen( dn ) + 2;
201 key.data = ch_malloc( key.size );
202 ((char *)key.data)[0] = DN_BASE_PREFIX;
203 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
208 data.ulen = sizeof(ID);
209 data.flags = DB_DBT_USERMEM;
212 rc = db->get( db, txn, &key, &data, 0 );
215 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
216 db_strerror( rc ), rc, 0 );
218 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
236 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
237 DB *db = bdb->bi_dn2id->bdi_db;
241 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
244 key.size = strlen( dn ) + 2;
245 key.data = ch_malloc( key.size );
246 ((char *)key.data)[0] = DN_BASE_PREFIX;
251 data.ulen = sizeof(ID);
252 data.flags = DB_DBT_USERMEM;
257 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
262 rc = db->get( db, txn, &key, &data, 0 );
264 if( rc == DB_NOTFOUND ) {
265 char *pdn = dn_parent( be, dn );
269 if( pdn == NULL || *pdn == '\0' ) {
270 Debug( LDAP_DEBUG_TRACE,
271 "<= bdb_dn2id_matched: no match\n",
279 key.size = strlen( dn ) + 2;
281 } else if ( rc == 0 ) {
282 if( data.size != sizeof( ID ) ) {
283 Debug( LDAP_DEBUG_ANY,
284 "<= bdb_dn2id_matched: get size mismatch: "
285 "expected %ld, got %ld\n",
286 (long) sizeof(ID), (long) data.size, 0 );
291 *matchedDN = (char *) dn;
294 Debug( LDAP_DEBUG_TRACE,
295 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
296 *id, *matchedDN == NULL ? "entry" : "matched", dn );
300 Debug( LDAP_DEBUG_ANY,
301 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
302 db_strerror(rc), rc, 0 );
320 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
321 DB *db = bdb->bi_dn2id->bdi_db;
324 Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
328 key.size = strlen( dn ) + 2;
329 key.data = ch_malloc( key.size );
330 ((char *)key.data)[0] = DN_ONE_PREFIX;
331 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
333 /* we actually could do a empty get... */
336 data.ulen = sizeof(id);
337 data.flags = DB_DBT_USERMEM;
339 data.dlen = sizeof(id);
341 rc = db->get( db, txn, &key, &data, 0 );
343 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
345 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
346 db_strerror(rc) ), rc );