2021-06-27 17:56:28 +00:00
|
|
|
#include "configuration.h"
|
2020-11-19 01:25:02 +00:00
|
|
|
#include "FSCommon.h"
|
|
|
|
|
2022-08-04 07:08:02 +00:00
|
|
|
|
|
|
|
bool copyFile(const char* from, const char* to)
|
|
|
|
{
|
|
|
|
#ifdef FSCom
|
|
|
|
unsigned char cbuffer[16];
|
|
|
|
|
|
|
|
File f1 = FSCom.open(from, FILE_O_READ);
|
|
|
|
if (!f1){
|
|
|
|
DEBUG_MSG("Failed to open file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
File f2 = FSCom.open(to, FILE_O_WRITE);
|
|
|
|
if (!f2) {
|
|
|
|
DEBUG_MSG("Failed to open file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (f1.available() > 0) {
|
|
|
|
byte i = f1.read(cbuffer, 16);
|
|
|
|
f2.write(cbuffer, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
f2.close();
|
|
|
|
f1.close();
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool renameFile(const char* pathFrom, const char* pathTo)
|
|
|
|
{
|
|
|
|
#ifdef FSCom
|
|
|
|
if (copyFile(pathFrom, pathTo) & FSCom.remove(pathFrom) ) {
|
|
|
|
return true;
|
|
|
|
} else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-15 21:49:06 +00:00
|
|
|
void listDir(const char * dirname, uint8_t levels)
|
2022-03-15 21:22:05 +00:00
|
|
|
{
|
2022-06-22 13:33:53 +00:00
|
|
|
#ifdef FSCom
|
2022-03-15 21:49:06 +00:00
|
|
|
File root = FSCom.open(dirname);
|
2022-03-15 21:22:05 +00:00
|
|
|
if(!root){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!root.isDirectory()){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
File file = root.openNextFile();
|
|
|
|
while(file){
|
2022-03-16 07:30:38 +00:00
|
|
|
if(file.isDirectory() && !String(file.name()).endsWith(".")) {
|
2022-03-15 21:22:05 +00:00
|
|
|
if(levels){
|
2022-03-15 21:49:06 +00:00
|
|
|
listDir(file.name(), levels -1);
|
2022-03-15 21:22:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DEBUG_MSG(" %s (%i Bytes)\n", file.name(), file.size());
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
file = root.openNextFile();
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-06-15 15:52:37 +00:00
|
|
|
void rmDir(const char * dirname)
|
|
|
|
{
|
2022-06-22 13:33:53 +00:00
|
|
|
#ifdef FSCom
|
|
|
|
File file = FSCom.open(dirname);
|
|
|
|
if(!file){
|
2022-06-15 15:52:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-22 13:33:53 +00:00
|
|
|
if(!file.isDirectory()){
|
|
|
|
file.close();
|
|
|
|
FSCom.remove(file.name());
|
|
|
|
// DEBUG_MSG("Remove FILE %s\n", file.name());
|
2022-06-15 15:52:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-22 13:33:53 +00:00
|
|
|
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);
|
2022-06-15 15:52:37 +00:00
|
|
|
} else {
|
2022-06-22 13:33:53 +00:00
|
|
|
entry.close();
|
|
|
|
// DEBUG_MSG("Remove FILE %s\n", entry.name());
|
|
|
|
FSCom.remove(entry.name());
|
2022-06-15 15:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-22 13:33:53 +00:00
|
|
|
FSCom.rmdir(dirname);
|
|
|
|
// DEBUG_MSG("Remove DIR %s\n", dirname);
|
2022-06-15 15:52:37 +00:00
|
|
|
file.close();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-19 01:25:02 +00:00
|
|
|
void fsInit()
|
|
|
|
{
|
2022-02-14 17:45:29 +00:00
|
|
|
#ifdef FSCom
|
|
|
|
if (!FSBegin())
|
2020-11-19 01:25:02 +00:00
|
|
|
{
|
2022-03-15 21:22:05 +00:00
|
|
|
DEBUG_MSG("ERROR filesystem mount Failed. Formatting...\n");
|
2020-11-19 01:25:02 +00:00
|
|
|
assert(0); // FIXME - report failure to phone
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_MSG("Filesystem files:\n");
|
2022-03-15 21:49:06 +00:00
|
|
|
listDir("/", 10);
|
2020-11-19 01:25:02 +00:00
|
|
|
#endif
|
|
|
|
}
|