Skip to content

Commit fab2bcb

Browse files
Fix issue with file not being created in ESP32 with LittleFS
The output now should be like this at first run. ./components/esp_littlefs/src/littlefs/lfs.c:1071:error: Corrupted dir pair at {0x0, 0x1} Directory doesn't exists. Directory created. /config/wifiSettings.json was opened. Directory exists. /config/apSettings.json was opened. Directory exists. /config/securitySettings.json was opened.
1 parent 1089b03 commit fab2bcb

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/FSPersistence.h

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <StatefulService.h>
55
#include <FS.h>
6+
#include <FactoryResetService.h>
67

78
template <class T>
89
class FSPersistence {
@@ -51,13 +52,33 @@ class FSPersistence {
5152
JsonObject jsonObject = jsonDocument.to<JsonObject>();
5253
_statefulService->read(jsonObject, _stateReader);
5354

55+
if(!_fs->exists(FS_CONFIG_DIRECTORY)){
56+
Serial.println(F("Directory doesn't exists."));
57+
58+
if(_fs->mkdir(FS_CONFIG_DIRECTORY)){
59+
Serial.println(F("Directory created."));
60+
}
61+
else{
62+
Serial.println(F("Can't create the directory."));
63+
}
64+
}
65+
else{
66+
Serial.println(F("Directory exists."));
67+
}
68+
5469
// serialize it to filesystem
5570
File settingsFile = _fs->open(_filePath, "w");
5671

5772
// failed to open file, return false
5873
if (!settingsFile) {
74+
Serial.print(F("Can't open the file: "));
75+
Serial.println(_filePath);
5976
return false;
6077
}
78+
else{
79+
Serial.print(_filePath);
80+
Serial.println(F(" was opened."));
81+
}
6182

6283
// serialize the data to the file
6384
serializeJson(jsonDocument, settingsFile);

0 commit comments

Comments
 (0)