@@ -42,17 +42,6 @@ static volatile size_t knownLargestSpace = MAX_SPACE;
4242
4343static File f; // don't export to other cpp files
4444
45- // wrapper to find out how long closing takes
46- void closeFile () {
47- #ifdef WLED_DEBUG_FS
48- DEBUGFS_PRINT (F (" Close -> " ));
49- uint32_t s = millis ();
50- #endif
51- f.close ();
52- DEBUGFS_PRINTF (" took %lu ms\n " , millis () - s);
53- doCloseFile = false ;
54- }
55-
5645// find() that reads and buffers data from file stream in 256-byte blocks.
5746// Significantly faster, f.find(key) can take SECONDS for multi-kB files
5847static bool bufferedFind (const char *target, bool fromStart = true ) {
@@ -186,7 +175,7 @@ static void writeSpace(size_t l)
186175 if (knownLargestSpace < l) knownLargestSpace = l;
187176}
188177
189- static bool appendObjectToFile (const char * key, const JsonDocument* content, uint32_t s, uint32_t contentLen = 0 )
178+ static bool appendObjectToFileAndClose (const char * key, const JsonDocument* content, uint32_t s, uint32_t contentLen = 0 )
190179{
191180 #ifdef WLED_DEBUG_FS
192181 DEBUGFS_PRINTLN (F (" Append" ));
@@ -198,7 +187,7 @@ static bool appendObjectToFile(const char* key, const JsonDocument* content, uin
198187 if (f.size () < 3 ) f.print (F (" {\" 0\" :{}}" ));
199188
200189 if (content->isNull ()) {
201- doCloseFile = true ;
190+ f. close () ;
202191 return true ; // nothing to append
203192 }
204193
@@ -210,7 +199,7 @@ static bool appendObjectToFile(const char* key, const JsonDocument* content, uin
210199 f.print (key);
211200 serializeJson (*content, f);
212201 DEBUGFS_PRINTF (" Inserted, took %lu ms (total %lu)" , millis () - s1, millis () - s);
213- doCloseFile = true ;
202+ f. close () ;
214203 return true ;
215204 }
216205
@@ -221,7 +210,7 @@ static bool appendObjectToFile(const char* key, const JsonDocument* content, uin
221210
222211 if (f.size () + 9000 > (fsBytesTotal - fsBytesUsed)) { // make sure there is enough space to at least copy the file once
223212 errorFlag = ERR_FS_QUOTA ;
224- doCloseFile = true ;
213+ f. close () ;
225214 return false ;
226215 }
227216
@@ -255,8 +244,7 @@ static bool appendObjectToFile(const char* key, const JsonDocument* content, uin
255244 // Append object
256245 serializeJson (*content, f);
257246 f.write (' }' );
258-
259- doCloseFile = true ;
247+ f.close ();
260248 DEBUGFS_PRINTF (" Appended, took %lu ms (total %lu)" , millis () - s1, millis () - s);
261249 return true ;
262250}
@@ -270,8 +258,6 @@ bool writeObjectToFileUsingId(const char* file, uint16_t id, const JsonDocument*
270258
271259bool writeObjectToFile (const char * file, const char * key, const JsonDocument* content)
272260{
273- if (doCloseFile) closeFile ();
274-
275261 uint32_t s = 0 ; // timing
276262 #ifdef WLED_DEBUG_FS
277263 DEBUGFS_PRINTF (" Write to %s with key %s >>>\n " , file, (key==nullptr )?" nullptr" :key);
@@ -289,7 +275,7 @@ bool writeObjectToFile(const char* file, const char* key, const JsonDocument* co
289275
290276 if (!bufferedFind (key)) // key does not exist in file
291277 {
292- return appendObjectToFile (key, content, s);
278+ return appendObjectToFileAndClose (key, content, s);
293279 }
294280
295281 // an object with this key already exists, replace or delete it
@@ -325,10 +311,10 @@ bool writeObjectToFile(const char* file, const char* key, const JsonDocument* co
325311 if (pos > 3 ) pos--; // also delete leading comma if not first object
326312 f.seek (pos);
327313 writeSpace (pos2 - pos);
328- if (contentLen) return appendObjectToFile (key, content, s, contentLen);
314+ if (contentLen) return appendObjectToFileAndClose (key, content, s, contentLen);
329315 }
330316
331- doCloseFile = true ;
317+ f. close () ;
332318 DEBUGFS_PRINTF (" Replaced/deleted, took %lu ms\n " , millis () - s);
333319 return true ;
334320}
@@ -343,7 +329,6 @@ bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest
343329// if the key is a nullptr, deserialize entire object
344330bool readObjectFromFile (const char * file, const char * key, JsonDocument* dest, const JsonDocument* filter)
345331{
346- if (doCloseFile) closeFile ();
347332 #ifdef WLED_DEBUG_FS
348333 DEBUGFS_PRINTF (" Read from %s with key %s >>>\n " , file, (key==nullptr )?" nullptr" :key);
349334 uint32_t s = millis ();
0 commit comments