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.
This commit is contained in:
1 parent
ebca9bdd3e
commit
328ed74758
2 files changed
+109
-3
No files matched your search
@@ -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() {
|
||||
|
||||
@@ -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<uint8_t>& 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<uint8_t> _staging;
|
||||
std::map<std::string, std::vector<uint8_t>> _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<uint8_t> published = {0xf5, 'M', 'Q', 'P', 1, 0, 0x09, 0x00,
|
||||
'n', 'e', 'w', '-', 'p', 'r', 'e', 'f', 's'};
|
||||
const std::vector<uint8_t> 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();
|
||||
|
||||
Reference in new issue
Block a user