1 /* id.c - keep track of the next id to be given out */
5 #include <sys/socket.h>
11 next_id( Backend *be )
13 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
19 sprintf( buf, "%s/NEXTID", li->li_directory );
21 pthread_mutex_lock( &li->li_nextid_mutex );
22 /* first time in here since startup - try to read the nexid */
23 if ( li->li_nextid == -1 ) {
24 if ( (fp = fopen( buf, "r" )) == NULL ) {
25 Debug( LDAP_DEBUG_ANY,
26 "next_id %d: could not open \"%s\"\n",
27 li->li_nextid, buf, 0 );
30 if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
31 li->li_nextid = atol( buf2 );
33 Debug( LDAP_DEBUG_ANY,
34 "next_id %d: could not fgets nextid from \"%s\"\n",
35 li->li_nextid, buf2, 0 );
43 if ( (fp = fopen( buf, "w" )) == NULL ) {
44 Debug( LDAP_DEBUG_ANY, "next_id %d: could not open \"%s\"\n",
45 li->li_nextid, buf, 0 );
47 if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
48 Debug( LDAP_DEBUG_ANY, "next_id %d: cannot fprintf\n",
49 li->li_nextid, 0, 0 );
51 if( fclose( fp ) != 0 ) {
52 Debug( LDAP_DEBUG_ANY, "next_id %d: cannot fclose\n",
53 li->li_nextid, 0, 0 );
56 id = li->li_nextid - 1;
57 pthread_mutex_unlock( &li->li_nextid_mutex );
63 next_id_return( Backend *be, ID id )
65 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
69 pthread_mutex_lock( &li->li_nextid_mutex );
70 if ( id != li->li_nextid - 1 ) {
71 pthread_mutex_unlock( &li->li_nextid_mutex );
75 sprintf( buf, "%s/NEXTID", li->li_directory );
78 if ( (fp = fopen( buf, "w" )) == NULL ) {
79 Debug( LDAP_DEBUG_ANY,
80 "next_id_return of %d: could not open \"%s\" next id %d\n",
81 id, buf, li->li_nextid );
83 if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
84 Debug( LDAP_DEBUG_ANY,
85 "next_id_return of %d: cannot fprintf \"%s\" next id %d\n",
86 id, buf, li->li_nextid );
88 if( fclose( fp ) != 0 ) {
89 Debug( LDAP_DEBUG_ANY,
90 "next_id_return of %d: cannot fclose \"%s\" next id %d\n",
91 id, buf, li->li_nextid );
94 pthread_mutex_unlock( &li->li_nextid_mutex );
98 next_id_get( Backend *be )
100 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
101 char buf[MAXPATHLEN];
106 sprintf( buf, "%s/NEXTID", li->li_directory );
108 pthread_mutex_lock( &li->li_nextid_mutex );
109 /* first time in here since startup - try to read the nexid */
110 if ( li->li_nextid == -1 ) {
111 if ( (fp = fopen( buf, "r" )) == NULL ) {
112 Debug( LDAP_DEBUG_ANY,
113 "next_id %d: could not open \"%s\"\n",
114 li->li_nextid, buf, 0 );
117 if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
118 li->li_nextid = atol( buf2 );
120 Debug( LDAP_DEBUG_ANY,
121 "next_id %d: cannot fgets nextid from \"%s\"\n",
122 li->li_nextid, buf2, 0 );
129 pthread_mutex_unlock( &li->li_nextid_mutex );