1999-08-18 03:00:59 +08:00
|
|
|
/* nextid.c - keep track of the next id to be given out */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1999-08-07 07:07:46 +08:00
|
|
|
/*
|
2000-05-13 10:47:56 +08:00
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
1999-08-07 07:07:46 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1999-08-18 05:21:43 +08:00
|
|
|
#include <ac/string.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/socket.h>
|
2001-12-18 10:25:21 +08:00
|
|
|
#include <ac/param.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include "slap.h"
|
|
|
|
#include "back-ldbm.h"
|
|
|
|
|
2001-07-20 17:50:28 +08:00
|
|
|
static int
|
|
|
|
next_id_read( Backend *be, ID *idp )
|
1999-01-22 09:40:39 +08:00
|
|
|
{
|
1999-08-18 03:00:59 +08:00
|
|
|
Datum key, data;
|
|
|
|
DBCache *db;
|
|
|
|
|
2001-07-20 17:50:28 +08:00
|
|
|
*idp = NOID;
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
|
|
|
|
== NULL ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
|
|
|
LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
|
|
|
|
"next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX ));
|
|
|
|
#else
|
1999-08-18 03:00:59 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
|
|
|
|
0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
2001-07-20 17:50:28 +08:00
|
|
|
return( -1 );
|
1999-01-22 09:40:39 +08:00
|
|
|
}
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
ldbm_datum_init( key );
|
2001-07-20 17:50:28 +08:00
|
|
|
key.dptr = (char *) idp;
|
1999-08-18 03:00:59 +08:00
|
|
|
key.dsize = sizeof(ID);
|
|
|
|
|
|
|
|
data = ldbm_cache_fetch( db, key );
|
1999-01-22 09:40:39 +08:00
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
if( data.dptr != NULL ) {
|
2001-07-20 17:50:28 +08:00
|
|
|
AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
|
1999-08-18 03:00:59 +08:00
|
|
|
ldbm_datum_free( db->dbc_db, data );
|
1999-01-22 09:40:39 +08:00
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
} else {
|
2001-07-20 17:50:28 +08:00
|
|
|
*idp = 1;
|
1999-01-22 09:40:39 +08:00
|
|
|
}
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
ldbm_cache_close( be, db );
|
2001-07-20 17:50:28 +08:00
|
|
|
return( 0 );
|
1999-01-22 09:40:39 +08:00
|
|
|
}
|
|
|
|
|
2001-07-20 17:50:28 +08:00
|
|
|
int
|
1999-08-18 03:00:59 +08:00
|
|
|
next_id_write( Backend *be, ID id )
|
1999-01-22 09:40:39 +08:00
|
|
|
{
|
1999-08-18 03:00:59 +08:00
|
|
|
Datum key, data;
|
|
|
|
DBCache *db;
|
|
|
|
ID noid = NOID;
|
2001-07-20 17:50:28 +08:00
|
|
|
int flags, rc = 0;
|
1999-08-18 03:00:59 +08:00
|
|
|
|
|
|
|
if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
|
|
|
|
== NULL ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
|
|
|
LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
|
|
|
|
"next_id_write: Could not open/create nextid%s\n", LDBM_SUFFIX ));
|
|
|
|
#else
|
1999-08-18 03:00:59 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
|
|
|
|
0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
2001-07-20 17:50:28 +08:00
|
|
|
return( -1 );
|
1999-01-22 09:40:39 +08:00
|
|
|
}
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
ldbm_datum_init( key );
|
|
|
|
ldbm_datum_init( data );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
key.dptr = (char *) &noid;
|
|
|
|
key.dsize = sizeof(ID);
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
data.dptr = (char *) &id;
|
|
|
|
data.dsize = sizeof(ID);
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
flags = LDBM_REPLACE;
|
|
|
|
if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
|
2001-07-20 17:50:28 +08:00
|
|
|
rc = -1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
ldbm_cache_close( be, db );
|
2001-07-20 17:50:28 +08:00
|
|
|
return( rc );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2001-07-20 17:50:28 +08:00
|
|
|
int
|
|
|
|
next_id_get( Backend *be, ID *idp )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
2001-07-20 17:50:28 +08:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
*idp = NOID;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
if ( li->li_nextid == NOID ) {
|
2001-07-20 17:50:28 +08:00
|
|
|
if ( ( rc = next_id_read( be, idp ) ) ) {
|
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
li->li_nextid = *idp;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2001-07-20 17:50:28 +08:00
|
|
|
*idp = li->li_nextid;
|
1999-01-22 09:40:39 +08:00
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
|
2001-07-20 17:50:28 +08:00
|
|
|
return( rc );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2001-07-20 17:50:28 +08:00
|
|
|
int
|
|
|
|
next_id( Backend *be, ID *idp )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
2001-07-20 17:50:28 +08:00
|
|
|
int rc = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1999-01-22 09:40:39 +08:00
|
|
|
if ( li->li_nextid == NOID ) {
|
2001-07-20 17:50:28 +08:00
|
|
|
if ( ( rc = next_id_read( be, idp ) ) ) {
|
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
li->li_nextid = *idp;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-11-26 07:45:57 +08:00
|
|
|
|
2001-07-20 17:50:28 +08:00
|
|
|
*idp = li->li_nextid++;
|
|
|
|
if ( next_id_write( be, li->li_nextid ) ) {
|
|
|
|
rc = -1;
|
1999-04-30 06:26:58 +08:00
|
|
|
}
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
|
2001-07-20 17:50:28 +08:00
|
|
|
return( rc );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|