fix and refactor FSCommon for new ESPIDF

This commit is contained in:
Thomas Göttgens 2022-09-23 19:51:08 +02:00
parent e8f4a8b739
commit af4d11e17b
2 changed files with 47 additions and 39 deletions

View File

@ -33,17 +33,23 @@ bool copyFile(const char* from, const char* to)
bool renameFile(const char* pathFrom, const char* pathTo) bool renameFile(const char* pathFrom, const char* pathTo)
{ {
#ifdef FSCom #ifdef FSCom
#ifdef ARCH_ESP32
// rename was fixed for ESP32 IDF LittleFS in April
return FSCom.rename(pathFrom, pathTo);
#else
if (copyFile(pathFrom, pathTo) && FSCom.remove(pathFrom) ) { if (copyFile(pathFrom, pathTo) && FSCom.remove(pathFrom) ) {
return true; return true;
} else{ } else{
return false; return false;
} }
#endif #endif
#endif
} }
void listDir(const char * dirname, uint8_t levels) void listDir(const char * dirname, uint8_t levels, boolean del = false)
{ {
#ifdef FSCom #ifdef FSCom
char buffer[255];
File root = FSCom.open(dirname, FILE_O_READ); File root = FSCom.open(dirname, FILE_O_READ);
if(!root){ if(!root){
return; return;
@ -57,62 +63,64 @@ void listDir(const char * dirname, uint8_t levels)
if(file.isDirectory() && !String(file.name()).endsWith(".")) { if(file.isDirectory() && !String(file.name()).endsWith(".")) {
if(levels){ if(levels){
#ifdef ARCH_ESP32 #ifdef ARCH_ESP32
listDir(file.path(), levels -1); listDir(file.path(), levels -1, del);
if(del) {
DEBUG_MSG("Removing %s\n", file.path());
strcpy(buffer, file.path());
file.close();
FSCom.rmdir(buffer);
} else {
file.close();
}
#else #else
listDir(file.name(), levels -1); listDir(file.name(), levels -1, del);
file.close();
#endif #endif
} }
} else { } else {
#ifdef ARCH_ESP32 #ifdef ARCH_ESP32
if(del) {
DEBUG_MSG("Deleting %s\n", file.path());
strcpy(buffer, file.path());
file.close();
FSCom.remove(buffer);
} else {
DEBUG_MSG(" %s (%i Bytes)\n", file.path(), file.size()); DEBUG_MSG(" %s (%i Bytes)\n", file.path(), file.size());
file.close();
}
#else #else
DEBUG_MSG(" %s (%i Bytes)\n", file.name(), file.size()); DEBUG_MSG(" %s (%i Bytes)\n", file.name(), file.size());
file.close();
#endif #endif
} }
file.close();
file = root.openNextFile(); file = root.openNextFile();
} }
file.close(); #ifdef ARCH_ESP32
if(del) {
DEBUG_MSG("Removing %s\n", root.path());
strcpy(buffer, root.path());
root.close();
FSCom.rmdir(buffer);
} else {
root.close();
}
#else
root.close();
#endif
#endif #endif
} }
void rmDir(const char * dirname) void rmDir(const char * dirname)
{ {
#ifdef FSCom #ifdef FSCom
File file = FSCom.open(dirname, FILE_O_READ); #ifdef ARCH_ESP32
if(!file){
return;
}
if(!file.isDirectory()){
file.close();
FSCom.remove(file.name());
// DEBUG_MSG("Remove FILE %s\n", file.name());
return;
}
file.rewindDirectory();
while (true) {
File entry = file.openNextFile();
if (!entry) {
break;
}
char dirpath[100]; // array to hold the result.
strcpy(dirpath, dirname); // copy string one into the result.
strcat(dirpath,"/"); // append string two to the result.
strcat(dirpath,entry.name()); // append string two to the result.
if(entry.isDirectory() && !String(entry.name()).endsWith(".")) {
entry.close();
DEBUG_MSG("Descend DIR %s\n", dirpath);
rmDir(dirpath);
} else {
entry.close();
DEBUG_MSG("Remove FILE %s\n", entry.name());
FSCom.remove(entry.name());
}
}
FSCom.rmdir(dirname); FSCom.rmdir(dirname);
DEBUG_MSG("Remove DIR %s\n", dirname); listDir(dirname, 10, true);
file.close(); #else
// nRF52 implementation of LittleFS has a recursive delete function
FSCom.rmdir_r(dirname);
#endif
#endif #endif
} }
@ -125,7 +133,7 @@ void fsInit()
assert(0); // FIXME - report failure to phone assert(0); // FIXME - report failure to phone
} }
#ifdef ARCH_ESP32 #ifdef ARCH_ESP32
DEBUG_MSG("Filesystem files (%d/%d total Bytes):\n", FSCom.usedBytes(), FSCom.totalBytes()); DEBUG_MSG("Filesystem files (%d/%d Bytes):\n", FSCom.usedBytes(), FSCom.totalBytes());
#else #else
DEBUG_MSG("Filesystem files:\n"); DEBUG_MSG("Filesystem files:\n");
#endif #endif

View File

@ -42,5 +42,5 @@ using namespace Adafruit_LittleFS_Namespace;
void fsInit(); void fsInit();
bool copyFile(const char* from, const char* to); bool copyFile(const char* from, const char* to);
bool renameFile(const char* pathFrom, const char* pathTo); bool renameFile(const char* pathFrom, const char* pathTo);
void listDir(const char * dirname, uint8_t levels); void listDir(const char * dirname, uint8_t levels, boolean del);
void rmDir(const char * dirname); void rmDir(const char * dirname);