diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index 42715605c1..3f7aca01b5 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -25,7 +25,7 @@ #include "interrupts.h" #include "MD5Builder.h" #include "umm_malloc/umm_malloc.h" -#include "cont.h" +#include "coredecls.h" extern "C" { #include "user_interface.h" @@ -574,3 +574,11 @@ String EspClass::getSketchMD5() result = md5.toString(); return result; } + +void EspClass::setAutoFormatSPIFFS (bool autoFormat) { + _spiffsAutoFormat = autoFormat; +} + +bool EspClass::getAutoFormatSPIFFS () { + return _spiffsAutoFormat; +} diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index b2ee09144b..09ed49e199 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -153,6 +153,9 @@ class EspClass { bool eraseConfig(); inline uint32_t getCycleCount(); + + bool getAutoFormatSPIFFS (); + void setAutoFormatSPIFFS (bool autoFormat); }; uint32_t EspClass::getCycleCount() diff --git a/cores/esp8266/coredecls.h b/cores/esp8266/coredecls.h index 3ac87eee9f..ff7d02e075 100644 --- a/cores/esp8266/coredecls.h +++ b/cores/esp8266/coredecls.h @@ -12,6 +12,7 @@ extern "C" { #include // g_pcont declaration extern bool timeshift64_is_set; +extern bool _spiffsAutoFormat; void esp_yield(); void esp_schedule(); diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 390a6412fd..85ef8b3583 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -23,6 +23,8 @@ */ #include "spiffs_api.h" +bool _spiffsAutoFormat = true; + using namespace fs; FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) diff --git a/cores/esp8266/spiffs_api.h b/cores/esp8266/spiffs_api.h index 4ce8d3e6ba..7228af3970 100644 --- a/cores/esp8266/spiffs_api.h +++ b/cores/esp8266/spiffs_api.h @@ -32,6 +32,7 @@ #include "spiffs/spiffs.h" #include "debug.h" #include "flash_utils.h" +#include "coredecls.h" // _spiffsAutoFormat using namespace fs; @@ -124,6 +125,9 @@ class SPIFFSImpl : public FSImpl if (_tryMount()) { return true; } + if (!_spiffsAutoFormat) { + return false; + } auto rc = SPIFFS_format(&_fs); if (rc != SPIFFS_OK) { DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code); diff --git a/doc/filesystem.rst b/doc/filesystem.rst index 3e6fdeffbf..3f975ab5ae 100644 --- a/doc/filesystem.rst +++ b/doc/filesystem.rst @@ -136,6 +136,10 @@ This method mounts SPIFFS file system. It must be called before any other FS APIs are used. Returns *true* if file system was mounted successfully, false otherwise. +This will by default format filesystem when SPIFFS is used the first time. +Note that this can be disabled with a call to +``ESP.setAutoFormatSPIFFS(false);`` prior to ``SPIFFS.begin()``. + end ~~~ diff --git a/libraries/esp8266/keywords.txt b/libraries/esp8266/keywords.txt index f6bb96b5d7..55c0303c9f 100644 --- a/libraries/esp8266/keywords.txt +++ b/libraries/esp8266/keywords.txt @@ -60,6 +60,8 @@ getResetInfo KEYWORD2 getResetInfoPtr KEYWORD2 eraseConfig KEYWORD2 getCycleCount KEYWORD2 +setAutoFormatSPIFFS KEYWORD2 +getAutoFormatSPIFFS KEYWORD2 ####################################### # Constants (LITERAL1)