diff --git a/src/SafeFile.cpp b/src/SafeFile.cpp index 94232e81d..d1dbab077 100644 --- a/src/SafeFile.cpp +++ b/src/SafeFile.cpp @@ -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) { } diff --git a/src/SafeFile.h b/src/SafeFile.h index 3d0f81cad..47a659c8d 100644 --- a/src/SafeFile.h +++ b/src/SafeFile.h @@ -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); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 762982287..f5414bafe 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -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(&f), protoSize};