From 590b8e70186f2e500b045aab0e4ab699fcdcccc7 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Tue, 14 Jan 2025 07:09:36 -0600 Subject: [PATCH 1/8] Remove prefs first --- src/SafeFile.cpp | 13 +++++++------ src/SafeFile.h | 2 +- src/mesh/NodeDB.cpp | 10 +++++++++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/SafeFile.cpp b/src/SafeFile.cpp index 94232e81d1..d1dbab077a 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 3d0f81cad2..47a659c8d9 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 762982287f..f5414bafee 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}; From bdc9c7ffd5d492b83f827aacd37c41e565a1ad3d Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 23 Jan 2025 05:39:09 -0600 Subject: [PATCH 2/8] Remove file first --- src/SafeFile.cpp | 7 +++---- src/SafeFile.h | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/SafeFile.cpp b/src/SafeFile.cpp index d1dbab077a..7fd49c77c3 100644 --- a/src/SafeFile.cpp +++ b/src/SafeFile.cpp @@ -3,14 +3,13 @@ #ifdef FSCom // Only way to work on both esp32 and nrf52 -static File openFile(const char *filename, bool fullAtomic, bool removeFirst) +static File openFile(const char *filename, bool fullAtomic) { concurrency::LockGuard g(spiLock); LOG_DEBUG("Opening %s, fullAtomic=%d", filename, fullAtomic); #ifdef ARCH_NRF52 lfs_assert_failed = false; - if (removeFirst) - FSCom.remove(filename); + FSCom.remove(filename); return FSCom.open(filename, FILE_O_WRITE); #endif if (!fullAtomic) @@ -24,7 +23,7 @@ static File openFile(const char *filename, bool fullAtomic, bool removeFirst) } SafeFile::SafeFile(const char *_filename, bool fullAtomic, bool removeFirst) - : filename(_filename), f(openFile(_filename, fullAtomic, removeFirst)), fullAtomic(fullAtomic) + : filename(_filename), f(openFile(_filename, fullAtomic)), fullAtomic(fullAtomic) { } diff --git a/src/SafeFile.h b/src/SafeFile.h index 47a659c8d9..3d0f81cad2 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, bool removeFirst = false); + explicit SafeFile(char const *filepath, bool fullAtomic = false); virtual size_t write(uint8_t); virtual size_t write(const uint8_t *buffer, size_t size); From 16c03d53efdd8ccb7789659e8a2b70f4e25ef991 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 23 Jan 2025 05:41:11 -0600 Subject: [PATCH 3/8] Remove truncate --- src/SafeFile.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/SafeFile.cpp b/src/SafeFile.cpp index 7fd49c77c3..0016be799e 100644 --- a/src/SafeFile.cpp +++ b/src/SafeFile.cpp @@ -59,9 +59,6 @@ bool SafeFile::close() return false; spiLock->lock(); -#ifdef ARCH_NRF52 - f.truncate(); -#endif f.close(); spiLock->unlock(); From 08f3a358aae663c5b690a4ffbbe351a6425b9f50 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 23 Jan 2025 05:44:56 -0600 Subject: [PATCH 4/8] No longer needed --- src/mesh/NodeDB.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index f5414bafee..354a818467 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -1103,16 +1103,7 @@ bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_ bool fullAtomic) { bool okay = false; -#ifdef FSCom - 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); + auto f = SafeFile(filename, fullAtomic); LOG_INFO("Save %s", filename); pb_ostream_t stream = {&writecb, static_cast(&f), protoSize}; From 1d89b0c47e8ebbcc2f26cb618c708723edda08fd Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 23 Jan 2025 07:59:40 -0600 Subject: [PATCH 5/8] Missed a param --- src/SafeFile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SafeFile.cpp b/src/SafeFile.cpp index 0016be799e..21da4e2a24 100644 --- a/src/SafeFile.cpp +++ b/src/SafeFile.cpp @@ -22,7 +22,7 @@ static File openFile(const char *filename, bool fullAtomic) return FSCom.open(filenameTmp.c_str(), FILE_O_WRITE); } -SafeFile::SafeFile(const char *_filename, bool fullAtomic, bool removeFirst) +SafeFile::SafeFile(const char *_filename, bool fullAtomic) : filename(_filename), f(openFile(_filename, fullAtomic)), fullAtomic(fullAtomic) { } From 245c84a47340c278c4fa7cb7d0263bdfd565ef36 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 23 Jan 2025 17:08:37 -0600 Subject: [PATCH 6/8] That wasn't supposed to be there --- src/mesh/NodeDB.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 354a818467..36d44edd02 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -1105,6 +1105,7 @@ bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_ bool okay = false; auto f = SafeFile(filename, fullAtomic); +#ifdef FSCom LOG_INFO("Save %s", filename); pb_ostream_t stream = {&writecb, static_cast(&f), protoSize}; From b1653ee588deae02526083cfa48ea64cf43e8b98 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 23 Jan 2025 17:11:36 -0600 Subject: [PATCH 7/8] Remove vestigal lfs assert --- src/SafeFile.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SafeFile.cpp b/src/SafeFile.cpp index 21da4e2a24..c942aa0ee7 100644 --- a/src/SafeFile.cpp +++ b/src/SafeFile.cpp @@ -8,7 +8,6 @@ static File openFile(const char *filename, bool fullAtomic) concurrency::LockGuard g(spiLock); LOG_DEBUG("Opening %s, fullAtomic=%d", filename, fullAtomic); #ifdef ARCH_NRF52 - lfs_assert_failed = false; FSCom.remove(filename); return FSCom.open(filename, FILE_O_WRITE); #endif From 347c83dced009def10fd9686e68dbfd38e4b4544 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 23 Jan 2025 17:12:12 -0600 Subject: [PATCH 8/8] Durr --- src/mesh/NodeDB.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 36d44edd02..762982287f 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -1103,9 +1103,9 @@ bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_ bool fullAtomic) { bool okay = false; +#ifdef FSCom auto f = SafeFile(filename, fullAtomic); -#ifdef FSCom LOG_INFO("Save %s", filename); pb_ostream_t stream = {&writecb, static_cast(&f), protoSize};