From f9763ec0eeaaa9c47fe2b65d232f4b251d4550ed Mon Sep 17 00:00:00 2001 From: Jason Huebert Date: Fri, 2 Oct 2026 23:06:44 -0500 Subject: [PATCH] fix: keep a pending config save pending when the write fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both save()s cleared their dirty flag before opening the file, so a config that could not be written (open failed, flash full) was silently marked saved and never retried — the change was lost until the next mutation. The flag is now cleared only after the write actually completed: the filter reuses the `ok` it already accumulates, and the battery gate checks the record write for the first time. A failed write stays dirty and loop() retries it; a partial file is already discarded by the length checks in load(). No test: the native FS shim cannot fail an open, and the property is the absence of a lost write rather than a reachable behavior. --- examples/simple_repeater/BatteryGate.cpp | 20 ++++++------- examples/simple_repeater/PacketFilter.cpp | 34 +++++++++++------------ 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/examples/simple_repeater/BatteryGate.cpp b/examples/simple_repeater/BatteryGate.cpp index be9580d5..fe2fa1f3 100644 --- a/examples/simple_repeater/BatteryGate.cpp +++ b/examples/simple_repeater/BatteryGate.cpp @@ -111,17 +111,17 @@ void BatteryGate::load(FILESYSTEM* fs) { } void BatteryGate::save(FILESYSTEM* fs) { - save_flag.clear(); File file = fsOpenWrite(fs, BATT_CFG_FILE); - if (file) { - uint8_t rec[BATT_CFG_RECORD_BYTES]; - rec[0] = BATT_CFG_VERSION; - rec[1] = enabled ? 1 : 0; - memcpy(&rec[2], &suspend_mV, 2); - memcpy(&rec[4], &resume_mV, 2); - file.write(rec, BATT_CFG_RECORD_BYTES); - file.close(); - } + if (!file) return; // keep the dirty flag: the write is retried by loop() + uint8_t rec[BATT_CFG_RECORD_BYTES]; + rec[0] = BATT_CFG_VERSION; + rec[1] = enabled ? 1 : 0; + memcpy(&rec[2], &suspend_mV, 2); + memcpy(&rec[4], &resume_mV, 2); + bool ok = (file.write(rec, BATT_CFG_RECORD_BYTES) == BATT_CFG_RECORD_BYTES); + file.close(); + // only once the config is actually on disk: a failed write stays pending + if (ok) save_flag.clear(); } // ---------------------------------------------------------------- CLI diff --git a/examples/simple_repeater/PacketFilter.cpp b/examples/simple_repeater/PacketFilter.cpp index ef97c251..9b743e00 100644 --- a/examples/simple_repeater/PacketFilter.cpp +++ b/examples/simple_repeater/PacketFilter.cpp @@ -615,25 +615,25 @@ void FilterRules::load(FILESYSTEM* fs) { } void FilterRules::save(FILESYSTEM* fs) { - save_flag.clear(); File file = fsOpenWrite(fs, FILTER_CFG_FILE); - if (file) { - uint8_t hdr[5]; - hdr[0] = FILTER_CFG_VERSION; - hdr[1] = enabled ? 1 : 0; - hdr[2] = (uint8_t)num_rules; - hdr[3] = (uint8_t)num_channels; - hdr[4] = 0; - bool ok = (file.write(hdr, 5) == 5); - ok = ok && (file.write((uint8_t*)&ratelimit_hours, 2) == 2); - for (int i = 0; ok && i < num_rules; i++) { - ok = (file.write((uint8_t*)&rules[i], FILTER_RULE_PERSIST_BYTES) == FILTER_RULE_PERSIST_BYTES); - } - for (int i = 0; ok && i < num_channels; i++) { - ok = (file.write((uint8_t*)&channels[i], sizeof(FilterChannel)) == sizeof(FilterChannel)); - } - file.close(); + if (!file) return; // keep the dirty flag: the write is retried by loop() + uint8_t hdr[5]; + hdr[0] = FILTER_CFG_VERSION; + hdr[1] = enabled ? 1 : 0; + hdr[2] = (uint8_t)num_rules; + hdr[3] = (uint8_t)num_channels; + hdr[4] = 0; + bool ok = (file.write(hdr, 5) == 5); + ok = ok && (file.write((uint8_t*)&ratelimit_hours, 2) == 2); + for (int i = 0; ok && i < num_rules; i++) { + ok = (file.write((uint8_t*)&rules[i], FILTER_RULE_PERSIST_BYTES) == FILTER_RULE_PERSIST_BYTES); } + for (int i = 0; ok && i < num_channels; i++) { + ok = (file.write((uint8_t*)&channels[i], sizeof(FilterChannel)) == sizeof(FilterChannel)); + } + file.close(); + // only once the config is actually on disk: a failed write stays pending + if (ok) save_flag.clear(); } // ---------------------------------------------------------------- CLI