]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
Don't return nextid.
[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 %lu: 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    buf[20];
58         char*   file = li->li_nextid_file; 
59         FILE*   fp;
60         int             rc;
61
62         if ( (fp = fopen( file, "w" )) == NULL ) {
63                 Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): could not open \"%s\"\n",
64                     id, file, 0 );
65                 return -1;
66         } 
67
68         rc = 0;
69
70         if ( fprintf( fp, "%ld\n", id ) == EOF ) {
71                 Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): cannot fprintf\n",
72                     id, 0, 0 );
73                 rc = -1;
74         }
75
76         if( fclose( fp ) != 0 ) {
77                 Debug( LDAP_DEBUG_ANY, "next_id_write %lu: cannot fclose\n",
78                     id, 0, 0 );
79                 rc = -1;
80         }
81
82         return rc;
83 }
84
85 ID
86 next_id( Backend *be )
87 {
88         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
89         ID              id;
90
91         pthread_mutex_lock( &li->li_nextid_mutex );
92
93         /* first time in here since startup - try to read the nexid */
94         if ( li->li_nextid == NOID ) {
95                 li->li_nextid = next_id_read( be );
96
97                 if ( li->li_nextid == NOID ) {
98                         li->li_nextid = 1;
99                 }
100         }
101
102         id = li->li_nextid++;
103         (void) next_id_write( be, li->li_nextid );
104
105         pthread_mutex_unlock( &li->li_nextid_mutex );
106         return( id );
107 }
108
109 void
110 next_id_return( Backend *be, ID id )
111 {
112 #ifdef NEXT_ID_RETURN
113         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
114
115         pthread_mutex_lock( &li->li_nextid_mutex );
116
117         if ( id != li->li_nextid - 1 ) {
118                 pthread_mutex_unlock( &li->li_nextid_mutex );
119                 return;
120         }
121
122         li->li_nextid--;
123         (void) next_id_write( be, li->li_nextid );
124
125         pthread_mutex_unlock( &li->li_nextid_mutex );
126 #endif
127 }
128
129 ID
130 next_id_get( Backend *be )
131 {
132         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
133         ID              id;
134
135         pthread_mutex_lock( &li->li_nextid_mutex );
136
137         /* first time in here since startup - try to read the nexid */
138         if ( li->li_nextid == NOID ) {
139                 li->li_nextid = next_id_read( be );
140
141                 if ( li->li_nextid == NOID ) {
142                         li->li_nextid = 1;
143                 }
144         }
145
146         id = li->li_nextid;
147
148         pthread_mutex_unlock( &li->li_nextid_mutex );
149
150         return( id );
151 }