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(); LFS_init();
if(existFile(CANGROW_CFG)) { if(existFile(CANGROW_CFG)) {
Serial.println(":: NICE"); readFile(CANGROW_CFG);
} else { } else {
//Panic(); writeFile(CANGROW_CFG, "{}");
} }
// read the configfile from LittleFS // read the configfile from LittleFS

View file

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