From 99e2ea64de12b1d2af1c3af8b7523cfdd7c7a7c2 Mon Sep 17 00:00:00 2001 From: mch <32224+hamsolodev@users.noreply.github.com> Date: Sat, 21 Nov 2026 08:57:13 +1100 Subject: [PATCH] host: open FILE_O_WRITE as LittleFS does, without truncating 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. --- variants/host/InternalFileSystem.h | 12 ++++- variants/host/filesystem_test.cpp | 83 ++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 variants/host/filesystem_test.cpp diff --git a/variants/host/InternalFileSystem.h b/variants/host/InternalFileSystem.h index d0313c9..b44a38d 100644 --- a/variants/host/InternalFileSystem.h +++ b/variants/host/InternalFileSystem.h @@ -172,7 +172,17 @@ class Adafruit_LittleFS { // this fails, and it fails silently as a null file. auto slash = p.find_last_of('/'); if (slash != std::string::npos) ensureDir(p.substr(0, slash)); - FILE* f = fopen(p.c_str(), mode); + // FILE_O_WRITE as Adafruit_LittleFS opens it on nRF52: read-write, + // created if missing, not truncated, positioned at the end. fopen's "wb" + // emptied the file, and putBlobByKey and openAppend rely on keeping it. + FILE* f; + if (strcmp(mode, FILE_O_WRITE) == 0) { + f = fopen(p.c_str(), "r+b"); + if (!f) f = fopen(p.c_str(), "w+b"); + if (f) fseek(f, 0, SEEK_END); + } else { + f = fopen(p.c_str(), mode); + } return File(f, p); } diff --git a/variants/host/filesystem_test.cpp b/variants/host/filesystem_test.cpp new file mode 100644 index 0000000..0240ad0 --- /dev/null +++ b/variants/host/filesystem_test.cpp @@ -0,0 +1,83 @@ +// FILE_O_WRITE must open a file as Adafruit_LittleFS does on nRF52. +// +// There it is LFS_O_RDWR | LFS_O_CREAT, then a seek to the end: the file keeps +// what it holds, can be read back, and a write with no seek lands at the end. +// MeshCore leans on all three. DataStore::putBlobByKey reads the advert store +// through a FILE_O_WRITE handle and overwrites one record in place, and the +// repeater's and room server's openAppend adds a line to the packet log. A host +// open that truncated would keep one advert and one log line, and nothing would +// say so. +// +// c++ -std=c++17 -I variants/host variants/host/filesystem_test.cpp -o /tmp/filesystem_test +// /tmp/filesystem_test + +#include "InternalFileSystem.h" + +#include +#include +#include +#include + +static int failures = 0; + +static void expect(bool ok, const char* what) { + if (!ok) { + printf("FAIL %s\n", what); + failures++; + } +} + +static std::string contents(Adafruit_LittleFS& fs, const char* path) { + File f = fs.open(path, FILE_O_READ); + std::string out; + for (int c = f.read(); c >= 0; c = f.read()) out += (char)c; + f.close(); + return out; +} + +int main() { + char dir[] = "/tmp/filesystem_test.XXXXXX"; + if (!mkdtemp(dir)) { + perror("mkdtemp"); + return 2; + } + Adafruit_LittleFS fs; + fs.setRoot(dir); + + // A missing file is created. + File f = fs.open("/blobs", FILE_O_WRITE); + expect((bool)f, "a missing file opens for writing"); + f.write((const uint8_t*)"AAAABBBBCCCC", 12); + f.close(); + expect(contents(fs, "/blobs") == "AAAABBBBCCCC", "a new file holds what was written"); + + // An existing file keeps its contents, and opens at its end. + f = fs.open("/blobs", FILE_O_WRITE); + expect(f.size() == 12, "an existing file keeps its contents"); + f.write((const uint8_t*)"DDDD", 4); + f.close(); + expect(contents(fs, "/blobs") == "AAAABBBBCCCCDDDD", "a write with no seek lands at the end"); + + // It can be read, then overwritten in place, as putBlobByKey does. + f = fs.open("/blobs", FILE_O_WRITE); + uint8_t rec[4] = {0}; + f.seek(4); + expect(f.read(rec, 4) == 4 && memcmp(rec, "BBBB", 4) == 0, "a FILE_O_WRITE handle reads"); + f.seek(4); + f.write((const uint8_t*)"XXXX", 4); + f.close(); + expect(contents(fs, "/blobs") == "AAAAXXXXCCCCDDDD", "a record is overwritten in place"); + + // A file removed first is written whole, as every other writer does. + fs.remove("/blobs"); + f = fs.open("/blobs", FILE_O_WRITE); + f.write((const uint8_t*)"EE", 2); + f.close(); + expect(contents(fs, "/blobs") == "EE", "remove then open writes a file afresh"); + + fs.remove("/blobs"); + rmdir(dir); + if (failures) return 1; + printf("ok\n"); + return 0; +}