From 328ed747581fc8f08e48ba45d7d212ab7107c176 Mon Sep 17 00:00:00 2001 From: agessaman Date: Mon, 20 Jul 2026 00:10:27 -0700 Subject: [PATCH] fix(mqtt): handle SPIFFS rename conflict in MQTTPrefsFileStore Update commit logic to remove existing destination before renaming the temporary file on ESP32. This change addresses the SPIFFS_ERR_CONFLICTING_NAME issue, ensuring that the new preferences are published correctly. Add tests to validate the new behavior and confirm that the temporary file is handled appropriately. --- src/helpers/CommonCLI.cpp | 15 ++- .../test_mqtt_prefs_atomic_store.cpp | 97 +++++++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 335ca5b74..28eff0824 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -482,8 +482,11 @@ static File openMqttPrefsRead(FILESYSTEM* fs) { } // Filesystem adapter for MQTTPrefsAtomicStore. It writes the new image only to -// /mqtt_prefs.tmp, then relies on the filesystem's atomic rename to publish it -// over /mqtt_prefs. It never removes the current source file itself. +// /mqtt_prefs.tmp, verifies size, then publishes by renaming over /mqtt_prefs. +// ESP32 targets use SPIFFS, whose rename refuses an existing destination +// (SPIFFS_ERR_CONFLICTING_NAME) — so commit() must remove /mqtt_prefs first. +// That is a brief non-atomic window; the verified tmp remains until rename +// succeeds, and abort() cleans it up on failure. class MQTTPrefsFileStore { public: explicit MQTTPrefsFileStore(FILESYSTEM* fs) : _fs(fs) {} @@ -538,7 +541,13 @@ public: } bool commit() { - return _finished && _fs->rename("/mqtt_prefs.tmp", "/mqtt_prefs"); + if (!_finished) return false; + // SPIFFS: rename(tmp, dest) fails when dest exists. LittleFS/POSIX replace + // in place; remove-then-rename is required on ESP32 and correct elsewhere. + if (_fs->exists("/mqtt_prefs") && !_fs->remove("/mqtt_prefs")) { + return false; + } + return _fs->rename("/mqtt_prefs.tmp", "/mqtt_prefs"); } void abort() { diff --git a/test/test_mqtt_prefs_atomic_store/test_mqtt_prefs_atomic_store.cpp b/test/test_mqtt_prefs_atomic_store/test_mqtt_prefs_atomic_store.cpp index 3d7d6f02d..32561dd6c 100644 --- a/test/test_mqtt_prefs_atomic_store/test_mqtt_prefs_atomic_store.cpp +++ b/test/test_mqtt_prefs_atomic_store/test_mqtt_prefs_atomic_store.cpp @@ -212,6 +212,70 @@ AtomicStore::ImageResult runCommonPrefsImage(InMemoryCommonPrefsStore* store) { }); } +// Models ESP32 SPIFFS rename semantics: rename fails when the destination +// already exists (SPIFFS_ERR_CONFLICTING_NAME). Production MQTTPrefsFileStore +// must remove /mqtt_prefs before renaming the verified tmp into place. +class SpiffsLikeMqttPrefsStore { +public: + SpiffsLikeMqttPrefsStore() { + _files["/mqtt_prefs"] = {'o', 'l', 'd', '-', 'p', 'r', 'e', 'f', 's'}; + } + + bool begin() { + _files.erase("/mqtt_prefs.tmp"); + _staging.clear(); + _open = true; + return true; + } + + size_t write(const uint8_t* bytes, size_t size) { + if (!_open) return 0; + _staging.insert(_staging.end(), bytes, bytes + size); + return size; + } + + bool finish() { + _open = false; + _files["/mqtt_prefs.tmp"] = _staging; + _finished = true; + return true; + } + + // Naive POSIX-style replace — wrong for SPIFFS when dest exists. + bool commitReplaceInPlace() { + if (!_finished || _files.count("/mqtt_prefs.tmp") == 0) return false; + if (_files.count("/mqtt_prefs") != 0) return false; // CONFLICTING_NAME + _files["/mqtt_prefs"] = _files["/mqtt_prefs.tmp"]; + _files.erase("/mqtt_prefs.tmp"); + return true; + } + + // Matches CommonCLI MQTTPrefsFileStore::commit() on ESP32. + bool commitRemoveThenRename() { + if (!_finished || _files.count("/mqtt_prefs.tmp") == 0) return false; + _files.erase("/mqtt_prefs"); + _files["/mqtt_prefs"] = _files["/mqtt_prefs.tmp"]; + _files.erase("/mqtt_prefs.tmp"); + return true; + } + + void abort() { + _open = false; + _finished = false; + _staging.clear(); + _files.erase("/mqtt_prefs.tmp"); + } + + const std::vector& source() const { return _files.at("/mqtt_prefs"); } + bool tempExists() const { return _files.count("/mqtt_prefs.tmp") != 0; } + +private: + bool _open = false; + bool _finished = false; + std::vector _staging; + std::map> _files; +}; + } // namespace TEST(MQTTPrefsAtomicStore, CommitPublishesExactHeaderThenPayload) { @@ -393,6 +457,39 @@ TEST(MQTTPrefsAtomicStore, NodePrefsMigrationFailurePreservesSourceAndNeverPrefe } } +TEST(MQTTPrefsAtomicStore, SpiffsRenameRequiresRemoveBeforePublish) { + const uint8_t header[] = {0xf5, 'M', 'Q', 'P', 1, 0, 0x09, 0x00}; + const uint8_t payload[] = {'n', 'e', 'w', '-', 'p', 'r', 'e', 'f', 's'}; + const std::vector published = {0xf5, 'M', 'Q', 'P', 1, 0, 0x09, 0x00, + 'n', 'e', 'w', '-', 'p', 'r', 'e', 'f', 's'}; + const std::vector previous = {'o', 'l', 'd', '-', 'p', 'r', 'e', 'f', 's'}; + + { + SpiffsLikeMqttPrefsStore store; + ASSERT_TRUE(store.begin()); + ASSERT_EQ(sizeof(header), store.write(header, sizeof(header))); + ASSERT_EQ(sizeof(payload), store.write(payload, sizeof(payload))); + ASSERT_TRUE(store.finish()); + // Dest exists → SPIFFS-style rename must fail (the pre-fix production bug). + EXPECT_FALSE(store.commitReplaceInPlace()); + EXPECT_EQ(previous, store.source()); + EXPECT_TRUE(store.tempExists()); + store.abort(); + EXPECT_FALSE(store.tempExists()); + } + + { + SpiffsLikeMqttPrefsStore store; + ASSERT_TRUE(store.begin()); + ASSERT_EQ(sizeof(header), store.write(header, sizeof(header))); + ASSERT_EQ(sizeof(payload), store.write(payload, sizeof(payload))); + ASSERT_TRUE(store.finish()); + EXPECT_TRUE(store.commitRemoveThenRename()); + EXPECT_EQ(published, store.source()); + EXPECT_FALSE(store.tempExists()); + } +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();