Skip to content

Commit 32daa03

Browse files
authored
Merge pull request #5276 from wled/fix_5275_part1
prevent file data loss due to replacing an open file handle (partial fix for #5275) - provides better protection again presets.json corruption.
2 parents 4749247 + a870474 commit 32daa03

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

wled00/file.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void closeFile() {
3838
DEBUGFS_PRINT(F("Close -> "));
3939
uint32_t s = millis();
4040
#endif
41-
f.close();
41+
f.close(); // "if (f)" check is aleady done inside f.close(), and f cannot be nullptr -> no need for double checking before closing the file handle.
4242
DEBUGFS_PRINTF("took %lu ms\n", millis() - s);
4343
doCloseFile = false;
4444
}
@@ -271,6 +271,8 @@ bool writeObjectToFile(const char* file, const char* key, const JsonDocument* co
271271
s = millis();
272272
#endif
273273

274+
if (doCloseFile) closeFile(); // This prevents the loss of file data that is still cached in the File object.
275+
274276
size_t pos = 0;
275277
char fileName[129]; strncpy_P(fileName, file, 128); fileName[128] = 0; //use PROGMEM safe copy as FS.open() does not
276278
f = WLED_FS.open(fileName, WLED_FS.exists(fileName) ? "r+" : "w+");

0 commit comments

Comments
 (0)