fix: keep a pending config save pending when the write fails

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.
This commit is contained in:
Jason Huebert committed 2026-10-02 23:06:44 -05:00
1 parent 31334fe032
commit f9763ec0ee
2 files changed
+27 -27

No files matched your search

+10 -10
View File
@@ -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
+17 -17
View File
@@ -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