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 %lu: atol(%s) return non-positive integer\n",
54 next_id_write( Backend *be, ID id )
56 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
58 char* file = li->li_nextid_file;
62 if ( (fp = fopen( file, "w" )) == NULL ) {
63 Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): could not open \"%s\"\n",
70 if ( fprintf( fp, "%ld\n", id ) == EOF ) {
71 Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): cannot fprintf\n",
76 if( fclose( fp ) != 0 ) {
77 Debug( LDAP_DEBUG_ANY, "next_id_write %lu: cannot fclose\n",
86 next_id( Backend *be )
88 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
91 pthread_mutex_lock( &li->li_nextid_mutex );
93 /* first time in here since startup - try to read the nexid */
94 if ( li->li_nextid == NOID ) {
95 li->li_nextid = next_id_read( be );
97 if ( li->li_nextid == NOID ) {
102 id = li->li_nextid++;
103 (void) next_id_write( be, li->li_nextid );
105 pthread_mutex_unlock( &li->li_nextid_mutex );
110 next_id_return( Backend *be, ID id )
112 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
114 pthread_mutex_lock( &li->li_nextid_mutex );
116 if ( id != li->li_nextid - 1 ) {
117 pthread_mutex_unlock( &li->li_nextid_mutex );
122 (void) next_id_write( be, li->li_nextid );
124 pthread_mutex_unlock( &li->li_nextid_mutex );
128 next_id_get( Backend *be )
130 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
133 pthread_mutex_lock( &li->li_nextid_mutex );
135 /* first time in here since startup - try to read the nexid */
136 if ( li->li_nextid == NOID ) {
137 li->li_nextid = next_id_read( be );
139 if ( li->li_nextid == NOID ) {
146 pthread_mutex_unlock( &li->li_nextid_mutex );