-
Notifications
You must be signed in to change notification settings - Fork 0
/
datafiles.c
400 lines (356 loc) · 9.72 KB
/
datafiles.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* Database file handling routines.
*
* Services is copyright (c) 1996-1999 Andy Church.
* E-mail: <achurch@dragonfire.net>
* This program is free but copyrighted software; see the file COPYING for
* details.
*/
#include "services.h"
#include "datafiles.h"
#include <fcntl.h>
/*************************************************************************/
/*************************************************************************/
/* Return the version number on the file. Return 0 if there is no version
* number or the number doesn't make sense (i.e. less than 1 or greater
* than FILE_VERSION).
*/
int get_file_version(dbFILE *f)
{
FILE *fp = f->fp;
int version = fgetc(fp)<<24 | fgetc(fp)<<16 | fgetc(fp)<<8 | fgetc(fp);
if (ferror(fp)) {
#ifndef NOT_MAIN
log_perror("Error reading version number on %s", f->filename);
#endif
return 0;
} else if (feof(fp)) {
#ifndef NOT_MAIN
log("Error reading version number on %s: End of file detected",
f->filename);
#endif
return 0;
} else if (version > FILE_VERSION || version < 1) {
#ifndef NOT_MAIN
log("Invalid version number (%d) on %s", version, f->filename);
#endif
return 0;
}
return version;
}
/*************************************************************************/
/* Write the current version number to the file. Return 0 on error, 1 on
* success.
*/
int write_file_version(dbFILE *f)
{
FILE *fp = f->fp;
if (
fputc(FILE_VERSION>>24 & 0xFF, fp) < 0 ||
fputc(FILE_VERSION>>16 & 0xFF, fp) < 0 ||
fputc(FILE_VERSION>> 8 & 0xFF, fp) < 0 ||
fputc(FILE_VERSION & 0xFF, fp) < 0
) {
#ifndef NOT_MAIN
log_perror("Error writing version number on %s", f->filename);
#endif
return 0;
}
return 1;
}
/*************************************************************************/
/*************************************************************************/
static dbFILE *open_db_read(const char *service, const char *filename)
{
dbFILE *f;
FILE *fp;
f = malloc(sizeof(*f));
if (!f) {
#ifndef NOT_MAIN
log_perror("Can't read %s database %s", service, filename);
#endif
return NULL;
}
strscpy(f->filename, filename, sizeof(f->filename));
f->mode = 'r';
fp = fopen(f->filename, "rb");
if (!fp) {
int errno_save = errno;
#ifndef NOT_MAIN
if (errno != ENOENT)
log_perror("Can't read %s database %s", service, f->filename);
#endif
free(f);
errno = errno_save;
return NULL;
}
f->fp = fp;
f->backupfp = NULL;
return f;
}
/*************************************************************************/
static dbFILE *open_db_write(const char *service, const char *filename)
{
dbFILE *f;
int fd;
f = malloc(sizeof(*f));
if (!f) {
#ifndef NOT_MAIN
log_perror("Can't read %s database %s", service, filename);
#endif
return NULL;
}
strscpy(f->filename, filename, sizeof(f->filename));
filename = f->filename;
f->mode = 'w';
*f->backupname = 0;
snprintf(f->backupname, sizeof(f->backupname), "%s.save", filename);
if (!*f->backupname || strcmp(f->backupname, filename) == 0) {
int errno_save = errno;
#ifndef NOT_MAIN
log("Opening %s database %s for write: Filename too long",
service, filename);
#endif
free(f);
errno = errno_save;
return NULL;
}
unlink(f->backupname);
f->backupfp = fopen(filename, "rb");
if (rename(filename, f->backupname) < 0 && errno != ENOENT) {
int errno_save = errno;
#ifndef NOT_MAIN
static int walloped = 0;
if (!walloped) {
walloped++;
canalopers(NULL, "Can't back up %s database %s", service, filename);
}
errno = errno_save;
log_perror("Can't back up %s database %s", service, filename);
if (!NoBackupOkay) {
#endif
if (f->backupfp)
fclose(f->backupfp);
free(f);
errno = errno_save;
return NULL;
#ifndef NOT_MAIN
}
#endif
*f->backupname = 0;
}
unlink(filename);
/* Use open() to avoid people sneaking a new file in under us */
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
f->fp = fdopen(fd, "wb"); /* will fail and return NULL if fd < 0 */
if (!f->fp || !write_file_version(f)) {
int errno_save = errno;
#ifndef NOT_MAIN
static int walloped = 0;
if (!walloped) {
walloped++;
canalopers(NULL, "Can't write to %s database %s", service, filename);
}
errno = errno_save;
log_perror("Can't write to %s database %s", service, filename);
#endif
if (f->fp) {
fclose(f->fp);
unlink(filename);
}
if (*f->backupname && rename(f->backupname, filename) < 0)
#ifndef NOT_MAIN
log_perror("Cannot restore backup copy of %s", filename);
#else
;
#endif
errno = errno_save;
return NULL;
}
return f;
}
/*************************************************************************/
/* Open a database file for reading (*mode == 'r') or writing (*mode == 'w').
* Return the stream pointer, or NULL on error. When opening for write, it
* is an error for rename() to return an error (when backing up the original
* file) other than ENOENT, if NO_BACKUP_OKAY is not defined; it is an error
* if the version number cannot be written to the file; and it is a fatal
* error if opening the file for write fails and the backup was successfully
* made but cannot be restored.
*/
dbFILE *open_db(const char *service, const char *filename, const char *mode)
{
if (*mode == 'r') {
return open_db_read(service, filename);
} else if (*mode == 'w') {
return open_db_write(service, filename);
} else {
errno = EINVAL;
return NULL;
}
}
/*************************************************************************/
/* Restore the database file to its condition before open_db(). This is
* identical to close_db() for files open for reading; however, for files
* open for writing, we first attempt to restore any backup file before
* closing files.
*/
void restore_db(dbFILE *f)
{
int errno_save = errno;
if (f->mode == 'w') {
int ok = 0; /* Did we manage to restore the old file? */
errno = errno_save = 0;
if (*f->backupname && strcmp(f->backupname, f->filename) != 0) {
if (rename(f->backupname, f->filename) == 0)
ok = 1;
}
if (!ok && f->backupfp) {
char buf[1024];
int i;
ok = 1;
if (fseek(f->fp, 0, SEEK_SET) < 0)
ok = 0;
while (ok && (i = fread(buf, 1, sizeof(buf), f->backupfp)) > 0) {
if (fwrite(buf, 1, i, f->fp) != i)
ok = 0;
}
if (ok) {
fflush(f->fp);
ftruncate(fileno(f->fp), ftell(f->fp));
}
}
#ifndef NOT_MAIN
if (!ok && errno > 0)
log_perror("Unable to restore backup of %s", f->filename);
#endif
errno_save = errno;
if (f->backupfp)
fclose(f->backupfp);
if (*f->backupname)
unlink(f->backupname);
}
fclose(f->fp);
if (!errno_save)
errno_save = errno;
free(f);
errno = errno_save;
}
/*************************************************************************/
/* Close a database file. If the file was opened for write, remove the
* backup we (may have) created earlier.
*/
void close_db(dbFILE *f)
{
if (f->mode == 'w' && *f->backupname
&& strcmp(f->backupname, f->filename) != 0) {
if (f->backupfp)
fclose(f->backupfp);
unlink(f->backupname);
}
fclose(f->fp);
free(f);
}
/*************************************************************************/
/*************************************************************************/
/* Read and write 2- and 4-byte quantities, pointers, and strings. All
* multibyte values are stored in big-endian order (most significant byte
* first). A pointer is stored as a byte, either 0 if NULL or 1 if not,
* and read pointers are returned as either (void *)0 or (void *)1. A
* string is stored with a 2-byte unsigned length (including the trailing
* \0) first; a length of 0 indicates that the string pointer is NULL.
* Written strings are truncated silently at 65534 bytes, and are always
* null-terminated.
*
* All routines return -1 on error, 0 otherwise.
*/
int read_int16(uint16 *ret, dbFILE *f)
{
int c1, c2;
c1 = fgetc(f->fp);
c2 = fgetc(f->fp);
if (c1 == EOF || c2 == EOF)
return -1;
*ret = c1<<8 | c2;
return 0;
}
int write_int16(uint16 val, dbFILE *f)
{
if (fputc((val>>8) & 0xFF, f->fp) == EOF || fputc(val & 0xFF, f->fp) == EOF)
return -1;
return 0;
}
int read_int32(uint32 *ret, dbFILE *f)
{
int c1, c2, c3, c4;
c1 = fgetc(f->fp);
c2 = fgetc(f->fp);
c3 = fgetc(f->fp);
c4 = fgetc(f->fp);
if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF)
return -1;
*ret = c1<<24 | c2<<16 | c3<<8 | c4;
return 0;
}
int write_int32(uint32 val, dbFILE *f)
{
if (fputc((val>>24) & 0xFF, f->fp) == EOF)
return -1;
if (fputc((val>>16) & 0xFF, f->fp) == EOF)
return -1;
if (fputc((val>> 8) & 0xFF, f->fp) == EOF)
return -1;
if (fputc((val ) & 0xFF, f->fp) == EOF)
return -1;
return 0;
}
int read_ptr(void **ret, dbFILE *f)
{
int c;
c = fgetc(f->fp);
if (c == EOF)
return -1;
*ret = (c ? (void *)1 : (void *)0);
return 0;
}
int write_ptr(const void *ptr, dbFILE *f)
{
if (fputc(ptr ? 1 : 0, f->fp) == EOF)
return -1;
return 0;
}
int read_string(char **ret, dbFILE *f)
{
char *s;
uint16 len;
if (read_int16(&len, f) < 0)
return -1;
if (len == 0) {
*ret = NULL;
return 0;
}
s = smalloc(len);
if (len != fread(s, 1, len, f->fp)) {
free(s);
return -1;
}
*ret = s;
return 0;
}
int write_string(const char *s, dbFILE *f)
{
uint32 len;
if (!s)
return write_int16(0, f);
len = strlen(s);
if (len > 65534)
len = 65534;
if (write_int16((uint16)(len+1), f) < 0)
return -1;
if (len > 0 && fwrite(s, 1, len, f->fp) != len)
return -1;
if (fputc(0, f->fp) == EOF)
return -1;
return 0;
}
/*************************************************************************/
/*************************************************************************/