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( Backend *be )
19 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25 sprintf( buf, "%s/NEXTID", li->li_directory );
27 pthread_mutex_lock( &li->li_nextid_mutex );
29 /* first time in here since startup - try to read the nexid */
30 if ( li->li_nextid == -1 ) {
31 if ( (fp = fopen( buf, "r" )) == NULL ) {
32 Debug( LDAP_DEBUG_ANY,
33 "next_id %lu: could not open \"%s\"\n",
34 li->li_nextid, buf, 0 );
38 if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
39 li->li_nextid = atol( buf2 );
41 if(li->li_nextid < 1) {
42 /* protect against bad data */
43 Debug( LDAP_DEBUG_ANY,
44 "next_id %lu: atol(%s) return non-positive integer\n",
45 li->li_nextid, buf2, 0 );
50 Debug( LDAP_DEBUG_ANY,
51 "next_id %lu: could not fgets nextid from \"%s\"\n",
52 li->li_nextid, buf2, 0 );
62 if ( (fp = fopen( buf, "w" )) == NULL ) {
63 Debug( LDAP_DEBUG_ANY, "next_id %lu: could not open \"%s\"\n",
64 li->li_nextid, buf, 0 );
66 if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
67 Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fprintf\n",
68 li->li_nextid, 0, 0 );
70 if( fclose( fp ) != 0 ) {
71 Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fclose\n",
72 li->li_nextid, 0, 0 );
76 pthread_mutex_unlock( &li->li_nextid_mutex );
81 next_id_return( Backend *be, ID id )
83 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
87 pthread_mutex_lock( &li->li_nextid_mutex );
89 if ( id != li->li_nextid - 1 ) {
90 pthread_mutex_unlock( &li->li_nextid_mutex );
94 sprintf( buf, "%s/NEXTID", li->li_directory );
97 if ( (fp = fopen( buf, "w" )) == NULL ) {
98 Debug( LDAP_DEBUG_ANY,
99 "next_id_return of %lu: could not open \"%s\" next id %lu\n",
100 id, buf, li->li_nextid );
102 if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
103 Debug( LDAP_DEBUG_ANY,
104 "next_id_return of %lu: cannot fprintf \"%s\" next id %lu\n",
105 id, buf, li->li_nextid );
107 if( fclose( fp ) != 0 ) {
108 Debug( LDAP_DEBUG_ANY,
109 "next_id_return of %lu: cannot fclose \"%s\" next id %lu\n",
110 id, buf, li->li_nextid );
113 pthread_mutex_unlock( &li->li_nextid_mutex );
117 next_id_get( Backend *be )
119 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
120 char buf[MAXPATHLEN];
125 sprintf( buf, "%s/NEXTID", li->li_directory );
127 pthread_mutex_lock( &li->li_nextid_mutex );
129 /* first time in here since startup - try to read the nexid */
130 if ( li->li_nextid == -1 ) {
131 if ( (fp = fopen( buf, "r" )) == NULL ) {
132 Debug( LDAP_DEBUG_ANY,
133 "next_id_get %lu: could not open \"%s\"\n",
134 li->li_nextid, buf, 0 );
138 if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
139 li->li_nextid = atol( buf2 );
141 if(li->li_nextid < 1) {
142 /* protect against bad data */
143 Debug( LDAP_DEBUG_ANY,
144 "next_id_get %lu: atol(%s) return non-positive integer\n",
145 li->li_nextid, buf2, 0 );
150 Debug( LDAP_DEBUG_ANY,
151 "next_id_get %lu: cannot fgets nextid from \"%s\"\n",
152 li->li_nextid, buf2, 0 );
161 pthread_mutex_unlock( &li->li_nextid_mutex );