add deleteFile() to CanGrow_LittleFS.h

This commit is contained in:
Marcus 2024-10-17 22:43:56 +02:00
parent 985b2cac5d
commit f130700cf0
2 changed files with 26 additions and 16 deletions

View file

@ -126,9 +126,9 @@ void setup() {
LFS_init();
if(existFile(CANGROW_CFG)) {
Serial.println(":: NICE");
readFile(CANGROW_CFG);
} else {
//Panic();
writeFile(CANGROW_CFG, "{}");
}
// read the configfile from LittleFS

View file

@ -46,7 +46,7 @@ void LFS_init() {
}
void LFS_format() {
Serial.println(":: [LittleFS] formatting");
Serial.println(":: [LittleFS] formatting...");
// ESP32 LittleFS needs begin() first, otherwise it would crash
// ESP8266 does not need it, so we leave it
#ifdef ESP32
@ -70,13 +70,11 @@ bool existFile(const char *path) {
#endif
if (!file) {
Serial.print(":: [LittleFS] File does not exist ");
Serial.println(path);
Serial.printf(":: [LittleFS] file does not exist: %s\n", path);
file.close();
return false;
} else {
Serial.print(":: [LittleFS] File does exist ");
Serial.println(path);
Serial.printf(":: [LittleFS] file does exist: %s\n", path);
file.close();
return true;
}
@ -93,13 +91,11 @@ void readFile(const char *path) {
#endif
if (!file) {
Serial.print(":: [LittleFS] Failed to open file for reading ");
Serial.println(path);
Serial.printf(":: [LittleFS] FAILED to open file for reading: %s\n");
return;
}
Serial.print(":: [LittleFS] file content ");
Serial.println(path);
Serial.printf(":: [LittleFS] file content: %s\n", path);
while (file.available()) { Serial.write(file.read()); }
file.close();
}
@ -115,21 +111,35 @@ void writeFile(const char *path, const char *message) {
#endif
if (!file) {
Serial.print(":: [LittleFS] Failed to open file for writing ");
Serial.println(path);
Serial.printf(":: [LittleFS] FAILED to open file for writing: %s\n", path);
return;
}
if (file.print(message)) {
Serial.println("File written");
Serial.printf(":: [LittleFS] file written: %s\n", path);
} else {
Serial.println("Write failed");
Serial.printf(":: [LittleFS] writing file FAILED: %s\n", path);
}
delay(2000); // Make sure the CREATE and LASTWRITE times are different
file.close();
}
void deleteFile() {
void deleteFile(const char *path) {
#ifdef ESP32
fs::FS &fs = LittleFS;
File file = fs.open(path, FILE_WRITE);
#endif
Serial.printf(":: [LittleFS] deleting file: %s\n", path);
#ifdef ESP8266
if (LittleFS.remove(path)) {
#endif
#ifdef ESP32
if (fs.remove(path)) {
#endif
Serial.printf(":: [LittleFS] deleted file: %s\n", path);
} else {
Serial.printf(":: [LittleFS] deleting file FAILED: %s\n", path);
}
}