-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfsimage.cpp
67 lines (60 loc) · 1.83 KB
/
fsimage.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
#include <stdbool.h>
#include <Arduino.h>
#include "fsimage.h"
#define printf Serial.printf
static void unpack_file(FS &fs, const fsimage_entry_t *entry)
{
uint8_t buf[1024];
File file = fs.open(entry->filename, "w");
if (file) {
const unsigned char *p = entry->data;
size_t remain = entry->length;
for (size_t block; remain > 0; remain -= block, p += block) {
block = remain > sizeof(buf) ? sizeof(buf) : remain;
memcpy_P(buf, p, block);
file.write(buf, block);
}
file.close();
}
}
static bool verify_contents(File & file, const fsimage_entry_t *entry)
{
uint8_t buf[1024];
const uint8_t *p = entry->data;
size_t remain = entry->length;
for (size_t block; remain > 0; remain -= block, p += block) {
block = remain > sizeof(buf) ? sizeof(buf) : remain;
file.read(buf, block);
if (memcmp_P(buf, p, block) != 0) {
return false;
}
}
return true;
}
// returns true if file contents are equal to data from file entry
static bool verify_file(FS & fs, const fsimage_entry_t *entry)
{
bool result = false;
// check existence
File file = fs.open(entry->filename, "r");
if (file) {
result = (file.size() == entry->length) && verify_contents(file, entry);
file.close();
}
return result;
}
void fsimage_unpack(FS & fs, bool force)
{
printf("Unpacking files...\n");
for (const fsimage_entry_t * entry = fsimage_table; *entry->filename; entry++) {
printf(" * %12s: ", entry ->filename);
if (force || !verify_file(fs, entry)) {
printf("writing %d bytes ...", entry->length);
unpack_file(fs, entry);
printf("done\n");
} else {
printf("skipped\n");
}
}
printf("All files unpacked\n");
}