-
Notifications
You must be signed in to change notification settings - Fork 1
/
datastore.cpp
134 lines (122 loc) · 3.09 KB
/
datastore.cpp
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
/**
* @file datastore.cpp
* @author Kirill Tregubov (KirillTregubov)
* @author Philip Cai (Gadnalf)
* @copyright Copyright (c) 2022 Kirill Tregubov & Philip Cai
*
* @brief This module contains functionality for initializing and using a
* filesystem.
*
* @bug No known bugs.
*/
#include "datastore.hpp"
BlockDevice *bd = BlockDevice::get_default_instance();
LittleFileSystem fs("fs");
void erase_fs() {
printf("Initializing the block device... ");
fflush(stdout);
int err = bd->init();
printf("%s\n", (err ? "Failed on init" : "OK"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
}
printf("Erasing the block device... ");
fflush(stdout);
err = bd->erase(0, bd->size());
printf("%s\n", (err ? "Failed on erase" : "OK"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
}
printf("Deinitializing the block device... ");
fflush(stdout);
err = bd->deinit();
printf("%s\n", (err ? "Failed on deinit\n" : "OK\n"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
}
}
int mount_fs() {
int err = fs.mount(bd);
printf("%s\n", (err ? "No file system found" : "OK"));
if (err) {
// Reformat if we can't mount the filesystem
printf("Formatting... ");
fflush(stdout);
err = fs.reformat(bd);
printf("%s\n", (err ? "Reformat failed" : "OK"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
return -1;
}
}
return 0;
}
int get_private_key(char *buf) {
FILE *f = fopen(PRIVATE_KEY_PATH, "r");
if (f) {
fgets(buf, PRIVATE_KEY_LENGTH + 1, f);
fclose(f);
return 0;
}
return -1;
}
int get_recovery_keys(char *buf) {
FILE *f = fopen(RECOVERY_KEY_PATH, "r");
if (f) {
fgets(buf, RECOVERY_KEY_LENGTH + 1, f);
fclose(f);
return 0;
}
return -1;
}
int set_private_key(const char *key) {
FILE *f = fopen(PRIVATE_KEY_PATH, "w");
if (f) {
fprintf(f, "%*s", PRIVATE_KEY_LENGTH + 1, key);
fclose(f);
return 0;
}
printf("Cannot open file for write %s: %s\n", PRIVATE_KEY_PATH,
strerror(errno));
return -1;
}
int set_recovery_keys(const char *key) {
FILE *f = fopen(RECOVERY_KEY_PATH, "w");
if (f) {
fprintf(f, "%*s", RECOVERY_KEY_LENGTH + 1, key);
fclose(f);
return 0;
}
printf("Cannot open file for write %s: %s\n", RECOVERY_KEY_PATH,
strerror(errno));
return -1;
}
int write_log(const char *log) {
time_t seconds = time(NULL);
char *formatted_time = (char *)malloc(20);
struct tm *timeinfo = localtime(&seconds);
strftime(formatted_time, 20, "%m/%d/%y %H:%M:%S", timeinfo);
FILE *f = fopen(LOGS_PATH, "a");
if (f) {
fprintf(f, "[%s] %s\n", formatted_time, log);
fclose(f);
return 0;
}
printf("Cannot open file for append %s: %s\n", LOGS_PATH, strerror(errno));
return -1;
}
int print_logs() {
FILE *f = fopen(LOGS_PATH, "r");
int c;
if (f) {
printf("=== Device Log ===\n");
while ((c = getc(f)) != EOF) {
putchar(c);
}
fclose(f);
printf("=== Log Ends ===\n");
return 0;
}
printf("Cannot open file for read %s: %s\n", LOGS_PATH, strerror(errno));
return -1;
}