Debian Codegraph @

sdb.c

raw symbols SCIP
1 /* -*- Mode: C; c-file-style: "stroustrup" -*- */cxx . . $ `<file>/sdb.c`/
2
3 #include <stdio.h>
4 #include <time.h>
5 #include <stdlib.h>
6 #if defined( unixcxx . . $ `<built-in>:395:9`! ) || defined(WIN32 )
7 #include <unistd.h>
8 #elif defined( VMS )
9 #include <unixio.h>
10 #endif
11 #include <string.h>
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include "types.h"cxx . . $ `<file>/types.h`/
17 #include "lock.h"cxx . . $ `<file>/lock.h`/
18
19 #define BUILD_DLLcxx . . $ `sdb.c:19:9`!
20 #include "sdb.h"cxx . . $ `<file>/sdb.h`/
21
22 #include "utct.h"cxx . . $ `<file>/utct.h`/
23
24 #if defined( VMS )
25 #define unlink(x) delete(x)
26 #endif
27
28 #if defined( WIN32 )
29 #define ftruncate chsize
30 #endif
31
32 #define MAX_UTCcxx . . $ `sdb.c:32:9`! 13
33
34 /* simple though inefficient implementation of a database function */
35
36 static int sdb_insertcxx . . $ sdb_insert(176861d66ae08f75).( DBcxx . . $ DB#*, const char* key, const char* val, int* err );
37
38 int sdb_opencxx . . $ sdb_open(e27edb077ed1b892).( DBcxx . . $ DB#* h, const char* filename, int* err )
39 {
40 int fd = 0 ;
41 FILEcxx . . $ FILE#* fp = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ;
42
43 *err = 0;
44 h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
45 if ( filename == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ) { return 0; }
46 fd = opencxx . . $ open(e156e3aa1e5c4ad1).( filename, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!, S_IREADcxx . . $ `<external>/531885356569766532/stat.h:175:10`! | S_IWRITEcxx . . $ `<external>/531885356569766532/stat.h:176:10`! );
47 if ( fd == -1 ) { goto fail; }
48 fp = fdopencxx . . $ fdopen(8f4e2d7350ebd077).( fd, "w+" );
49 if ( fp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ) { goto fail; }
50 h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. = fp;
51 if ( !lock_writecxx . . $ lock_write(89bc1efcdac0ca22).( h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. ) ) { goto fail; }
52 strncpycxx . . $ strncpy(255158a415b673b0).( h->filenamecxx . . $ $anonymous_type_f019eebf87c83299_0#filename., filename, PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! ); h->filenamecxx . . $ $anonymous_type_f019eebf87c83299_0#filename.[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!] = '\0';
53 h->write_poscxx . . $ $anonymous_type_f019eebf87c83299_0#write_pos. = 0;
54 return 1;
55 fail:
56 *err = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
57 return 0;
58 }
59
60 int sdb_closecxx . . $ sdb_close(f4f73c0fb9625ddd).( DBcxx . . $ DB#* h, int* err )
61 {
62 if ( h == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ) { return 0; }
63 if ( fclosecxx . . $ fclose(89bc1efcdac0ca22).( h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. ) == EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`! ) { *err = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; return 0; }
64 *err = 0; return 1;
65 }
66
67 int sdb_addcxx . . $ sdb_add(176861d66ae08f75).( DBcxx . . $ DB#* h, const char* key, const char* val, int* err )
68 {
69 *err = 0;
70
71 if ( h == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ) { return 0; }
72 if ( strlencxx . . $ strlen(751346f8406fd082).( key ) > MAX_KEYcxx . . $ `sdb.h:33:9`! ) { return 0; }
73 if ( strlencxx . . $ strlen(751346f8406fd082).( val ) > MAX_VALcxx . . $ `sdb.h:34:9`! ) { return 0; }
74 if ( fseekcxx . . $ fseek(543fae30d249f6b1).( h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file., 0, SEEK_ENDcxx . . $ `<external>/14710646456994434043/fcntl.h:139:10`! ) == -1 ) { goto fail; }
75 if ( fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).( h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file., "%s %s\n", key, val ) == 0 ) { goto fail; }
76 return 1;
77 fail:
78 *err = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
79 return 0;
80 }
81
82 #define MAX_LINEcxx . . $ `sdb.c:82:9`! (MAX_KEYcxx . . $ `sdb.h:33:9`!+MAX_VALcxx . . $ `sdb.h:34:9`!)
83
84 static void sdb_rewindcxx . . $ sdb_rewind(14613a252c7791ed).( DBcxx . . $ DB#* h )
85 {
86 rewindcxx . . $ rewind(8d4262d45fc644ab).( h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. );
87 h->write_poscxx . . $ $anonymous_type_f019eebf87c83299_0#write_pos. = 0;
88 }
89
90 int sdb_findfirstcxx . . $ sdb_findfirst(cfbcccf6b7331460).( DBcxx . . $ DB#* h, char* key, int klen, char* val, int vlen, int* err )
91 {
92 sdb_rewindcxx . . $ sdb_rewind(14613a252c7791ed).( h );
93 return sdb_findnextcxx . . $ sdb_findnext(cfbcccf6b7331460).( h, key, klen, val, vlen, err );
94 }
95
96 int sdb_findnextcxx . . $ sdb_findnext(cfbcccf6b7331460).( DBcxx . . $ DB#* h, char* key, int klen, char* val, int vlen,
97 int* err )
98 {
99 char line[MAX_LINEcxx . . $ `sdb.c:82:9`!+1] = {0};
100 int line_len = 0 ;
101 char* fkey = line;
102 char* fval = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ;
103
104 *err = 0;
105 if ( h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ) { return 0; }
106 if ( feofcxx . . $ feof(89bc1efcdac0ca22).( h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. ) ) { return 0; }
107 if ( fgetscxx . . $ fgets(efb0aaba7ac866ae).( line, MAX_LINEcxx . . $ `sdb.c:82:9`!, h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. ) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ) { return 0; }
108 line_len = strlencxx . . $ strlen(751346f8406fd082).( line );
109 if ( line_len == 0 ) { return 0; }
110
111 /* remove unix, DOS, and MAC linefeeds */
112
113 if ( line[line_len-1] == '\n' ) { line[--line_len] = '\0'; }
114 if ( line[line_len-1] == '\r' ) { line[--line_len] = '\0'; }
115 if ( line[line_len-1] == '\n' ) { line[--line_len] = '\0'; }
116
117 fval = strchrcxx . . $ strchr(2050848944e00d54).( line, ' ' );
118 if ( fval != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ) { *fval = '\0'; fval++; }
119 else { fval = ""; } /* empty */
120 strncpycxx . . $ strncpy(255158a415b673b0).( key, fkey, klen ); key[klen] = '\0';
121 strncpycxx . . $ strncpy(255158a415b673b0).( val, fval, vlen ); val[vlen] = '\0';
122 return 1;
123 }
124
125 static int sdb_cb_notkeymatchcxx . . $ sdb_cb_notkeymatch(afb5946505032ab8).( const char* key, const char* val,
126 void* arg, int* err )
127 {
128 *err = 0;
129 if ( strncmpcxx . . $ strncmp(fd278af2f295fa2e).( key, arg, MAX_KEYcxx . . $ `sdb.h:33:9`! ) != 0 ) { return 1; }
130 return 0;
131 }
132
133 static int sdb_cb_keymatchcxx . . $ sdb_cb_keymatch(afb5946505032ab8).( const char* key, const char* val,
134 void* arg, int* err )
135 {
136 *err = 0;
137 if ( strncmpcxx . . $ strncmp(fd278af2f295fa2e).( key, arg, MAX_KEYcxx . . $ `sdb.h:33:9`! ) == 0 ) { return 1; }
138 return 0;
139 }
140
141 int sdb_delcxx . . $ sdb_del(e27edb077ed1b892).( DBcxx . . $ DB#* h, const char* key, int* err )
142 {
143 return sdb_updateiteratecxx . . $ sdb_updateiterate(11caef27b9582e).( h, (sdb_wcallbackcxx . . $ sdb_wcallback#)sdb_cb_notkeymatchcxx . . $ sdb_cb_notkeymatch(afb5946505032ab8).,
144 (void*)key, err );
145 }
146
147 int sdb_lookupcxx . . $ sdb_lookup(4eb379e6448fe8d).( DBcxx . . $ DB#* h, const char* key, char* val, int vlen, int* err )
148 {
149 char fkey[MAX_KEYcxx . . $ `sdb.h:33:9`!+1] = {0};
150 return sdb_callbacklookupcxx . . $ sdb_callbacklookup(576815eff8be041).( h, sdb_cb_keymatchcxx . . $ sdb_cb_keymatch(afb5946505032ab8)., (void*)key,
151 fkey, MAX_KEYcxx . . $ `sdb.h:33:9`!, val, vlen, err );
152 }
153
154 int sdb_lookupnextcxx . . $ sdb_lookupnext(4eb379e6448fe8d).( DBcxx . . $ DB#* h, const char* key, char* val, int vlen, int* err )
155 {
156 char fkey[MAX_KEYcxx . . $ `sdb.h:33:9`!+1] = {0};
157 return sdb_callbacklookupnextcxx . . $ sdb_callbacklookupnext(576815eff8be041).( h, sdb_cb_keymatchcxx . . $ sdb_cb_keymatch(afb5946505032ab8)., (void*)key,
158 fkey, MAX_KEYcxx . . $ `sdb.h:33:9`!, val, vlen, err );
159 }
160
161 int sdb_callbacklookupcxx . . $ sdb_callbacklookup(576815eff8be041).( DBcxx . . $ DB#* h, sdb_rcallbackcxx . . $ sdb_rcallback# cb, void* arg, char* key,
162 int klen, char* val, int vlen, int* err )
163 {
164 rewindcxx . . $ rewind(8d4262d45fc644ab).( h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. );
165 return sdb_callbacklookupnextcxx . . $ sdb_callbacklookupnext(576815eff8be041).( h, cb, arg, key, klen, val, vlen, err );
166 }
167
168 int sdb_callbacklookupnextcxx . . $ sdb_callbacklookupnext(576815eff8be041).( DBcxx . . $ DB#* h, sdb_rcallbackcxx . . $ sdb_rcallback# cb, void* arg, char* key,
169 int klen, char* val, int vlen, int* err )
170 {
171 char fkey[MAX_KEYcxx . . $ `sdb.h:33:9`!+1] = {0};
172
173 while ( sdb_findnextcxx . . $ sdb_findnext(cfbcccf6b7331460).( h, fkey, MAX_KEYcxx . . $ `sdb.h:33:9`!, val, vlen, err ) ) {
174 if ( cb( fkey, val, arg, err ) ) {
175 strncpycxx . . $ strncpy(255158a415b673b0).( key, fkey, klen ); key[klen] = '\0';
176 return 1;
177 }
178 }
179 return 0;
180 }
181
182 int sdb_insertcxx . . $ sdb_insert(176861d66ae08f75).( DBcxx . . $ DB#* db, const char* key, const char* val, int* err )
183 {
184 int res = 0 ;
185 *err = 0;
186
187 res = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).( db->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. );
188 if ( res < 0 ) { goto fail; }
189 db->read_poscxx . . $ $anonymous_type_f019eebf87c83299_0#read_pos. = res;
190
191 if ( fseekcxx . . $ fseek(543fae30d249f6b1).( db->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file., db->write_poscxx . . $ $anonymous_type_f019eebf87c83299_0#write_pos., SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`! ) == -1 ) { goto fail; }
192 if ( fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).( db->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file., "%s %s\n", key, val ) == 0 ) { goto fail; }
193
194 res = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).( db->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. );
195 if ( res < 0 ) { goto fail; }
196 db->write_poscxx . . $ $anonymous_type_f019eebf87c83299_0#write_pos. = res;
197
198 if ( fseekcxx . . $ fseek(543fae30d249f6b1).( db->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file., db->read_poscxx . . $ $anonymous_type_f019eebf87c83299_0#read_pos., SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`! ) == -1 ) { goto fail; }
199 return 1;
200 fail:
201 *err = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
202 return 0;
203 }
204
205 int sdb_updateiteratecxx . . $ sdb_updateiterate(11caef27b9582e).( DBcxx . . $ DB#* h, sdb_wcallbackcxx . . $ sdb_wcallback# cb, void* arg, int* err )
206 {
207 int found = 0, res = 0;
208 char fkey[MAX_KEYcxx . . $ `sdb.h:33:9`!+1] = {0};
209 char fval[MAX_VALcxx . . $ `sdb.h:34:9`!+1] = {0};
210
211 for ( found = sdb_findfirstcxx . . $ sdb_findfirst(cfbcccf6b7331460).( h, fkey, MAX_KEYcxx . . $ `sdb.h:33:9`!, fval, MAX_VALcxx . . $ `sdb.h:34:9`!, err );
212 found;
213 found = sdb_findnextcxx . . $ sdb_findnext(cfbcccf6b7331460).( h, fkey, MAX_KEYcxx . . $ `sdb.h:33:9`!, fval, MAX_VALcxx . . $ `sdb.h:34:9`!, err ) ) {
214 if ( cb( fkey, fval, arg, err ) ) {
215 if ( *err ) { goto fail; }
216 if ( !sdb_insertcxx . . $ sdb_insert(176861d66ae08f75).( h, fkey, fval, err ) ) { goto fail; }
217 }
218 else if ( *err ) { goto fail; }
219 }
220
221 res = ftruncatecxx . . $ ftruncate(bba9d6e9fb34fad7).( filenocxx . . $ fileno(89bc1efcdac0ca22).( h->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. ), h->write_poscxx . . $ $anonymous_type_f019eebf87c83299_0#write_pos. );
222 return 1;
223 fail:
224 return 0;
225 }
226
227 /* higher level functions */
228
229 int hashcash_db_opencxx . . $ hashcash_db_open(e27edb077ed1b892).( DBcxx . . $ DB#* db, const char* db_filename, int* err ) {
230 int my_err;
231
232 if ( !err ) { err = &my_err; }
233 if ( !sdb_opencxx . . $ sdb_open(e27edb077ed1b892).( db, db_filename, err ) ) { return 0; }
234 fgetccxx . . $ fgetc(89bc1efcdac0ca22).( db->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. ); /* try read to trigger EOF */
235 if ( feofcxx . . $ feof(89bc1efcdac0ca22).( db->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. ) ) {
236 if ( !sdb_addcxx . . $ sdb_add(176861d66ae08f75).( db, PURGED_KEYcxx . . $ `sdb.h:41:9`!, "700101000000", err ) ) {
237 return 0;
238 }
239 }
240 rewindcxx . . $ rewind(8d4262d45fc644ab).( db->filecxx . . $ $anonymous_type_f019eebf87c83299_0#file. );
241 return 1;
242 }
243
244 int hashcash_db_incxx . . $ hashcash_db_in(c165a313c805dd0c).( DBcxx . . $ DB#* db, char* token, char *period, int* err ) {
245 int in_db = 0;
246 int my_err;
247
248 if ( !err ) { err = &my_err; }
249 *err = 0;
250
251 in_db = sdb_lookupcxx . . $ sdb_lookup(4eb379e6448fe8d).( db, token, period, MAX_UTCcxx . . $ `sdb.c:32:9`!, err );
252 if ( *err ) { return 0; }
253 return in_db;
254 }
255
256 int hashcash_db_addcxx . . $ hashcash_db_add(c165a313c805dd0c).( DBcxx . . $ DB#* db, char* token, char *period, int* err ) {
257 if ( !sdb_addcxx . . $ sdb_add(176861d66ae08f75).( db, token, period, err ) ) {
258 return 0;
259 }
260 return 1;
261 }
262
263 int hashcash_db_closecxx . . $ hashcash_db_close(f4f73c0fb9625ddd).( DBcxx . . $ DB#* db, int* err ) {
264 if ( !sdb_closecxx . . $ sdb_close(f4f73c0fb9625ddd).( db, err ) ) { return 0; }
265 return 1;
266 }