1 /* id.c - keep track of the next id to be given out */
9 #ifdef HAVE_SYS_PARAM_H
10 #include <sys/param.h>
14 #include "back-ldbm.h"
16 static ID next_id_read( Backend *be );
17 static int next_id_write( Backend *be, ID id );
18 static ID next_id_get_save( Backend *be, int do_save );
22 next_id_read( Backend *be )
24 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
27 char* file = li->li_nextid_file;
30 if ( ldbm_ignore_nextid_file )
33 if ( (fp = fopen( file, "r" )) == NULL ) {
34 Debug( LDAP_DEBUG_ANY,
35 "next_id_read: could not open \"%s\"\n",
40 if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
41 Debug( LDAP_DEBUG_ANY,
42 "next_id_read: could not fgets nextid from \"%s\"\n",
52 Debug( LDAP_DEBUG_ANY,
53 "next_id_read %ld: atol(%s) return non-positive integer\n",
62 next_id_write( Backend *be, ID id )
64 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
65 char* file = li->li_nextid_file;
69 if ( ldbm_ignore_nextid_file )
72 if ( (fp = fopen( file, "w" )) == NULL ) {
73 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): could not open \"%s\"\n",
80 if ( fprintf( fp, "%ld\n", id ) == EOF ) {
81 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): cannot fprintf\n",
86 if( fclose( fp ) != 0 ) {
87 Debug( LDAP_DEBUG_ANY, "next_id_write %ld: cannot fclose\n",
96 next_id_save( Backend *be )
98 return( next_id_get_save( be, 1 ) == NOID ? -1 : 0 );
102 next_id( Backend *be )
104 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
107 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
109 /* first time in here since startup - try to read the nexid */
110 if ( li->li_nextid == NOID ) {
111 li->li_nextid = next_id_read( be );
113 if ( li->li_nextid == NOID ) {
117 #if SLAPD_NEXTID_CHUNK > 1
118 li->li_nextid_wrote = li->li_nextid;
122 id = li->li_nextid++;
124 #if SLAPD_NEXTID_CHUNK > 1
125 if ( li->li_nextid > li->li_nextid_wrote ) {
126 li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
127 (void) next_id_write( be, li->li_nextid_wrote );
130 (void) next_id_write( be, li->li_nextid );
133 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
138 next_id_return( Backend *be, ID id )
140 #ifdef SLAPD_NEXTID_RETURN
141 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
143 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
145 if ( id != li->li_nextid - 1 ) {
146 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
152 #if !( SLAPD_NEXTID_CHUNK > 1 )
153 (void) next_id_write( be, li->li_nextid );
156 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
161 next_id_get( Backend *be )
163 return next_id_get_save( be, 0 );
167 next_id_get_save( Backend *be, int do_save )
169 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
172 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
174 /* first time in here since startup - try to read the nexid */
175 if ( li->li_nextid == NOID ) {
176 li->li_nextid = next_id_read( be );
178 if ( li->li_nextid == NOID ) {
182 #if SLAPD_NEXTID_CHUNK > 1
183 li->li_nextid_wrote = li->li_nextid;
190 if ( next_id_write( be, id ) == 0 ) {
191 li->li_nextid_wrote = id;
197 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );