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

139 lines
3.3 KiB
C
Raw Normal View History

1998-08-09 08:43:13 +08:00
/* id.c - keep track of the next id to be given out */
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
#include <ac/socket.h>
#ifdef HAVE_SYS_PARAM_H
1998-08-09 08:43:13 +08:00
#include <sys/param.h>
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
#include "slap.h"
#include "back-ldbm.h"
ID
next_id( Backend *be )
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
char buf[MAXPATHLEN];
char buf2[20];
FILE *fp;
ID id;
sprintf( buf, "%s/NEXTID", li->li_directory );
pthread_mutex_lock( &li->li_nextid_mutex );
/* first time in here since startup - try to read the nexid */
if ( li->li_nextid == -1 ) {
if ( (fp = fopen( buf, "r" )) == NULL ) {
Debug( LDAP_DEBUG_ANY,
"next_id %lu: could not open \"%s\"\n",
1998-08-09 08:43:13 +08:00
li->li_nextid, buf, 0 );
li->li_nextid = 1;
} else {
if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
li->li_nextid = atol( buf2 );
} else {
Debug( LDAP_DEBUG_ANY,
"next_id %lu: could not fgets nextid from \"%s\"\n",
1998-08-09 08:43:13 +08:00
li->li_nextid, buf2, 0 );
li->li_nextid = 1;
}
fclose( fp );
}
}
li->li_nextid++;
if ( (fp = fopen( buf, "w" )) == NULL ) {
Debug( LDAP_DEBUG_ANY, "next_id %lu: could not open \"%s\"\n",
1998-08-09 08:43:13 +08:00
li->li_nextid, buf, 0 );
} else {
if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fprintf\n",
1998-08-09 08:43:13 +08:00
li->li_nextid, 0, 0 );
}
if( fclose( fp ) != 0 ) {
Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fclose\n",
1998-08-09 08:43:13 +08:00
li->li_nextid, 0, 0 );
}
}
id = li->li_nextid - 1;
pthread_mutex_unlock( &li->li_nextid_mutex );
return( id );
}
void
next_id_return( Backend *be, ID id )
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
char buf[MAXPATHLEN];
FILE *fp;
pthread_mutex_lock( &li->li_nextid_mutex );
if ( id != li->li_nextid - 1 ) {
pthread_mutex_unlock( &li->li_nextid_mutex );
return;
}
sprintf( buf, "%s/NEXTID", li->li_directory );
li->li_nextid--;
if ( (fp = fopen( buf, "w" )) == NULL ) {
Debug( LDAP_DEBUG_ANY,
"next_id_return of %lu: could not open \"%s\" next id %lu\n",
1998-08-09 08:43:13 +08:00
id, buf, li->li_nextid );
} else {
if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
Debug( LDAP_DEBUG_ANY,
"next_id_return of %lu: cannot fprintf \"%s\" next id %lu\n",
1998-08-09 08:43:13 +08:00
id, buf, li->li_nextid );
}
if( fclose( fp ) != 0 ) {
Debug( LDAP_DEBUG_ANY,
"next_id_return of %lu: cannot fclose \"%s\" next id %lu\n",
1998-08-09 08:43:13 +08:00
id, buf, li->li_nextid );
}
}
pthread_mutex_unlock( &li->li_nextid_mutex );
}
ID
next_id_get( Backend *be )
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
char buf[MAXPATHLEN];
char buf2[20];
FILE *fp;
ID id;
sprintf( buf, "%s/NEXTID", li->li_directory );
pthread_mutex_lock( &li->li_nextid_mutex );
/* first time in here since startup - try to read the nexid */
if ( li->li_nextid == -1 ) {
if ( (fp = fopen( buf, "r" )) == NULL ) {
Debug( LDAP_DEBUG_ANY,
"next_id %lu: could not open \"%s\"\n",
1998-08-09 08:43:13 +08:00
li->li_nextid, buf, 0 );
li->li_nextid = 1;
} else {
if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
li->li_nextid = atol( buf2 );
} else {
Debug( LDAP_DEBUG_ANY,
"next_id %lu: cannot fgets nextid from \"%s\"\n",
1998-08-09 08:43:13 +08:00
li->li_nextid, buf2, 0 );
li->li_nextid = 1;
}
fclose( fp );
}
}
id = li->li_nextid;
pthread_mutex_unlock( &li->li_nextid_mutex );
return( id );
}