diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index b0ba44f28..76dc97757 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -1,6 +1,46 @@ #include "configuration.h" #include "FSCommon.h" + +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 +} + void listDir(const char * dirname, uint8_t levels) { #ifdef FSCom diff --git a/src/FSCommon.h b/src/FSCommon.h index 3493a8464..89b2ed10d 100644 --- a/src/FSCommon.h +++ b/src/FSCommon.h @@ -31,5 +31,6 @@ using namespace Adafruit_LittleFS_Namespace; #endif void fsInit(); +bool renameFile(const char* pathFrom, const char* pathTo); void listDir(const char * dirname, uint8_t levels); void rmDir(const char * dirname); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index cb4c625db..48a525fcd 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -414,7 +414,7 @@ bool saveProto(const char *filename, size_t protoSize, size_t objSize, const pb_ // brief window of risk here ;-) if (FSCom.exists(filename) && !FSCom.remove(filename)) DEBUG_MSG("Warning: Can't remove old pref file\n"); - if (!FSCom.rename(filenameTmp.c_str(), filename)) + if (!renameFile(filenameTmp.c_str(), filename)) DEBUG_MSG("Error: can't rename new pref file\n"); } else { DEBUG_MSG("Can't write prefs\n");