1 /* id.c - keep track of the next id to be given out */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
13 #ifdef HAVE_SYS_PARAM_H
14 #include <sys/param.h>
18 #include "back-ldbm.h"
20 static ID next_id_read( Backend *be );
21 static int next_id_write( Backend *be, ID id );
22 static ID next_id_get_save( Backend *be, int do_save );
26 next_id_read( Backend *be )
28 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
31 char* file = li->li_nextid_file;
34 if ( ldbm_ignore_nextid_file )
37 if ( (fp = fopen( file, "r" )) == NULL ) {
38 Debug( LDAP_DEBUG_ANY,
39 "next_id_read: could not open \"%s\"\n",
44 if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
45 Debug( LDAP_DEBUG_ANY,
46 "next_id_read: could not fgets nextid from \"%s\"\n",
56 Debug( LDAP_DEBUG_ANY,
57 "next_id_read %ld: atol(%s) return non-positive integer\n",
66 next_id_write( Backend *be, ID id )
68 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
69 char* file = li->li_nextid_file;
73 if ( ldbm_ignore_nextid_file )
76 if ( (fp = fopen( file, "w" )) == NULL ) {
77 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): could not open \"%s\"\n",
84 if ( fprintf( fp, "%ld\n", id ) == EOF ) {
85 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): cannot fprintf\n",
90 if( fclose( fp ) != 0 ) {
91 Debug( LDAP_DEBUG_ANY, "next_id_write %ld: cannot fclose\n",
100 next_id_save( Backend *be )
102 return( next_id_get_save( be, 1 ) == NOID ? -1 : 0 );
106 next_id( Backend *be )
108 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
111 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
113 /* first time in here since startup - try to read the nexid */
114 if ( li->li_nextid == NOID ) {
115 li->li_nextid = next_id_read( be );
117 if ( li->li_nextid == NOID ) {
121 #if SLAPD_NEXTID_CHUNK > 1
122 li->li_nextid_wrote = li->li_nextid;
126 id = li->li_nextid++;
128 #if SLAPD_NEXTID_CHUNK > 1
129 if ( li->li_nextid > li->li_nextid_wrote ) {
130 li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
131 (void) next_id_write( be, li->li_nextid_wrote );
134 (void) next_id_write( be, li->li_nextid );
137 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
142 next_id_return( Backend *be, ID id )
144 #ifdef SLAPD_NEXTID_RETURN
145 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
147 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
149 if ( id != li->li_nextid - 1 ) {
150 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
156 #if !( SLAPD_NEXTID_CHUNK > 1 )
157 (void) next_id_write( be, li->li_nextid );
160 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
165 next_id_get( Backend *be )
167 return next_id_get_save( be, 0 );
171 next_id_get_save( Backend *be, int do_save )
173 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
176 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
178 /* first time in here since startup - try to read the nexid */
179 if ( li->li_nextid == NOID ) {
180 li->li_nextid = next_id_read( be );
182 if ( li->li_nextid == NOID ) {
186 #if SLAPD_NEXTID_CHUNK > 1
187 li->li_nextid_wrote = li->li_nextid;
194 if ( next_id_write( be, id ) == 0 ) {
195 li->li_nextid_wrote = id;
201 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );