@@ -221,13 +221,9 @@ class SPIFFSFileImpl : public FileImpl {
221
221
: _fs(fs)
222
222
, _fd(fd)
223
223
, _stat({0 })
224
+ , _written(false )
224
225
{
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 ();
231
227
}
232
228
233
229
~SPIFFSFileImpl () override {
@@ -242,7 +238,7 @@ class SPIFFSFileImpl : public FileImpl {
242
238
DEBUGV (" SPIFFS_write rc=%d\r\n " , result);
243
239
return 0 ;
244
240
}
245
-
241
+ _written = true ;
246
242
return result;
247
243
}
248
244
@@ -265,11 +261,16 @@ class SPIFFSFileImpl : public FileImpl {
265
261
if (rc < 0 ) {
266
262
DEBUGV (" SPIFFS_fflush rc=%d\r\n " , rc);
267
263
}
264
+ _written = true ;
268
265
}
269
266
270
267
bool seek (uint32_t pos, SeekMode mode) override {
271
268
CHECKFD ();
272
269
270
+ int32_t offset = static_cast <int32_t >(pos);
271
+ if (mode == SeekEnd) {
272
+ offset = -offset;
273
+ }
273
274
auto rc = SPIFFS_lseek (_fs->getFs (), _fd, pos, (int ) mode);
274
275
if (rc < 0 ) {
275
276
DEBUGV (" SPIFFS_lseek rc=%d\r\n " , rc);
@@ -293,7 +294,9 @@ class SPIFFSFileImpl : public FileImpl {
293
294
294
295
size_t size () const override {
295
296
CHECKFD ();
296
-
297
+ if (_written) {
298
+ _getStat ();
299
+ }
297
300
return _stat.size ;
298
301
}
299
302
@@ -311,9 +314,20 @@ class SPIFFSFileImpl : public FileImpl {
311
314
}
312
315
313
316
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
+
314
327
SPIFFSImpl* _fs;
315
328
spiffs_file _fd;
316
- spiffs_stat _stat;
329
+ mutable spiffs_stat _stat;
330
+ mutable bool _written;
317
331
};
318
332
319
333
class SPIFFSDirImpl : public DirImpl {
0 commit comments