]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
e51f4d84e76ce4de24f4dc6a4819befdc4cc3deb
[openldap] / servers / slapd / back-ldbm / nextid.c
1 /* id.c - keep track of the next id to be given out */
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <sys/param.h>
7 #include "slap.h"
8 #include "back-ldbm.h"
9
10 ID
11 next_id( Backend *be )
12 {
13         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
14         char            buf[MAXPATHLEN];
15         char            buf2[20];
16         FILE            *fp;
17         ID              id;
18
19         sprintf( buf, "%s/NEXTID", li->li_directory );
20
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 );
28                         li->li_nextid = 1;
29                 } else {
30                         if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
31                                 li->li_nextid = atol( buf2 );
32                         } else {
33                                 Debug( LDAP_DEBUG_ANY,
34                             "next_id %d: could not fgets nextid from \"%s\"\n",
35                                     li->li_nextid, buf2, 0 );
36                                 li->li_nextid = 1;
37                         }
38                         fclose( fp );
39                 }
40         }
41
42         li->li_nextid++;
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 );
46         } else {
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 );
50                 }
51                 if( fclose( fp ) != 0 ) {
52                         Debug( LDAP_DEBUG_ANY, "next_id %d: cannot fclose\n",
53                             li->li_nextid, 0, 0 );
54                 }
55         }
56         id = li->li_nextid - 1;
57         pthread_mutex_unlock( &li->li_nextid_mutex );
58
59         return( id );
60 }
61
62 void
63 next_id_return( Backend *be, ID id )
64 {
65         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
66         char            buf[MAXPATHLEN];
67         FILE            *fp;
68
69         pthread_mutex_lock( &li->li_nextid_mutex );
70         if ( id != li->li_nextid - 1 ) {
71                 pthread_mutex_unlock( &li->li_nextid_mutex );
72                 return;
73         }
74
75         sprintf( buf, "%s/NEXTID", li->li_directory );
76
77         li->li_nextid--;
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 );
82         } else {
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 );
87                 }
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 );
92                 }
93         }
94         pthread_mutex_unlock( &li->li_nextid_mutex );
95 }
96
97 ID
98 next_id_get( Backend *be )
99 {
100         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
101         char            buf[MAXPATHLEN];
102         char            buf2[20];
103         FILE            *fp;
104         ID              id;
105
106         sprintf( buf, "%s/NEXTID", li->li_directory );
107
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 );
115                         li->li_nextid = 1;
116                 } else {
117                         if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
118                                 li->li_nextid = atol( buf2 );
119                         } else {
120                                 Debug( LDAP_DEBUG_ANY,
121                             "next_id %d: cannot fgets nextid from \"%s\"\n",
122                                     li->li_nextid, buf2, 0 );
123                                 li->li_nextid = 1;
124                         }
125                         fclose( fp );
126                 }
127         }
128         id = li->li_nextid;
129         pthread_mutex_unlock( &li->li_nextid_mutex );
130
131         return( id );
132 }