]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
Remove unused variables
[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 static ID
17 next_id_read( Backend *be )
18 {
19         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
20         ID      id;
21         char    buf[20];
22         char*   file = li->li_nextid_file; 
23         FILE*   fp;
24
25         if ( (fp = fopen( file, "r" )) == NULL ) {
26                 Debug( LDAP_DEBUG_ANY,
27                     "next_id_read: could not open \"%s\"\n",
28                     file, 0, 0 );
29                 return NOID;
30         }
31
32         if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
33                 Debug( LDAP_DEBUG_ANY,
34                    "next_id_read: could not fgets nextid from \"%s\"\n",
35                     file, 0, 0 );
36                 fclose( fp );
37                 return NOID;
38         }
39
40         id = atol( buf );
41         fclose( fp );
42
43         if(id < 1) {
44                 Debug( LDAP_DEBUG_ANY,
45                         "next_id_read %ld: atol(%s) return non-positive integer\n",
46                         id, buf, 0 );
47                 return NOID;
48         }
49
50         return id;
51 }
52
53 static int
54 next_id_write( Backend *be, ID id )
55 {
56         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
57         char*   file = li->li_nextid_file; 
58         FILE*   fp;
59         int             rc;
60
61         if ( (fp = fopen( file, "w" )) == NULL ) {
62                 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): could not open \"%s\"\n",
63                     id, file, 0 );
64                 return -1;
65         } 
66
67         rc = 0;
68
69         if ( fprintf( fp, "%ld\n", id ) == EOF ) {
70                 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): cannot fprintf\n",
71                     id, 0, 0 );
72                 rc = -1;
73         }
74
75         if( fclose( fp ) != 0 ) {
76                 Debug( LDAP_DEBUG_ANY, "next_id_write %ld: cannot fclose\n",
77                     id, 0, 0 );
78                 rc = -1;
79         }
80
81         return rc;
82 }
83
84 int
85 next_id_save( Backend *be )
86 {
87         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
88         ID id = next_id_get( be );
89         int rc = next_id_write( be, id );
90
91         if (rc == 0) {
92                 li->li_nextid_wrote = id;
93         }
94
95         return rc;
96 }
97
98 ID
99 next_id( Backend *be )
100 {
101         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
102         ID              id;
103
104         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
105
106         /* first time in here since startup - try to read the nexid */
107         if ( li->li_nextid == NOID ) {
108                 li->li_nextid = next_id_read( be );
109
110                 if ( li->li_nextid == NOID ) {
111                         li->li_nextid = 1;
112                 }
113
114 #if SLAPD_NEXTID_CHUNK > 1
115                 li->li_nextid_wrote = li->li_nextid;
116 #endif
117         }
118
119         id = li->li_nextid++;
120
121 #if SLAPD_NEXTID_CHUNK > 1
122         if ( li->li_nextid > li->li_nextid_wrote ) {
123                 li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
124                 (void) next_id_write( be, li->li_nextid_wrote );
125         }
126 #else
127         (void) next_id_write( be, li->li_nextid );
128 #endif
129
130         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
131         return( id );
132 }
133
134 void
135 next_id_return( Backend *be, ID id )
136 {
137 #ifdef SLAPD_NEXTID_RETURN
138         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
139
140         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
141
142         if ( id != li->li_nextid - 1 ) {
143                 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
144                 return;
145         }
146
147         li->li_nextid--;
148
149 #if !( SLAPD_NEXTID_CHUCK > 1 )
150         (void) next_id_write( be, li->li_nextid );
151 #endif
152
153         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
154 #endif
155 }
156
157 ID
158 next_id_get( Backend *be )
159 {
160         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
161         ID              id;
162
163         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
164
165         /* first time in here since startup - try to read the nexid */
166         if ( li->li_nextid == NOID ) {
167                 li->li_nextid = next_id_read( be );
168
169                 if ( li->li_nextid == NOID ) {
170                         li->li_nextid = 1;
171                 }
172
173 #if SLAPD_NEXTID_CHUNK > 1
174                 li->li_nextid_wrote = li->li_nextid;
175 #endif
176         }
177
178         id = li->li_nextid;
179
180         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
181
182         return( id );
183 }