Merge pull request #1594 from meshtastic/littlefs-rename-fix

Littlefs rename fix
This commit is contained in:
Thomas Göttgens 2022-08-04 09:37:34 +02:00 committed by GitHub
commit 39aa7f9880
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 1 deletions

View File

@ -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

View File

@ -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);

View File

@ -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");