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"
17 next_id_read( Backend *be )
19 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
22 char* file = li->li_nextid_file;
25 if ( (fp = fopen( file, "r" )) == NULL ) {
26 Debug( LDAP_DEBUG_ANY,
27 "next_id_read: could not open \"%s\"\n",
32 if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
33 Debug( LDAP_DEBUG_ANY,
34 "next_id_read: could not fgets nextid from \"%s\"\n",
44 Debug( LDAP_DEBUG_ANY,
45 "next_id_read %ld: atol(%s) return non-positive integer\n",
54 next_id_write( Backend *be, ID id )
56 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
57 char* file = li->li_nextid_file;
61 if ( (fp = fopen( file, "w" )) == NULL ) {
62 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): could not open \"%s\"\n",
69 if ( fprintf( fp, "%ld\n", id ) == EOF ) {
70 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): cannot fprintf\n",
75 if( fclose( fp ) != 0 ) {
76 Debug( LDAP_DEBUG_ANY, "next_id_write %ld: cannot fclose\n",
85 next_id_save( Backend *be )
87 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
88 ID id = next_id_get( be );
89 int rc = next_id_write( be, id );
92 li->li_nextid_wrote = id;
99 next_id( Backend *be )
101 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
104 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
106 /* first time in here since startup - try to read the nexid */
107 if ( li->li_nextid == NOID ) {
108 li->li_nextid = next_id_read( be );
110 if ( li->li_nextid == NOID ) {
114 #if SLAPD_NEXTID_CHUNK > 1
115 li->li_nextid_wrote = li->li_nextid;
119 id = li->li_nextid++;
121 #if SLAPD_NEXTID_CHUNK > 1
122 if ( li->li_nextid > li->li_nextid_wrote ) {
123 li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
124 (void) next_id_write( be, li->li_nextid_wrote );
127 (void) next_id_write( be, li->li_nextid );
130 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
135 next_id_return( Backend *be, ID id )
137 #ifdef SLAPD_NEXTID_RETURN
138 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
140 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
142 if ( id != li->li_nextid - 1 ) {
143 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
149 #if !( SLAPD_NEXTID_CHUCK > 1 )
150 (void) next_id_write( be, li->li_nextid );
153 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
158 next_id_get( Backend *be )
160 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
163 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
165 /* first time in here since startup - try to read the nexid */
166 if ( li->li_nextid == NOID ) {
167 li->li_nextid = next_id_read( be );
169 if ( li->li_nextid == NOID ) {
173 #if SLAPD_NEXTID_CHUNK > 1
174 li->li_nextid_wrote = li->li_nextid;
180 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );