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