Remove prefs first

This commit is contained in:
Ben Meadors 2025-01-14 07:09:36 -06:00
parent 8e8b22edb0
commit 590b8e7018
3 changed files with 17 additions and 8 deletions

View File

@ -3,14 +3,15 @@
#ifdef FSCom
// Only way to work on both esp32 and nrf52
static File openFile(const char *filename, bool fullAtomic)
static File openFile(const char *filename, bool fullAtomic, bool removeFirst)
{
concurrency::LockGuard g(spiLock);
LOG_DEBUG("Opening %s, fullAtomic=%d", filename, fullAtomic);
#ifdef ARCH_NRF52
File file = FSCom.open(filename, FILE_O_WRITE);
file.seek(0);
return file;
lfs_assert_failed = false;
if (removeFirst)
FSCom.remove(filename);
return FSCom.open(filename, FILE_O_WRITE);
#endif
if (!fullAtomic)
FSCom.remove(filename); // Nuke the old file to make space (ignore if it !exists)
@ -22,8 +23,8 @@ static File openFile(const char *filename, bool fullAtomic)
return FSCom.open(filenameTmp.c_str(), FILE_O_WRITE);
}
SafeFile::SafeFile(const char *_filename, bool fullAtomic)
: filename(_filename), f(openFile(_filename, fullAtomic)), fullAtomic(fullAtomic)
SafeFile::SafeFile(const char *_filename, bool fullAtomic, bool removeFirst)
: filename(_filename), f(openFile(_filename, fullAtomic, removeFirst)), fullAtomic(fullAtomic)
{
}

View File

@ -25,7 +25,7 @@
class SafeFile : public Print
{
public:
explicit SafeFile(char const *filepath, bool fullAtomic = false);
explicit SafeFile(char const *filepath, bool fullAtomic = false, bool removeFirst = false);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buffer, size_t size);

View File

@ -1104,7 +1104,15 @@ bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_
{
bool okay = false;
#ifdef FSCom
auto f = SafeFile(filename, fullAtomic);
bool removeFirst = false;
#ifdef ARCH_NRF52
// On nrf52 we have to fully remove the device state file before writing it,
// because the filesystem seems to just append to the file otherwise.
if (filename == prefFileName) {
removeFirst = true;
}
#endif
auto f = SafeFile(filename, fullAtomic, removeFirst);
LOG_INFO("Save %s", filename);
pb_ostream_t stream = {&writecb, static_cast<Print *>(&f), protoSize};