From e1ce46eda95d2d32bde93152b29e9087f0d9fb75 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sat, 28 Dec 2019 09:13:42 -0800 Subject: [PATCH] Only update LittleFS timestamp when opened write 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. --- libraries/LittleFS/src/LittleFS.cpp | 4 ++-- libraries/LittleFS/src/LittleFS.h | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libraries/LittleFS/src/LittleFS.cpp b/libraries/LittleFS/src/LittleFS.cpp index c30bb9f73d..518ef663de 100644 --- a/libraries/LittleFS/src/LittleFS.cpp +++ b/libraries/LittleFS/src/LittleFS.cpp @@ -72,9 +72,9 @@ FileImplPtr LittleFSImpl::open(const char* path, OpenMode openMode, AccessMode a if (rc == LFS_ERR_ISDIR) { // To support the SD.openNextFile, a null FD indicates to the LittleFSFile this is just // a directory whose name we are carrying around but which cannot be read or written - return std::make_shared(this, path, nullptr); + return std::make_shared(this, path, nullptr, flags); } else if (rc == 0) { - return std::make_shared(this, path, fd); + return std::make_shared(this, path, fd, flags); } else { DEBUGV("LittleFSDirImpl::openFile: rc=%d fd=%p path=`%s` openMode=%d accessMode=%d err=%d\n", rc, fd.get(), path, openMode, accessMode, rc); diff --git a/libraries/LittleFS/src/LittleFS.h b/libraries/LittleFS/src/LittleFS.h index 2a35869eda..d7cfc4bbee 100644 --- a/libraries/LittleFS/src/LittleFS.h +++ b/libraries/LittleFS/src/LittleFS.h @@ -323,7 +323,7 @@ class LittleFSImpl : public FSImpl class LittleFSFileImpl : public FileImpl { public: - LittleFSFileImpl(LittleFSImpl* fs, const char *name, std::shared_ptr fd) : _fs(fs), _fd(fd), _opened(true) { + LittleFSFileImpl(LittleFSImpl* fs, const char *name, std::shared_ptr fd, int flags) : _fs(fs), _fd(fd), _opened(true), _flags(flags) { _name = std::shared_ptr(new char[strlen(name) + 1], std::default_delete()); strcpy(_name.get(), name); } @@ -419,7 +419,7 @@ class LittleFSFileImpl : public FileImpl lfs_file_close(_fs->getFS(), _getFD()); _opened = false; DEBUGV("lfs_file_close: fd=%p\n", _getFD()); - if (timeCallback) { + if (timeCallback && (_flags & LFS_O_WRONLY)) { // Add metadata with last write time time_t now = timeCallback(); int rc = lfs_setattr(_fs->getFS(), _name.get(), 't', (const void *)&now, sizeof(now)); @@ -483,6 +483,7 @@ class LittleFSFileImpl : public FileImpl std::shared_ptr _fd; std::shared_ptr _name; bool _opened; + int _flags; }; class LittleFSDirImpl : public DirImpl