]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
Protoized, moved extern definitions to .h files, fixed related bugs.
[openldap] / servers / slapd / back-ldbm / nextid.c
1 /* id.c - keep track of the next id to be given out */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8
9 #ifdef HAVE_SYS_PARAM_H
10 #include <sys/param.h>
11 #endif
12
13 #include "slap.h"
14 #include "back-ldbm.h"
15
16 ID
17 next_id( Backend *be )
18 {
19         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
20         char            buf[MAXPATHLEN];
21         char            buf2[20];
22         FILE            *fp;
23         ID              id;
24
25         sprintf( buf, "%s/NEXTID", li->li_directory );
26
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 );
34                         li->li_nextid = 1;
35                 } else {
36                         if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
37                                 li->li_nextid = atol( buf2 );
38                         } else {
39                                 Debug( LDAP_DEBUG_ANY,
40                            "next_id %lu: could not fgets nextid from \"%s\"\n",
41                                     li->li_nextid, buf2, 0 );
42                                 li->li_nextid = 1;
43                         }
44                         fclose( fp );
45                 }
46         }
47
48         li->li_nextid++;
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 );
52         } else {
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 );
56                 }
57                 if( fclose( fp ) != 0 ) {
58                         Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fclose\n",
59                             li->li_nextid, 0, 0 );
60                 }
61         }
62         id = li->li_nextid - 1;
63         pthread_mutex_unlock( &li->li_nextid_mutex );
64
65         return( id );
66 }
67
68 void
69 next_id_return( Backend *be, ID id )
70 {
71         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
72         char            buf[MAXPATHLEN];
73         FILE            *fp;
74
75         pthread_mutex_lock( &li->li_nextid_mutex );
76         if ( id != li->li_nextid - 1 ) {
77                 pthread_mutex_unlock( &li->li_nextid_mutex );
78                 return;
79         }
80
81         sprintf( buf, "%s/NEXTID", li->li_directory );
82
83         li->li_nextid--;
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 );
88         } else {
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 );
93                 }
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 );
98                 }
99         }
100         pthread_mutex_unlock( &li->li_nextid_mutex );
101 }
102
103 ID
104 next_id_get( Backend *be )
105 {
106         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
107         char            buf[MAXPATHLEN];
108         char            buf2[20];
109         FILE            *fp;
110         ID              id;
111
112         sprintf( buf, "%s/NEXTID", li->li_directory );
113
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 );
121                         li->li_nextid = 1;
122                 } else {
123                         if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
124                                 li->li_nextid = atol( buf2 );
125                         } else {
126                                 Debug( LDAP_DEBUG_ANY,
127                             "next_id %lu: cannot fgets nextid from \"%s\"\n",
128                                     li->li_nextid, buf2, 0 );
129                                 li->li_nextid = 1;
130                         }
131                         fclose( fp );
132                 }
133         }
134         id = li->li_nextid;
135         pthread_mutex_unlock( &li->li_nextid_mutex );
136
137         return( id );
138 }