Keep whole contact records ahead of a partial trailing record
Upstream MeshCore, and this fork up to v1.17.1.6 except ESP32 Full builds, rewrote /contacts3 in place, one record after another. A reset or a full volume during that save leaves whole records followed by part of the next one. Upstream reads such a file up to the short read. Here readableContactFile() treats any size that is not a multiple of 152 bytes as unreadable, and since 2311160a9 the bounded recovery then deletes /contacts3. On the first boot after an upgrade, three whole records plus one stray byte became zero contacts. Invariant: on ESP32, RP2040 and STM32, when /contacts3 holds at least one whole record followed by part of one, and /contacts3.bak does not exist, the whole records load and nothing is written or deleted at boot. The next ordinary save publishes only whole records through the existing transaction. Unchanged: whole-record images; a backup still wins over an incomplete primary, including a backup that is unreadable or of unknown presence at boot, which keeps its retry at the recovery boundary; read errors keep the bounded recovery; a file with no whole record is still discarded; nRF52 is not touched. selectContactFile() makes this check only after the strict checks of both names fail, through a new partial_tail mode of readableContactFile(). loadContacts() drops its own size check; its whole-record loop bound already stops before the tail. Add test_companion_contact_partial_tail.py, which runs the real load, recovery and save paths on the uncached ESP32, RP2040 and STM32 harness and on the ESP32 Full cache harness under ASan/UBSan. On the old code all four fail with zero contacts and /contacts3 deleted. It catches 14 of 15 mutations of this change, including a full revert; the other drops a guard whose value is read only when the file handle is valid. Two cases in the uncached storage fixture used a one-byte truncation as their unreadable image; they now use a file shorter than one record, so they still exercise the discard path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0e72474f41
commit
618c18d385
4 files changed
+258
-17
No files matched your search
@@ -2111,12 +2111,15 @@ void DataStore::clearContactLoadFailure() {
|
||||
#if !defined(NRF52_PLATFORM)
|
||||
// Validate a complete image before retiring its alternative. Legacy contacts
|
||||
// have no file CRC; retain their existing record/route repair semantics.
|
||||
static bool readableContactFile(FILESYSTEM* fs, const char* path) {
|
||||
// partial_tail instead requires whole records followed by part of one.
|
||||
static bool readableContactFile(FILESYSTEM* fs, const char* path,
|
||||
bool partial_tail = false) {
|
||||
File file = mesh::openFileRead(fs, path);
|
||||
bool valid = file && !file.isDirectory()
|
||||
&& file.size() % mesh::storage::CONTACT_RECORD_SIZE == 0;
|
||||
bool valid = file && !file.isDirectory();
|
||||
const size_t tail = valid ? file.size() % mesh::storage::CONTACT_RECORD_SIZE : 0;
|
||||
valid = valid && (partial_tail ? tail != 0 && file.size() > tail : tail == 0);
|
||||
uint8_t record[mesh::storage::CONTACT_RECORD_SIZE];
|
||||
size_t remaining = valid ? file.size() : 0;
|
||||
size_t remaining = valid ? file.size() - tail : 0;
|
||||
while (valid && remaining != 0) {
|
||||
valid = file.read(record, sizeof(record)) == sizeof(record);
|
||||
remaining -= sizeof(record);
|
||||
@@ -2154,6 +2157,17 @@ const char* DataStore::selectContactFile(bool& present) {
|
||||
}
|
||||
}
|
||||
if (failed) {
|
||||
// Older firmware rewrote this file in place; a reset or full volume
|
||||
// during that save leaves part of a record after whole ones. Keep those
|
||||
// records while no backup exists for this incomplete image to retire.
|
||||
bool backup = false;
|
||||
#if defined(ESP32_PLATFORM) || defined(RP2040_PLATFORM)
|
||||
if (!companionPathPresence(fs, "/contacts3.bak", backup)) backup = true;
|
||||
#endif
|
||||
if (!backup && readableContactFile(fs, "/contacts3", true)) {
|
||||
present = true;
|
||||
return "/contacts3";
|
||||
}
|
||||
queueContactRecovery(true);
|
||||
return nullptr;
|
||||
}
|
||||
@@ -2500,17 +2514,6 @@ void DataStore::loadContacts(DataStoreHost* host) {
|
||||
if (file) {
|
||||
bool full = false;
|
||||
uint16_t record_index = 0;
|
||||
#if !defined(NRF52_PLATFORM)
|
||||
if (file.size() % mesh::storage::CONTACT_RECORD_SIZE != 0) {
|
||||
#if MESH_CONTACT_CACHE
|
||||
_cache_load_incomplete = true;
|
||||
#else
|
||||
_uncached_contact_load_incomplete = true;
|
||||
#endif
|
||||
file.close();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#if defined(NRF52_PLATFORM)
|
||||
_legacy_contact_count =
|
||||
mesh::storage::legacyContactCountForSize(legacy_size);
|
||||
@@ -2519,6 +2522,7 @@ void DataStore::loadContacts(DataStoreHost* host) {
|
||||
#endif
|
||||
while (!full
|
||||
#if !defined(NRF52_PLATFORM)
|
||||
// A partial trailing record admitted by selectContactFile() stays unread.
|
||||
&& record_index < file.size() / mesh::storage::CONTACT_RECORD_SIZE
|
||||
#endif
|
||||
#if defined(NRF52_PLATFORM)
|
||||
|
||||
Reference in new issue
Block a user