diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 978e2ea0d..f1689cbfe 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -410,6 +410,17 @@ protected: FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) { int mode = getSpiffsMode(openMode, accessMode); int fd = SPIFFS_open(&_fs, path, mode, 0); + if (fd < 0 && _fs.err_code == SPIFFS_ERR_DELETED && (openMode & OM_CREATE)) { + DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d, trying to remove\r\n", + fd, path, openMode, accessMode, _fs.err_code); + auto rc = SPIFFS_remove(&_fs, path); + if (rc != SPIFFS_OK) { + DEBUGV("SPIFFSImpl::open: SPIFFS_ERR_DELETED, but failed to remove path=`%s` openMode=%d accessMode=%d err=%d\r\n", + path, openMode, accessMode, _fs.err_code); + return FileImplPtr(); + } + fd = SPIFFS_open(&_fs, path, mode, 0); + } if (fd < 0) { DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n", fd, path, openMode, accessMode, _fs.err_code); diff --git a/tests/FSWrapper/FSWrapper.ino b/tests/FSWrapper/FSWrapper.ino index b152093d5..099065f4f 100644 --- a/tests/FSWrapper/FSWrapper.ino +++ b/tests/FSWrapper/FSWrapper.ino @@ -139,7 +139,15 @@ void setup() { fail("some files left after format"); } } - + { + File tmp = SPIFFS.open("/tmp.txt", "w"); + } + { + File tmp = SPIFFS.open("/tmp.txt", "w"); + if (!tmp) { + fail("failed to re-open empty file"); + } + } Serial.println("success"); }