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 /* All functions except put_nextid() obey slapMode == SLAP_TOOL_MODE. */
22 static ID next_id_read( Backend *be );
23 static ID next_id_get_save( Backend *be, int do_save );
25 #define next_id_write( be, id ) \
26 (slapMode == SLAP_TOOL_MODE ? (be, id, 0) : put_nextid( be, id ))
29 next_id_read( Backend *be )
31 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
34 char* file = li->li_nextid_file;
37 if ( slapMode == SLAP_TOOL_MODE )
40 if ( (fp = fopen( file, "r" )) == NULL ) {
41 Debug( LDAP_DEBUG_ANY,
42 "next_id_read: could not open \"%s\"\n",
47 if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
48 Debug( LDAP_DEBUG_ANY,
49 "next_id_read: could not fgets nextid from \"%s\"\n",
59 Debug( LDAP_DEBUG_ANY,
60 "next_id_read %ld: atol(%s) return non-positive integer\n",
69 put_nextid( Backend *be, ID id )
71 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
72 char* file = li->li_nextid_file;
76 if ( (fp = fopen( file, "w" )) == NULL ) {
77 Debug( LDAP_DEBUG_ANY, "put_nextid(%ld): could not open \"%s\"\n",
84 if ( fprintf( fp, "%ld\n", id ) == EOF ) {
85 Debug( LDAP_DEBUG_ANY, "put_nextid(%ld): cannot fprintf\n",
90 if( fclose( fp ) != 0 ) {
91 Debug( LDAP_DEBUG_ANY, "put_nextid %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 );