X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=servers%2Fslapd%2Fback-ldbm%2Fnextid.c;h=c80a7015bfe10e11b907e61d1d6b6c0261c45a14;hb=324431062bff39a1bd3969bd37d4913526dd583f;hp=e29d86b6ca4e9cb9930d361ade9f27ab19390280;hpb=0f17fac37dbde13ab47898d07f6ebea710f79714;p=openldap diff --git a/servers/slapd/back-ldbm/nextid.c b/servers/slapd/back-ldbm/nextid.c index e29d86b6ca..c80a7015bf 100644 --- a/servers/slapd/back-ldbm/nextid.c +++ b/servers/slapd/back-ldbm/nextid.c @@ -1,184 +1,135 @@ -/* id.c - keep track of the next id to be given out */ +/* nextid.c - keep track of the next id to be given out */ +/* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . + * + * Copyright 1998-2006 The OpenLDAP Foundation. + * 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 + * . + */ #include "portable.h" #include +#include #include - -#ifdef HAVE_SYS_PARAM_H -#include -#endif +#include #include "slap.h" #include "back-ldbm.h" -static ID -next_id_read( Backend *be ) +static int +next_id_read( Backend *be, ID *idp ) { - struct ldbminfo *li = (struct ldbminfo *) be->be_private; - ID id; - char buf[20]; - char* file = li->li_nextid_file; - FILE* fp; - - if ( (fp = fopen( file, "r" )) == NULL ) { - Debug( LDAP_DEBUG_ANY, - "next_id_read: could not open \"%s\"\n", - file, 0, 0 ); - return NOID; - } - - if ( fgets( buf, sizeof(buf), fp ) == NULL ) { - Debug( LDAP_DEBUG_ANY, - "next_id_read: could not fgets nextid from \"%s\"\n", - file, 0, 0 ); - fclose( fp ); - return NOID; - } + Datum key, data; + DBCache *db; - id = atol( buf ); - fclose( fp ); + *idp = NOID; - if(id < 1) { - Debug( LDAP_DEBUG_ANY, - "next_id_read %lu: atol(%s) return non-positive integer\n", - id, buf, 0 ); - return NOID; - } + if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT )) + == NULL ) { + Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n", + 0, 0, 0 ); - return id; -} - -static int -next_id_write( Backend *be, ID id ) -{ - struct ldbminfo *li = (struct ldbminfo *) be->be_private; - char buf[20]; - char* file = li->li_nextid_file; - FILE* fp; - int rc; - - if ( (fp = fopen( file, "w" )) == NULL ) { - Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): could not open \"%s\"\n", - id, file, 0 ); - return -1; - } - - rc = 0; - - if ( fprintf( fp, "%ld\n", id ) == EOF ) { - Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): cannot fprintf\n", - id, 0, 0 ); - rc = -1; + return( -1 ); } - if( fclose( fp ) != 0 ) { - Debug( LDAP_DEBUG_ANY, "next_id_write %lu: cannot fclose\n", - id, 0, 0 ); - rc = -1; - } + ldbm_datum_init( key ); + key.dptr = (char *) idp; + key.dsize = sizeof(ID); - return rc; -} + data = ldbm_cache_fetch( db, key ); -int -next_id_save( Backend *be ) -{ - struct ldbminfo *li = (struct ldbminfo *) be->be_private; - ID id = next_id_get( be ); - int rc = next_id_write( be, id ); + if( data.dptr != NULL ) { + AC_MEMCPY( idp, data.dptr, sizeof( ID ) ); + ldbm_datum_free( db->dbc_db, data ); - if (rc == 0) { - li->li_nextid_wrote = id; + } else { + *idp = 1; } - return rc; + ldbm_cache_close( be, db ); + return( 0 ); } -ID -next_id( Backend *be ) +int +next_id_write( Backend *be, ID id ) { - struct ldbminfo *li = (struct ldbminfo *) be->be_private; - ID id; + Datum key, data; + DBCache *db; + ID noid = NOID; + int flags, rc = 0; - pthread_mutex_lock( &li->li_nextid_mutex ); + if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT )) + == NULL ) { + Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n", + 0, 0, 0 ); - /* first time in here since startup - try to read the nexid */ - if ( li->li_nextid == NOID ) { - li->li_nextid = next_id_read( be ); + return( -1 ); + } - if ( li->li_nextid == NOID ) { - li->li_nextid = 1; - } + ldbm_datum_init( key ); + ldbm_datum_init( data ); -#if SLAPD_NEXTID_CHUNK > 1 - li->li_nextid_wrote = li->li_nextid; -#endif - } + key.dptr = (char *) &noid; + key.dsize = sizeof(ID); - id = li->li_nextid++; + data.dptr = (char *) &id; + data.dsize = sizeof(ID); -#if SLAPD_NEXTID_CHUNK > 1 - if ( li->li_nextid > li->li_nextid_wrote ) { - li->li_nextid_wrote += SLAPD_NEXTID_CHUNK; - (void) next_id_write( be, li->li_nextid_wrote ); + flags = LDBM_REPLACE; + if ( ldbm_cache_store( db, key, data, flags ) != 0 ) { + rc = -1; } -#else - (void) next_id_write( be, li->li_nextid ); -#endif - pthread_mutex_unlock( &li->li_nextid_mutex ); - return( id ); + ldbm_cache_close( be, db ); + return( rc ); } -void -next_id_return( Backend *be, ID id ) +int +next_id_get( Backend *be, ID *idp ) { -#ifdef SLAPD_NEXTID_RETURN struct ldbminfo *li = (struct ldbminfo *) be->be_private; + int rc = 0; - pthread_mutex_lock( &li->li_nextid_mutex ); + *idp = NOID; - if ( id != li->li_nextid - 1 ) { - pthread_mutex_unlock( &li->li_nextid_mutex ); - return; + if ( li->li_nextid == NOID ) { + if ( ( rc = next_id_read( be, idp ) ) ) { + return( rc ); + } + li->li_nextid = *idp; } - li->li_nextid--; - -#if !( SLAPD_NEXTID_CHUCK > 1 ) - (void) next_id_write( be, li->li_nextid ); -#endif + *idp = li->li_nextid; - pthread_mutex_unlock( &li->li_nextid_mutex ); -#endif + return( rc ); } -ID -next_id_get( Backend *be ) +int +next_id( Backend *be, ID *idp ) { struct ldbminfo *li = (struct ldbminfo *) be->be_private; - ID id; + int rc = 0; - pthread_mutex_lock( &li->li_nextid_mutex ); - - /* first time in here since startup - try to read the nexid */ if ( li->li_nextid == NOID ) { - li->li_nextid = next_id_read( be ); - - if ( li->li_nextid == NOID ) { - li->li_nextid = 1; + if ( ( rc = next_id_read( be, idp ) ) ) { + return( rc ); } - -#if SLAPD_NEXTID_CHUNK > 1 - li->li_nextid_wrote = li->li_nextid; -#endif + li->li_nextid = *idp; } - id = li->li_nextid; - - pthread_mutex_unlock( &li->li_nextid_mutex ); + *idp = li->li_nextid++; + if ( next_id_write( be, li->li_nextid ) ) { + rc = -1; + } - return( id ); + return( rc ); }