Skip to content

Commit aaa2ba0

Browse files
authored
Merge pull request #5789 from netmindz/refactor/fsbytes-accessors
Encapsulate fsBytesUsed/fsBytesTotal with read-only accessors
2 parents 1d36afb + b01cb54 commit aaa2ba0

4 files changed

Lines changed: 15 additions & 4 deletions

File tree

wled00/fcn_declare.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ bool writeObjectToFile(const char* file, const char* key, const JsonDocument* co
122122
bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest, const JsonDocument* filter = nullptr);
123123
bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest, const JsonDocument* filter = nullptr);
124124
void updateFSInfo();
125+
size_t getFsBytesUsed();
126+
size_t getFsBytesTotal();
125127
void closeFile();
126128
inline bool writeObjectToFileUsingId(const String &file, uint16_t id, const JsonDocument* content) { return writeObjectToFileUsingId(file.c_str(), id, content); };
127129
inline bool writeObjectToFile(const String &file, const char* key, const JsonDocument* content) { return writeObjectToFile(file.c_str(), key, content); };

wled00/file.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212

1313
#define FS_BUFSIZE 256
1414

15+
// Filesystem usage stats, refreshed by updateFSInfo() - previously WLED_GLOBAL,
16+
// a leftover from when all state lived in one big extern block regardless of
17+
// who used it. json.cpp only ever reads these (status report), so it gets
18+
// by-value getters rather than a mutable reference - an accidental write from
19+
// outside this file is now a build error instead of a silent bug.
20+
static size_t fsBytesUsed = 0;
21+
static size_t fsBytesTotal = 0;
22+
size_t getFsBytesUsed() { return fsBytesUsed; }
23+
size_t getFsBytesTotal() { return fsBytesTotal; }
24+
1525
/*
1626
* Structural requirements for files managed by writeObjectToFile() and readObjectFromFile() utilities:
1727
* 1. File must be a string representation of a valid JSON object

wled00/json.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,8 @@ void serializeInfo(JsonObject root)
838838
wifi_info[F("ap")] = apActive;
839839

840840
JsonObject fs_info = root.createNestedObject("fs");
841-
fs_info["u"] = fsBytesUsed / 1000;
842-
fs_info["t"] = fsBytesTotal / 1000;
841+
fs_info["u"] = getFsBytesUsed() / 1000;
842+
fs_info["t"] = getFsBytesTotal() / 1000;
843843
fs_info[F("pmt")] = presetsModifiedTime;
844844

845845
root[F("ndc")] = nodeListEnabled ? (int)Nodes.size() : -1;

wled00/wled.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,7 @@ WLED_GLOBAL time_t sunset _INIT(0);
765765
WLED_GLOBAL Toki toki _INIT(Toki());
766766

767767
// General filesystem
768-
WLED_GLOBAL size_t fsBytesUsed _INIT(0);
769-
WLED_GLOBAL size_t fsBytesTotal _INIT(0);
768+
// fsBytesUsed/fsBytesTotal are private to file.cpp - use getFsBytesUsed()/getFsBytesTotal() instead.
770769
WLED_GLOBAL unsigned long presetsModifiedTime _INIT(0L);
771770
WLED_GLOBAL bool doCloseFile _INIT(false);
772771

0 commit comments

Comments
 (0)