Skip to content

host: open FILE_O_WRITE as LittleFS does, without truncating - #35

Merged
A13xB0 merged 1 commit into
MeshBench:mainfrom
hamsolodev:host-file-o-write
Nov 21, 2026
Merged

A13xB0 merged 1 commit into
MeshBench:mainfrom
hamsolodev:host-file-o-write

Conversation

@hamsolodev

@hamsolodev hamsolodev commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What this changes, and why

FILE_O_WRITE was "wb", which empties the file and gives a write-only handle. On nRF52, Adafruit_LittleFS opens it read-write, created if missing, not truncated, positioned at the end. MeshCore relies on that in two places:

  • DataStore::putBlobByKey reads the advert store and overwrites one record in place;
  • openAppend, in the repeater and room server, adds to the packet log.

From outside, the symptom was a companion that could share or export only the last contact it heard (ERR 3 or ERR 2 for any other), and a packet log that held one line. The advert store shrank from its pre-allocated 3,600 bytes to one 180-byte record after the first advert.

open() now opens FILE_O_WRITE with "r+b", falling back to "w+b" when the file doesn't exist, then seeks to the end. "a+b" would not do, because it forces every write to the end and breaks putBlobByKey's seek-and-overwrite. Writers that remove the file first, which is every other FILE_O_WRITE user in MeshCore v1.18.0, behave as before.

Verification

  • variants/host/filesystem_test.cpp, new, run as its header says. Without the change it prints four FAILs (an existing file keeps its contents; a write with no seek lands at the end; the handle reads; a record is overwritten in place) and exits 1. With it, it prints ok. Like streaming_test.cpp, it is run by hand: the workflows don't build it.
  • companion_radio and simple_repeater built with build.sh at companion-v1.18.0 with this change, and run under MeshBench v0.4.1: a companion beside two repeaters, driven by meshcore_py 2.3.19.
    • Without the change: adv_blobs was 180 B after the first advert, sharing the first-heard contact gave 01 03, exporting it gave 01 02, and the repeater's packet_log after log start held 1 line.
    • With it: adv_blobs stayed 3,600 B with both records, share and export answered OK and the advert, and the log held 13 lines.
  • A protocol conformance run of 65 cases against the patched companion gave the same results as the unpatched one.

The change was made against companion-v1.18.0 and applies unchanged to main.

Reported in MeshBench/meshbench#808, which has the reproduction.

FILE_O_WRITE was "wb", which empties the file and gives a write-only
handle. On nRF52, Adafruit_LittleFS opens it read-write, created if
missing, not truncated, positioned at the end, and MeshCore relies on
that in two places: DataStore::putBlobByKey reads the advert store and
overwrites one record in place, and openAppend in the repeater and
room server adds to the packet log. On the host the companion kept
only the last advert heard, so sharing or exporting any other contact
failed, and the packet log held one line.

filesystem_test.cpp pins the behaviour; it fails four checks without
this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants