Skip to content

Host file system truncates on FILE_O_WRITE, so the companion keeps one advert and the packet log one line #808

Closed

Description

@hamsolodev

What happened

I've been running the native companion and repeater (v1.18.0, built from source with MeshBench v0.4.1) to check a client against the real firmware. Sharing a contact failed for every contact except the last one heard. I expected any contact the companion holds to be shareable and exportable, as on a radio.

The cause is in meshcore-native's host file system. variants/host/InternalFileSystem.h:29 has #define FILE_O_WRITE "wb", and open() passes that straight to fopen. On the host this empties the file, and the handle is write-only.

The nRF52 build MeshCore is written against behaves differently. Adafruit's LittleFS opens FILE_O_WRITE as LFS_O_RDWR | LFS_O_CREAT, without truncating, then seeks to the end (Adafruit_LittleFS_File.cpp, File::_open_file). So on a radio, a file opened for writing keeps its contents, can be read, and is positioned at its end.

I went through every FILE_O_WRITE open in MeshCore at v1.18.0. Most of them (prefs, contacts, channels, identity, ACL, regions) remove the file first and write it whole, so "wb" comes out the same. Two don't:

  • The companion's advert store. DataStore::putBlobByKey (examples/companion_radio/DataStore.cpp:518-553) opens /adv_blobs with FILE_O_WRITE, reads through the records to find the key or the oldest one, seeks there and writes one record. On the host the read gets nothing, so the record goes at offset 0 of a file that has just been emptied. The file is pre-allocated at boot (20 records, 3,600 bytes), but after the first advert it is one record, 180 bytes, and stays that way. Only the most recently heard advert is kept, so CMD_SHARE_CONTACT and CMD_EXPORT_CONTACT fail for every other contact.
  • The repeater's and room server's packet log. openAppend uses FILE_O_WRITE on nRF52 and relies on the seek to the end (examples/simple_repeater/MyMesh.cpp:386-394, examples/simple_room_server/MyMesh.cpp:141-149). On the host every logged packet replaces the file, so after log start the log only ever holds the newest entry.

It's the same in every meshcore-native tag I looked at (companion, repeater and room server, v1.16.0 to v1.18.0) and on main. The define has been there since the host variant was added.

I ran a companion beside two repeaters, A and B. I had A advert, then B, so A was heard first and B last, and drove the companion with meshcore_py 2.3.19. I then ran it again with the fix in MeshBench/meshcore-native#35 applied.

as it is with the fix
companion's adv_blobs 3,600 B at boot, 180 B after the first advert, still 180 B after both 3,600 B throughout, both records inside
CMD_SHARE_CONTACT, A (first heard) 01 03 (ERR_CODE_TABLE_FULL) 00 (OK)
CMD_SHARE_CONTACT, B (last heard) 00 00
CMD_EXPORT_CONTACT, A 01 02 (ERR_CODE_NOT_FOUND) the advert
CMD_EXPORT_CONTACT, B the advert the advert
A's packet_log after log start, four flood adverts and a message 1 line, 103 B 13 lines, 1,120 B

Share fails with "table full" because shareContactZeroHop returns false when it finds no blob, and the command handler reports any false that way.

How to reproduce it

  1. Run a companion with two other nodes in range, and have each advert in turn.
  2. Look at the companion's adv_blobs in its nodefs folder: 180 bytes, where it was 3,600 at boot.
  3. From a client, share or export the contact heard first: ERR 3 or ERR 2. The last one heard works.
  4. On a repeater, log start, let some traffic through, and look at its packet_log: one line.

Version

MeshBench v0.4.1 (765b1a3); meshcore-native companion-v1.18.0 and repeater-v1.18.0 (f268766), built from source. Also present on meshcore-native main (9097ca8).

Platform

Linux

Anything else

Fix, with a test that fails without it: MeshBench/meshcore-native#35. It 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" isn't enough: it forces every write to the end, which breaks putBlobByKey's seek-and-overwrite. With the fix, the checks I run against the companion (65 cases over its protocol) gave the same results as before.

MeshBench's own internal/mesh/shim/repeater/InternalFileSystem.h:27 has the same define. I know that shim isn't wired up, but it's worth fixing there too so it doesn't come back.

Activity

  1. A13xB0 commented on Nov 21, 2026

    @A13xB0
    Collaborator

    Fixed by MeshBench/meshcore-native#35. Thanks for the detailed report and the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions