Skip to content

Commit

Permalink
Fix issue with file not being created in ESP32 with LittleFS
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
FernandoGarcia committed Apr 8, 2022
1 parent 38d9f0d commit 7cfeae1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/FSPersistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <StatefulService.h>
#include <FS.h>
#include <FactoryResetService.h>

template <class T>
class FSPersistence {
Expand Down Expand Up @@ -51,13 +52,33 @@ class FSPersistence {
JsonObject jsonObject = jsonDocument.to<JsonObject>();
_statefulService->read(jsonObject, _stateReader);

if(!_fs->exists(FS_CONFIG_DIRECTORY)){
Serial.println(F("Directory doesn't exists."));

if(_fs->mkdir(FS_CONFIG_DIRECTORY)){
Serial.println(F("Directory created."));
}
else{
Serial.println(F("Can't create the directory."));
}
}
else{
Serial.println(F("Directory exists."));
}

// serialize it to filesystem
File settingsFile = _fs->open(_filePath, "w");

// failed to open file, return false
if (!settingsFile) {
Serial.print(F("Can't open the file: "));
Serial.println(_filePath);
return false;
}
else{
Serial.print(_filePath);
Serial.println(F(" was opened."));
}

// serialize the data to the file
serializeJson(jsonDocument, settingsFile);
Expand Down

0 comments on commit 7cfeae1

Please sign in to comment.