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 #ifdef NEXT_ID_RETURN
113 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
115 pthread_mutex_lock( &li->li_nextid_mutex );
117 if ( id != li->li_nextid - 1 ) {
118 pthread_mutex_unlock( &li->li_nextid_mutex );
123 (void) next_id_write( be, li->li_nextid );
125 pthread_mutex_unlock( &li->li_nextid_mutex );
130 next_id_get( Backend *be )
132 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
135 pthread_mutex_lock( &li->li_nextid_mutex );
137 /* first time in here since startup - try to read the nexid */
138 if ( li->li_nextid == NOID ) {
139 li->li_nextid = next_id_read( be );
141 if ( li->li_nextid == NOID ) {
148 pthread_mutex_unlock( &li->li_nextid_mutex );