Skip to content

Commit 75e0366

Browse files
committed
File system: fix SeekEnd behaviour, fix size() not being updated after write()
1 parent 66d7a9b commit 75e0366

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

hardware/esp8266com/esp8266/cores/esp8266/spiffs_api.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,9 @@ class SPIFFSFileImpl : public FileImpl {
221221
: _fs(fs)
222222
, _fd(fd)
223223
, _stat({0})
224+
, _written(false)
224225
{
225-
CHECKFD();
226-
auto rc = SPIFFS_fstat(_fs->getFs(), _fd, &_stat);
227-
if (rc != SPIFFS_OK) {
228-
DEBUGV("SPIFFS_fstat rc=%d\r\n", rc);
229-
_stat = {0};
230-
}
226+
_getStat();
231227
}
232228

233229
~SPIFFSFileImpl() override {
@@ -242,7 +238,7 @@ class SPIFFSFileImpl : public FileImpl {
242238
DEBUGV("SPIFFS_write rc=%d\r\n", result);
243239
return 0;
244240
}
245-
241+
_written = true;
246242
return result;
247243
}
248244

@@ -265,11 +261,16 @@ class SPIFFSFileImpl : public FileImpl {
265261
if (rc < 0) {
266262
DEBUGV("SPIFFS_fflush rc=%d\r\n", rc);
267263
}
264+
_written = true;
268265
}
269266

270267
bool seek(uint32_t pos, SeekMode mode) override {
271268
CHECKFD();
272269

270+
int32_t offset = static_cast<int32_t>(pos);
271+
if (mode == SeekEnd) {
272+
offset = -offset;
273+
}
273274
auto rc = SPIFFS_lseek(_fs->getFs(), _fd, pos, (int) mode);
274275
if (rc < 0) {
275276
DEBUGV("SPIFFS_lseek rc=%d\r\n", rc);
@@ -293,7 +294,9 @@ class SPIFFSFileImpl : public FileImpl {
293294

294295
size_t size() const override {
295296
CHECKFD();
296-
297+
if (_written) {
298+
_getStat();
299+
}
297300
return _stat.size;
298301
}
299302

@@ -311,9 +314,20 @@ class SPIFFSFileImpl : public FileImpl {
311314
}
312315

313316
protected:
317+
void _getStat() const{
318+
CHECKFD();
319+
auto rc = SPIFFS_fstat(_fs->getFs(), _fd, &_stat);
320+
if (rc != SPIFFS_OK) {
321+
DEBUGV("SPIFFS_fstat rc=%d\r\n", rc);
322+
_stat = {0};
323+
}
324+
_written = false;
325+
}
326+
314327
SPIFFSImpl* _fs;
315328
spiffs_file _fd;
316-
spiffs_stat _stat;
329+
mutable spiffs_stat _stat;
330+
mutable bool _written;
317331
};
318332

319333
class SPIFFSDirImpl : public DirImpl {

0 commit comments

Comments
 (0)