-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileUtils.cpp
executable file
·80 lines (66 loc) · 1.85 KB
/
fileUtils.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
#include <FS.h>
#include <LittleFS.h>
// Filesystem variables
FS* fileSystem = &LittleFS;
LittleFSConfig fileSystemConfig = LittleFSConfig();
static bool fsOK = false;
bool isFileExist(const char * _path) {
return fileSystem->exists(_path);
}
bool isfsOK() {
return fsOK;
}
void fs_setup() {
////////////////////////////////
// FILESYSTEM INIT
fileSystemConfig.setAutoFormat(false);
fileSystem->setConfig(fileSystemConfig);
fsOK = fileSystem->begin();
Serial.println(fsOK ? F("Filesystem initialized.") : F("Filesystem init failed!"));
}
void appendFile(const char * path, const char * message) {
// Serial.printf("Appending to file: %s\n", path);
File file = fileSystem->open(path, "a");
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if (!file.print(message)) {
// Serial.println("Message appended");
// } else {
Serial.println("Append failed");
}
file.close();
}
void writeFile(const char * path, const char * message) {
File file = fileSystem->open(path, "w");
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if (!file.print(message)) {
// Serial.println("Message appended");
// } else {
Serial.println("Append failed");
}
file.close();
}
void deleteRecursive(const char *path) {
String pathStr = "";
File file = fileSystem->open(path, "r");
bool isDir = file.isDirectory();
file.close();
// If it's a plain file, delete it
if (!isDir) {
fileSystem->remove(path);
return;
}
// Otherwise delete its contents first
Dir dir = fileSystem->openDir(path);
while (dir.next()) {
pathStr = String(path) + "/" + dir.fileName();
deleteRecursive(pathStr.c_str());
}
// Then delete the folder itself
fileSystem->rmdir(path);
}