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 ( (fp = fopen( file, "r" )) == NULL ) {
31 Debug( LDAP_DEBUG_ANY,
32 "next_id_read: could not open \"%s\"\n",
37 if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
38 Debug( LDAP_DEBUG_ANY,
39 "next_id_read: could not fgets nextid from \"%s\"\n",
49 Debug( LDAP_DEBUG_ANY,
50 "next_id_read %ld: atol(%s) return non-positive integer\n",
59 next_id_write( Backend *be, ID id )
61 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
62 char* file = li->li_nextid_file;
66 if ( (fp = fopen( file, "w" )) == NULL ) {
67 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): could not open \"%s\"\n",
74 if ( fprintf( fp, "%ld\n", id ) == EOF ) {
75 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): cannot fprintf\n",
80 if( fclose( fp ) != 0 ) {
81 Debug( LDAP_DEBUG_ANY, "next_id_write %ld: cannot fclose\n",
90 next_id_save( Backend *be )
92 return( next_id_get_save( be, 1 ) == NOID ? -1 : 0 );
96 next_id( Backend *be )
98 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
101 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
103 /* first time in here since startup - try to read the nexid */
104 if ( li->li_nextid == NOID ) {
105 li->li_nextid = next_id_read( be );
107 if ( li->li_nextid == NOID ) {
111 #if SLAPD_NEXTID_CHUNK > 1
112 li->li_nextid_wrote = li->li_nextid;
116 id = li->li_nextid++;
118 #if SLAPD_NEXTID_CHUNK > 1
119 if ( li->li_nextid > li->li_nextid_wrote ) {
120 li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
121 (void) next_id_write( be, li->li_nextid_wrote );
124 (void) next_id_write( be, li->li_nextid );
127 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
132 next_id_return( Backend *be, ID id )
134 #ifdef SLAPD_NEXTID_RETURN
135 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
137 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
139 if ( id != li->li_nextid - 1 ) {
140 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
146 #if !( SLAPD_NEXTID_CHUNK > 1 )
147 (void) next_id_write( be, li->li_nextid );
150 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
155 next_id_get( Backend *be )
157 return next_id_get_save( be, 0 );
161 next_id_get_save( Backend *be, int do_save )
163 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
166 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
168 /* first time in here since startup - try to read the nexid */
169 if ( li->li_nextid == NOID ) {
170 li->li_nextid = next_id_read( be );
172 if ( li->li_nextid == NOID ) {
176 #if SLAPD_NEXTID_CHUNK > 1
177 li->li_nextid_wrote = li->li_nextid;
184 if ( next_id_write( be, id ) == 0 ) {
185 li->li_nextid_wrote = id;
191 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );