Skip to content

Commit f7bbea4

Browse files
committed
Fix failure when trying to open empty file (#1126)
1 parent 053de36 commit f7bbea4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

cores/esp8266/spiffs_api.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,17 @@ class SPIFFSDirImpl : public DirImpl {
410410
FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) {
411411
int mode = getSpiffsMode(openMode, accessMode);
412412
int fd = SPIFFS_open(&_fs, path, mode, 0);
413+
if (fd < 0 && _fs.err_code == SPIFFS_ERR_DELETED && (openMode & OM_CREATE)) {
414+
DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d, trying to remove\r\n",
415+
fd, path, openMode, accessMode, _fs.err_code);
416+
auto rc = SPIFFS_remove(&_fs, path);
417+
if (rc != SPIFFS_OK) {
418+
DEBUGV("SPIFFSImpl::open: SPIFFS_ERR_DELETED, but failed to remove path=`%s` openMode=%d accessMode=%d err=%d\r\n",
419+
path, openMode, accessMode, _fs.err_code);
420+
return FileImplPtr();
421+
}
422+
fd = SPIFFS_open(&_fs, path, mode, 0);
423+
}
413424
if (fd < 0) {
414425
DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n",
415426
fd, path, openMode, accessMode, _fs.err_code);

tests/FSWrapper/FSWrapper.ino

+9-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,15 @@ void setup() {
139139
fail("some files left after format");
140140
}
141141
}
142-
142+
{
143+
File tmp = SPIFFS.open("/tmp.txt", "w");
144+
}
145+
{
146+
File tmp = SPIFFS.open("/tmp.txt", "w");
147+
if (!tmp) {
148+
fail("failed to re-open empty file");
149+
}
150+
}
143151
Serial.println("success");
144152
}
145153

0 commit comments

Comments
 (0)