Skip to content

Commit fa5040d

Browse files
Only update LittleFS timestamp when opened write (#6956)
Fixes #6955 LittleFS was updating the timestamp on any close, not only for files when they were opened for writing. This could lead to excessive writes to the flash. Preserve the LFS flags, and only update the timestamp if the file was opened for writing.
1 parent 2492043 commit fa5040d

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

libraries/LittleFS/src/LittleFS.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ FileImplPtr LittleFSImpl::open(const char* path, OpenMode openMode, AccessMode a
7272
if (rc == LFS_ERR_ISDIR) {
7373
// To support the SD.openNextFile, a null FD indicates to the LittleFSFile this is just
7474
// a directory whose name we are carrying around but which cannot be read or written
75-
return std::make_shared<LittleFSFileImpl>(this, path, nullptr);
75+
return std::make_shared<LittleFSFileImpl>(this, path, nullptr, flags);
7676
} else if (rc == 0) {
77-
return std::make_shared<LittleFSFileImpl>(this, path, fd);
77+
return std::make_shared<LittleFSFileImpl>(this, path, fd, flags);
7878
} else {
7979
DEBUGV("LittleFSDirImpl::openFile: rc=%d fd=%p path=`%s` openMode=%d accessMode=%d err=%d\n",
8080
rc, fd.get(), path, openMode, accessMode, rc);

libraries/LittleFS/src/LittleFS.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ class LittleFSImpl : public FSImpl
323323
class LittleFSFileImpl : public FileImpl
324324
{
325325
public:
326-
LittleFSFileImpl(LittleFSImpl* fs, const char *name, std::shared_ptr<lfs_file_t> fd) : _fs(fs), _fd(fd), _opened(true) {
326+
LittleFSFileImpl(LittleFSImpl* fs, const char *name, std::shared_ptr<lfs_file_t> fd, int flags) : _fs(fs), _fd(fd), _opened(true), _flags(flags) {
327327
_name = std::shared_ptr<char>(new char[strlen(name) + 1], std::default_delete<char[]>());
328328
strcpy(_name.get(), name);
329329
}
@@ -419,7 +419,7 @@ class LittleFSFileImpl : public FileImpl
419419
lfs_file_close(_fs->getFS(), _getFD());
420420
_opened = false;
421421
DEBUGV("lfs_file_close: fd=%p\n", _getFD());
422-
if (timeCallback) {
422+
if (timeCallback && (_flags & LFS_O_WRONLY)) {
423423
// Add metadata with last write time
424424
time_t now = timeCallback();
425425
int rc = lfs_setattr(_fs->getFS(), _name.get(), 't', (const void *)&now, sizeof(now));
@@ -483,6 +483,7 @@ class LittleFSFileImpl : public FileImpl
483483
std::shared_ptr<lfs_file_t> _fd;
484484
std::shared_ptr<char> _name;
485485
bool _opened;
486+
int _flags;
486487
};
487488

488489
class LittleFSDirImpl : public DirImpl

0 commit comments

Comments
 (0)