Add dn2entry.c

This commit is contained in:
Kurt Zeilenga 2000-09-24 22:48:13 +00:00
parent 64c521085b
commit 3874e8571d
10 changed files with 308 additions and 164 deletions

View file

@ -11,6 +11,7 @@
#include <ac/string.h>
#include "back-bdb.h"
#include "external.h"
int
bdb_add(

View file

@ -143,6 +143,10 @@ SOURCE=.\delete.c
# End Source File
# Begin Source File
SOURCE=.\dn2entry.c
# End Source File
# Begin Source File
SOURCE=.\dn2id.c
# End Source File
# Begin Source File

View file

@ -11,6 +11,7 @@
#include <ac/string.h>
#include "back-bdb.h"
#include "external.h"
int
bdb_compare(

View file

@ -11,6 +11,7 @@
#include <ac/string.h>
#include "back-bdb.h"
#include "external.h"
int
bdb_delete(

View file

@ -0,0 +1,53 @@
/* dn2entry.c - routines to deal with the dn2id / id2entry glue */
/* $OpenLDAP$ */
/*
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "portable.h"
#include <stdio.h>
#include <ac/string.h>
#include "back-bdb.h"
/*
* dn2entry - look up dn in the cache/indexes and return the corresponding
* entry.
*/
int
bdb_dn2entry(
Backend *be,
DB_TXN *tid,
const char *dn,
Entry **e,
Entry **matched,
int flags )
{
int rc;
ID id;
char *matchedDN = NULL;
Debug(LDAP_DEBUG_TRACE, "bdb_dn2entry: dn: \"%s\"\n",
dn, 0, 0 );
if( matched != NULL ) {
rc = bdb_dn2id_matched( be, tid, dn, &id, &matchedDN );
} else {
rc = bdb_dn2id( be, tid, dn, &id );
}
if( rc != 0 ) {
return rc;
}
if( matchedDN == NULL ) {
rc = bdb_id2entry( be, tid, id, e );
} else {
rc = bdb_id2entry( be, tid, id, matched );
}
return rc;
}

View file

@ -95,8 +95,7 @@ bdb_dn2id_delete(
Backend *be,
DB_TXN *txn,
const char *dn,
ID id
)
ID id )
{
int rc;
DBT key;
@ -104,7 +103,6 @@ bdb_dn2id_delete(
DB *db = bdb->bi_dn2id->bdi_db;
Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", %ld )\n", dn, id, 0 );
assert( id != NOID );
DBTzero( &key );
key.size = strlen( dn ) + 2;
@ -164,12 +162,116 @@ done:
return rc;
}
int
bdb_dn2id(
Backend *be,
DB_TXN *txn,
const char *dn,
ID *id )
{
int rc;
DBT key, data;
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_dn2id->bdi_db;
Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
DBTzero( &key );
key.size = strlen( dn ) + 2;
key.data = ch_malloc( key.size );
((char *)key.data)[0] = DN_BASE_PREFIX;
AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
/* store the ID */
DBTzero( &data );
data.data = id;
data.ulen = sizeof(ID);
data.flags = DB_DBT_USERMEM;
/* fetch it */
rc = db->get( db, txn, &key, &data, 0 );
Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: id=%ld: %s (%d)\n",
id, db_strerror( rc ), rc );
ch_free( key.data );
return rc;
}
int
bdb_dn2id_matched(
Backend *be,
DB_TXN *txn,
const char *in,
ID *id,
char **matchedDN )
{
int rc;
DBT key, data;
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_dn2id->bdi_db;
const char *dn = in;
char *tmp = NULL;
Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
DBTzero( &key );
key.size = strlen( dn ) + 2;
key.data = ch_malloc( key.size );
((char *)key.data)[0] = DN_BASE_PREFIX;
/* store the ID */
DBTzero( &data );
data.data = id;
data.ulen = sizeof(ID);
data.flags = DB_DBT_USERMEM;
*matchedDN = NULL;
while(1) {
AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
/* fetch it */
rc = db->get( db, txn, &key, &data, 0 );
if( rc == DB_NOTFOUND ) {
char *pdn = dn_parent( be, dn );
ch_free( tmp );
tmp = NULL;
if( pdn == NULL || *pdn == '\0' ) {
ch_free( pdn );
break;
}
dn = pdn;
tmp = pdn;
key.size = strlen( dn ) + 2;
} else if ( rc == 0 ) {
if( in != dn ) {
*matchedDN = (char *) dn;
}
Debug( LDAP_DEBUG_TRACE,
"<= bdb_dn2id_matched: id=%ld: %s\n",
id, dn, 0 );
break;
} else {
ch_free( tmp );
break;
}
}
ch_free( key.data );
return rc;
}
int
bdb_dn2id_children(
Backend *be,
DB_TXN *txn,
const char *dn
)
const char *dn )
{
int rc;
DBT key, data;
@ -198,155 +300,8 @@ bdb_dn2id_children(
Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
dn,
rc == 0 ? "yes" : ( rc == DB_NOTFOUND ? "no" :
rc == 0 ? "yes" : ( rc == DB_NOTFOUND ? "no" :
db_strerror(rc) ), rc );
return rc;
}
#if 0
ID
dn2id(
Backend *be,
const char *dn
)
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
DBCache *db;
ID id;
Datum key, data;
Debug( LDAP_DEBUG_TRACE, "=> dn2id( \"%s\" )\n", dn, 0, 0 );
/* first check the cache */
if ( (id = cache_find_entry_dn2id( be, &li->li_cache, dn )) != NOID ) {
Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld (in cache)\n", id,
0, 0 );
return( id );
}
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
Debug( LDAP_DEBUG_ANY, "<= dn2id could not open dn2id%s\n",
LDBM_SUFFIX, 0, 0 );
return( NOID );
}
ldbm_datum_init( key );
key.dsize = strlen( dn ) + 2;
key.dptr = ch_malloc( key.dsize );
sprintf( key.dptr, "%c%s", DN_BASE_PREFIX, dn );
data = ldbm_cache_fetch( db, key );
ldbm_cache_close( be, db );
free( key.dptr );
if ( data.dptr == NULL ) {
Debug( LDAP_DEBUG_TRACE, "<= dn2id NOID\n", 0, 0, 0 );
return( NOID );
}
AC_MEMCPY( (char *) &id, data.dptr, sizeof(ID) );
assert( id != NOID );
ldbm_datum_free( db->dbc_db, data );
Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld\n", id, 0, 0 );
return( id );
}
ID_BLOCK *
dn2idl(
Backend *be,
const char *dn,
int prefix
)
{
DBCache *db;
Datum key;
ID_BLOCK *idl;
Debug( LDAP_DEBUG_TRACE, "=> dn2idl( \"%c%s\" )\n", prefix, dn, 0 );
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
Debug( LDAP_DEBUG_ANY, "<= dn2idl could not open dn2id%s\n",
LDBM_SUFFIX, 0, 0 );
return NULL;
}
ldbm_datum_init( key );
key.dsize = strlen( dn ) + 2;
key.dptr = ch_malloc( key.dsize );
sprintf( key.dptr, "%c%s", prefix, dn );
idl = idl_fetch( be, db, key );
ldbm_cache_close( be, db );
free( key.dptr );
return( idl );
}
/*
* dn2entry - look up dn in the cache/indexes and return the corresponding
* entry.
*/
Entry *
dn2entry_rw(
Backend *be,
const char *dn,
Entry **matched,
int rw
)
{
ID id;
Entry *e = NULL;
char *pdn;
Debug(LDAP_DEBUG_TRACE, "dn2entry_%s: dn: \"%s\"\n",
rw ? "w" : "r", dn, 0);
if( matched != NULL ) {
/* caller cares about match */
*matched = NULL;
}
if ( (id = dn2id( be, dn )) != NOID &&
(e = id2entry_rw( be, id, rw )) != NULL )
{
return( e );
}
if ( id != NOID ) {
Debug(LDAP_DEBUG_ANY,
"dn2entry_%s: no entry for valid id (%ld), dn \"%s\"\n",
rw ? "w" : "r", id, dn);
/* must have been deleted from underneath us */
/* treat as if NOID was found */
}
/* caller doesn't care about match */
if( matched == NULL ) return NULL;
/* entry does not exist - see how much of the dn does exist */
/* dn_parent checks returns NULL if dn is suffix */
if ( (pdn = dn_parent( be, dn )) != NULL ) {
/* get entry with reader lock */
if ( (e = dn2entry_r( be, pdn, matched )) != NULL ) {
*matched = e;
}
free( pdn );
}
return NULL;
}
#endif

View file

@ -11,6 +11,46 @@ LDAP_BEGIN_DECL
extern int bdb_initialize LDAP_P(( BackendInfo *bi ));
extern int bdb_add LDAP_P(( BackendDB *bd,
Connection *conn, Operation *op, Entry *e ));
extern int bdb_bind LDAP_P(( BackendDB *bd,
Connection *conn, Operation *op,
const char *dn, const char *ndn, int method,
struct berval *cred, char** edn ));
extern int bdb_compare LDAP_P(( BackendDB *bd,
Connection *conn, Operation *op,
const char *dn, const char *ndn,
AttributeAssertion *ava ));
extern int bdb_delete LDAP_P(( BackendDB *bd,
Connection *conn, Operation *op,
const char *dn, const char *ndn ));
extern int bdb_abandon LDAP_P(( BackendDB *bd,
Connection *conn, Operation *op, ber_int_t msgid ));
extern int bdb_modify LDAP_P(( BackendDB *bd,
Connection *conn, Operation *op,
const char *dn, const char *ndn, Modifications *ml ));
extern int bdb_modrdn LDAP_P(( BackendDB *bd,
Connection *conn, Operation *op,
const char *dn, const char *ndn,
const char* newrdn, int deleteoldrdn,
const char *newSuperior ));
extern int bdb_search LDAP_P(( BackendDB *bd,
Connection *conn, Operation *op,
const char *base, const char *nbase,
int scope, int deref, int sizelimit, int timelimit,
Filter *filter, const char *filterstr,
char **attrs, int attrsonly ));
extern int bdb_unbind LDAP_P(( BackendDB *bd,
Connection *conn, Operation *op ));
LDAP_END_DECL
#endif /* _BDB_EXTERNAL_H */

View file

@ -18,6 +18,7 @@ int bdb_id2entry_add(
Entry *e )
{
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_id2entry->bdi_db;
DBT key, data;
struct berval *bv;
int rc;
@ -34,9 +35,71 @@ int bdb_id2entry_add(
DBTzero( &data );
bv2DBT( bv, &data );
rc = bdb->bi_id2entry->bdi_db->put( bdb->bi_id2entry->bdi_db,
tid, &key, &data, DB_NOOVERWRITE );
rc = db->put( db, tid, &key, &data, DB_NOOVERWRITE );
ber_bvfree( bv );
return rc;
}
int bdb_id2entry(
Backend *be,
DB_TXN *tid,
ID id,
Entry **e )
{
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_id2entry->bdi_db;
DBT key, data;
struct berval bv;
int rc;
DBTzero( &key );
key.data = (char *) &id;
key.size = sizeof(ID);
DBTzero( &data );
data.flags = DB_DBT_MALLOC;
/* fetch it */
rc = db->get( db, tid, &key, &data, 0 );
if( rc != 0 ) {
return rc;
}
DBT2bv( &data, &bv );
rc = entry_decode( &bv, e );
ch_free( data.data );
return rc;
}
int bdb_id2entry_delete(
Backend *be,
DB_TXN *tid,
ID id )
{
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_id2entry->bdi_db;
DBT key;
struct berval *bv;
int rc;
DBTzero( &key );
key.data = (char *) &id;
key.size = sizeof(ID);
rc = db->del( db, tid, &key, 0 );
ber_bvfree( bv );
return rc;
}
int bdb_entry_return(
Backend *be,
Entry *e )
{
entry_free( e );
return 0;
}

View file

@ -11,6 +11,7 @@
#include <ac/string.h>
#include "back-bdb.h"
#include "external.h"
static struct bdbi_database {
char *file;
@ -273,15 +274,15 @@ bdb_initialize(
bi->bi_db_close = bdb_db_close;
bi->bi_db_destroy = bdb_db_destroy;
bi->bi_op_add = bdb_add;
bi->bi_op_compare = bdb_compare;
bi->bi_op_delete = bdb_delete;
#if 0
bi->bi_op_bind = bi_back_bind;
bi->bi_op_unbind = bi_back_unbind;
bi->bi_op_search = bi_back_search;
bi->bi_op_compare = bi_back_compare;
bi->bi_op_modify = bi_back_modify;
bi->bi_op_modrdn = bi_back_modrdn;
bi->bi_op_add = bi_back_add;
bi->bi_op_delete = bi_back_delete;
bi->bi_op_abandon = bi_back_abandon;
bi->bi_extended = bi_back_extended;

View file

@ -25,9 +25,34 @@ Entry *bdb_deref_internal_r LDAP_P((
#define deref_dn_r( be, dn, err, matched, text ) \
bdb_deref_internal_r( be, NULL, dn, err, matched, text)
/*
* dn2entry.c
*/
int bdb_dn2entry LDAP_P(( Backend *be, DB_TXN *tid,
const char *dn, Entry **e, Entry **matched, int flags ));
#define dn2entry_r(be, tid, dn, p, m) \
bdb_dn2entry((be), (tid), (dn), (p), (m), 0 )
#define dn2entry_w(be, tid, dn, p, m) \
bdb_dn2entry((be), (tid), (dn), (p), (m), DB_RMW )
/*
* dn2id.c
*/
int bdb_dn2id(
BackendDB *be,
DB_TXN *tid,
const char *dn,
ID *id );
int bdb_dn2id_matched(
BackendDB *be,
DB_TXN *tid,
const char *dn,
ID *id,
char **matchedDN );
int bdb_dn2id_add(
BackendDB *be,
DB_TXN *tid,
@ -45,12 +70,6 @@ int bdb_dn2id_children(
DB_TXN *tid,
const char *dn );
int bdb_dn2entry_rw LDAP_P(( Backend *be, DB_TXN *tid,
const char *dn, Entry **e, Entry **matched, int rw ));
#define dn2entry_r(be, tid, dn, p, m) bdb_dn2entry_rw((be), (tid), (dn), (p), (m), 0)
#define dn2entry_w(be, tid, dn, p, m) bdb_dn2entry_rw((be), (tid), (dn), (p), (m), 1)
/*
* entry.c
*/
@ -74,6 +93,12 @@ int bdb_id2entry_delete(
DB_TXN *tid,
ID id );
int bdb_id2entry(
Backend *be,
DB_TXN *tid,
ID id,
Entry **e );
/*
* idl.c
*/