openldap/servers/slapd/back-ldbm/nextid.c

146 lines
2.8 KiB
C
Raw Normal View History

/* nextid.c - keep track of the next id to be given out */
/* $OpenLDAP$ */
2003-11-28 16:08:20 -05:00
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
2004-01-01 14:15:16 -05:00
* Copyright 1998-2004 The OpenLDAP Foundation.
2003-11-28 16:08:20 -05:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
1999-08-06 19:07:46 -04:00
*/
1998-08-08 20:43:13 -04:00
1998-10-24 21:41:42 -04:00
#include "portable.h"
1998-08-08 20:43:13 -04:00
#include <stdio.h>
1998-10-24 21:41:42 -04:00
1999-08-17 17:21:43 -04:00
#include <ac/string.h>
1998-10-24 21:41:42 -04:00
#include <ac/socket.h>
#include <ac/param.h>
1998-10-24 21:41:42 -04:00
1998-08-08 20:43:13 -04:00
#include "slap.h"
#include "back-ldbm.h"
static int
next_id_read( Backend *be, ID *idp )
{
Datum key, data;
DBCache *db;
*idp = NOID;
if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
2001-01-17 12:01:19 -05:00
#ifdef NEW_LOGGING
LDAP_LOG( BACK_LDBM, CRIT,
"next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
2001-01-17 12:01:19 -05:00
#else
Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
0, 0, 0 );
2001-01-17 12:01:19 -05:00
#endif
return( -1 );
}
ldbm_datum_init( key );
key.dptr = (char *) idp;
key.dsize = sizeof(ID);
data = ldbm_cache_fetch( db, key );
if( data.dptr != NULL ) {
AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
ldbm_datum_free( db->dbc_db, data );
} else {
*idp = 1;
}
ldbm_cache_close( be, db );
return( 0 );
}
int
next_id_write( Backend *be, ID id )
{
Datum key, data;
DBCache *db;
ID noid = NOID;
int flags, rc = 0;
if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
2001-01-17 12:01:19 -05:00
#ifdef NEW_LOGGING
LDAP_LOG( BACK_LDBM, CRIT,
"next_id_write: Could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
2001-01-17 12:01:19 -05:00
#else
Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
0, 0, 0 );
2001-01-17 12:01:19 -05:00
#endif
return( -1 );
}
ldbm_datum_init( key );
ldbm_datum_init( data );
1998-08-08 20:43:13 -04:00
key.dptr = (char *) &noid;
key.dsize = sizeof(ID);
1998-11-25 18:45:57 -05:00
data.dptr = (char *) &id;
data.dsize = sizeof(ID);
1998-11-25 18:45:57 -05:00
flags = LDBM_REPLACE;
if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
rc = -1;
1998-08-08 20:43:13 -04:00
}
ldbm_cache_close( be, db );
return( rc );
1998-08-08 20:43:13 -04:00
}
int
next_id_get( Backend *be, ID *idp )
1998-08-08 20:43:13 -04:00
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
int rc = 0;
*idp = NOID;
1998-08-08 20:43:13 -04:00
if ( li->li_nextid == NOID ) {
if ( ( rc = next_id_read( be, idp ) ) ) {
return( rc );
}
li->li_nextid = *idp;
1998-08-08 20:43:13 -04:00
}
*idp = li->li_nextid;
return( rc );
1998-08-08 20:43:13 -04:00
}
int
next_id( Backend *be, ID *idp )
1998-08-08 20:43:13 -04:00
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
int rc = 0;
1998-08-08 20:43:13 -04:00
if ( li->li_nextid == NOID ) {
if ( ( rc = next_id_read( be, idp ) ) ) {
return( rc );
}
li->li_nextid = *idp;
1998-08-08 20:43:13 -04:00
}
1998-11-25 18:45:57 -05:00
*idp = li->li_nextid++;
if ( next_id_write( be, li->li_nextid ) ) {
rc = -1;
}
return( rc );
1998-08-08 20:43:13 -04:00
}