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 );
28 /* first time in here since startup - try to read the nexid */
29 if ( li->li_nextid == -1 ) {
30 if ( (fp = fopen( buf, "r" )) == NULL ) {
31 Debug( LDAP_DEBUG_ANY,
32 "next_id %lu: could not open \"%s\"\n",
33 li->li_nextid, buf, 0 );
36 if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
37 li->li_nextid = atol( buf2 );
39 Debug( LDAP_DEBUG_ANY,
40 "next_id %lu: could not fgets nextid from \"%s\"\n",
41 li->li_nextid, buf2, 0 );
49 if ( (fp = fopen( buf, "w" )) == NULL ) {
50 Debug( LDAP_DEBUG_ANY, "next_id %lu: could not open \"%s\"\n",
51 li->li_nextid, buf, 0 );
53 if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
54 Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fprintf\n",
55 li->li_nextid, 0, 0 );
57 if( fclose( fp ) != 0 ) {
58 Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fclose\n",
59 li->li_nextid, 0, 0 );
62 id = li->li_nextid - 1;
63 pthread_mutex_unlock( &li->li_nextid_mutex );
69 next_id_return( Backend *be, ID id )
71 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
75 pthread_mutex_lock( &li->li_nextid_mutex );
76 if ( id != li->li_nextid - 1 ) {
77 pthread_mutex_unlock( &li->li_nextid_mutex );
81 sprintf( buf, "%s/NEXTID", li->li_directory );
84 if ( (fp = fopen( buf, "w" )) == NULL ) {
85 Debug( LDAP_DEBUG_ANY,
86 "next_id_return of %lu: could not open \"%s\" next id %lu\n",
87 id, buf, li->li_nextid );
89 if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
90 Debug( LDAP_DEBUG_ANY,
91 "next_id_return of %lu: cannot fprintf \"%s\" next id %lu\n",
92 id, buf, li->li_nextid );
94 if( fclose( fp ) != 0 ) {
95 Debug( LDAP_DEBUG_ANY,
96 "next_id_return of %lu: cannot fclose \"%s\" next id %lu\n",
97 id, buf, li->li_nextid );
100 pthread_mutex_unlock( &li->li_nextid_mutex );
104 next_id_get( Backend *be )
106 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
107 char buf[MAXPATHLEN];
112 sprintf( buf, "%s/NEXTID", li->li_directory );
114 pthread_mutex_lock( &li->li_nextid_mutex );
115 /* first time in here since startup - try to read the nexid */
116 if ( li->li_nextid == -1 ) {
117 if ( (fp = fopen( buf, "r" )) == NULL ) {
118 Debug( LDAP_DEBUG_ANY,
119 "next_id %lu: could not open \"%s\"\n",
120 li->li_nextid, buf, 0 );
123 if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
124 li->li_nextid = atol( buf2 );
126 Debug( LDAP_DEBUG_ANY,
127 "next_id %lu: cannot fgets nextid from \"%s\"\n",
128 li->li_nextid, buf2, 0 );
135 pthread_mutex_unlock( &li->li_nextid_mutex );