From b01cb549740596631dc74bf1b8b0c016f6bf029f Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Sat, 8 Aug 2026 10:39:39 +0100 Subject: [PATCH] Encapsulate fsBytesUsed/fsBytesTotal with read-only accessors fsBytesUsed/fsBytesTotal were WLED_GLOBAL, written and read by file.cpp (via updateFSInfo()), and read-only from json.cpp (status report in serializeInfo()). Since json.cpp never writes them, moved them to file-local static in file.cpp and exposed two by-value getters, getFsBytesUsed()/getFsBytesTotal(), instead of a mutable reference. This is a real, compiler-enforced improvement, not just relabeling: because the getters return by value, an accidental write from json.cpp (or any other file) is now a build error instead of a silent bug - unlike the write-needed Tier-2 cases (loadLedmap, dnsServer) that were deliberately left as plain globals since no accessor shape actually restricts those. No behavior change - purely a storage/access-pattern change. Verified: - esp32dev builds and links cleanly via `pio run -e esp32dev`. - usermods env (builds all 59 usermods) builds and links cleanly via `pio run -e usermods`. - Repo-wide grep confirms no remaining raw references to either identifier outside file.cpp. Co-Authored-By: Claude Sonnet 5 --- wled00/fcn_declare.h | 2 ++ wled00/file.cpp | 10 ++++++++++ wled00/json.cpp | 4 ++-- wled00/wled.h | 3 +-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 18327eb877..a22c9a5911 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -122,6 +122,8 @@ bool writeObjectToFile(const char* file, const char* key, const JsonDocument* co bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest, const JsonDocument* filter = nullptr); bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest, const JsonDocument* filter = nullptr); void updateFSInfo(); +size_t getFsBytesUsed(); +size_t getFsBytesTotal(); void closeFile(); inline bool writeObjectToFileUsingId(const String &file, uint16_t id, const JsonDocument* content) { return writeObjectToFileUsingId(file.c_str(), id, content); }; inline bool writeObjectToFile(const String &file, const char* key, const JsonDocument* content) { return writeObjectToFile(file.c_str(), key, content); }; diff --git a/wled00/file.cpp b/wled00/file.cpp index 5a169d6450..4ab044441e 100644 --- a/wled00/file.cpp +++ b/wled00/file.cpp @@ -12,6 +12,16 @@ #define FS_BUFSIZE 256 +// Filesystem usage stats, refreshed by updateFSInfo() - previously WLED_GLOBAL, +// a leftover from when all state lived in one big extern block regardless of +// who used it. json.cpp only ever reads these (status report), so it gets +// by-value getters rather than a mutable reference - an accidental write from +// outside this file is now a build error instead of a silent bug. +static size_t fsBytesUsed = 0; +static size_t fsBytesTotal = 0; +size_t getFsBytesUsed() { return fsBytesUsed; } +size_t getFsBytesTotal() { return fsBytesTotal; } + /* * Structural requirements for files managed by writeObjectToFile() and readObjectFromFile() utilities: * 1. File must be a string representation of a valid JSON object diff --git a/wled00/json.cpp b/wled00/json.cpp index d68b76f59b..4d4acf1743 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -838,8 +838,8 @@ void serializeInfo(JsonObject root) wifi_info[F("ap")] = apActive; JsonObject fs_info = root.createNestedObject("fs"); - fs_info["u"] = fsBytesUsed / 1000; - fs_info["t"] = fsBytesTotal / 1000; + fs_info["u"] = getFsBytesUsed() / 1000; + fs_info["t"] = getFsBytesTotal() / 1000; fs_info[F("pmt")] = presetsModifiedTime; root[F("ndc")] = nodeListEnabled ? (int)Nodes.size() : -1; diff --git a/wled00/wled.h b/wled00/wled.h index 9bafb49196..3c12964f59 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -766,8 +766,7 @@ WLED_GLOBAL time_t sunset _INIT(0); WLED_GLOBAL Toki toki _INIT(Toki()); // General filesystem -WLED_GLOBAL size_t fsBytesUsed _INIT(0); -WLED_GLOBAL size_t fsBytesTotal _INIT(0); +// fsBytesUsed/fsBytesTotal are private to file.cpp - use getFsBytesUsed()/getFsBytesTotal() instead. WLED_GLOBAL unsigned long presetsModifiedTime _INIT(0L); WLED_GLOBAL bool doCloseFile _INIT(false);