1 #include "fastdb.h"
2 #include <stdio.h>
3 #include <string.h>
4 
5 USE_FASTDB_NAMESPACE
6 
main(int argc,char * argv[])7 int main(int argc, char* argv[])
8 {
9     if (argc < 2) {
10         fprintf(stderr, "Usage: forcerecovery [-shadow] [-norecovery] DATABASE-FILE-NAME\n");
11         return 1;
12     }
13     bool swapCurr = false;
14     int  dirty = 1;
15     FILE* f = NULL;
16     for (int i = 1; i < argc; i++) {
17         if (strcmp(argv[i], "-shadow") == 0) {
18             swapCurr = true;
19         } else if (strcmp(argv[i], "-norecovery") == 0) {
20             dirty = 0;
21         } else if (*argv[i] == '-') {
22             fprintf(stderr, "No such option '%s'\n", argv[i]);
23             return 1;
24         } else if (f != NULL) {
25             fprintf(stderr, "File was already specified\n");
26             return 1;
27         } else {
28             f = fopen(argv[i], "r+b");
29             if (f == NULL) {
30                 fprintf(stderr, "Failed to open database file '%s'\n", argv[i]);
31                 return 1;
32             }
33         }
34     }
35     if (f == NULL) {
36         fprintf(stderr, "File was not specified\n");
37         return 1;
38     }
39     dbHeader rec;
40     if (fread(&rec, sizeof(dbHeader), 1, f) != 1) {
41         fprintf(stderr, "Failed to read database header\n");
42         return 1;
43     }
44     if (swapCurr) {
45         rec.curr = 1 - rec.curr;
46     }
47     rec.dirty = dirty;
48     fseek(f, 0, 0);
49     if (fwrite(&rec, sizeof(dbHeader), 1, f) != 1) {
50         fprintf(stderr, "Failed to write database header\n");
51         return 1;
52     }
53     fclose(f);
54     return 0;
55 }
56