From f9bd73cd6256092c5f219374a678148013e2a104 Mon Sep 17 00:00:00 2001 From: Chris van Marle Date: Wed, 17 Feb 2021 14:34:30 +0100 Subject: [PATCH 1/3] LittleFS: add reading FS creation time --- cores/esp8266/FS.cpp | 7 +++++++ cores/esp8266/FS.h | 2 ++ cores/esp8266/FSImpl.h | 1 + libraries/LittleFS/src/LittleFS.h | 13 +++++++++++++ 4 files changed, 23 insertions(+) diff --git a/cores/esp8266/FS.cpp b/cores/esp8266/FS.cpp index a6199bb11f..9d87d037d5 100644 --- a/cores/esp8266/FS.cpp +++ b/cores/esp8266/FS.cpp @@ -441,6 +441,13 @@ bool FS::rename(const String& pathFrom, const String& pathTo) { return rename(pathFrom.c_str(), pathTo.c_str()); } +time_t FS::getCreationTime() { + if (!_impl) { + return 0; + } + return _impl->getCreationTime(); +} + void FS::setTimeCallback(time_t (*cb)(void)) { if (!_impl) return; diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index 99c71cdcea..5f420ec475 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -235,6 +235,8 @@ class FS bool gc(); bool check(); + time_t getCreationTime(); + void setTimeCallback(time_t (*cb)(void)); friend class ::SDClass; // More of a frenemy, but SD needs internal implementation to get private FAT bits diff --git a/cores/esp8266/FSImpl.h b/cores/esp8266/FSImpl.h index 801a8d9c0d..fa40e355d7 100644 --- a/cores/esp8266/FSImpl.h +++ b/cores/esp8266/FSImpl.h @@ -115,6 +115,7 @@ class FSImpl { virtual bool rmdir(const char* path) = 0; virtual bool gc() { return true; } // May not be implemented in all file systems. virtual bool check() { return true; } // May not be implemented in all file systems. + virtual time_t getCreationTime() { return -1; } // May not be implemented in all file systems. // Filesystems *may* support a timestamp per-file, so allow the user to override with // their own callback for all files on this FS. The default implementation simply diff --git a/libraries/LittleFS/src/LittleFS.h b/libraries/LittleFS/src/LittleFS.h index 0a483c9b1d..44ba4a4d83 100644 --- a/libraries/LittleFS/src/LittleFS.h +++ b/libraries/LittleFS/src/LittleFS.h @@ -228,6 +228,19 @@ class LittleFSImpl : public FSImpl return true; } + time_t getCreationTime() override { + time_t t; + uint32_t t32b; + + if (lfs_getattr(&_lfs, "/", 'c', &t, 8) == 8) { + return t; + } else if (lfs_getattr(&_lfs, "/", 'c', &t32b, 4) == 4) { + return (time_t)t32b; + } else { + return 0; + } + } + protected: friend class LittleFSFileImpl; From 869f066922def39be710a3209c4e9550019891d9 Mon Sep 17 00:00:00 2001 From: Chris van Marle Date: Wed, 17 Feb 2021 14:35:36 +0100 Subject: [PATCH 2/3] LittleFS: set create/modified FS times on format --- libraries/LittleFS/src/LittleFS.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/libraries/LittleFS/src/LittleFS.h b/libraries/LittleFS/src/LittleFS.h index 44ba4a4d83..bf77026b43 100644 --- a/libraries/LittleFS/src/LittleFS.h +++ b/libraries/LittleFS/src/LittleFS.h @@ -221,6 +221,26 @@ class LittleFSImpl : public FSImpl return false; } + if(_timeCallback && _tryMount()) { + // Mounting is required to set attributes + + time_t t = _timeCallback(); + rc = lfs_setattr(&_lfs, "/", 'c', &t, 8); + if (rc != 0) { + DEBUGV("lfs_format, lfs_setattr 'c': rc=%d\n", rc); + return false; + } + + rc = lfs_setattr(&_lfs, "/", 't', &t, 8); + if (rc != 0) { + DEBUGV("lfs_format, lfs_setattr 't': rc=%d\n", rc); + return false; + } + + lfs_unmount(&_lfs); + _mounted = false; + } + if (wasMounted) { return _tryMount(); } From cf70a56af685292f98c5d6b784903fda92ff0cce Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 2 Mar 2021 17:39:59 -0800 Subject: [PATCH 3/3] Update FSImpl.h When `FS::getCreationTime` isn't implemented in the FS, return 0 not -1, same as other getXXXtime() calls in the File object. --- cores/esp8266/FSImpl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/FSImpl.h b/cores/esp8266/FSImpl.h index fa40e355d7..fdc857bc69 100644 --- a/cores/esp8266/FSImpl.h +++ b/cores/esp8266/FSImpl.h @@ -115,7 +115,7 @@ class FSImpl { virtual bool rmdir(const char* path) = 0; virtual bool gc() { return true; } // May not be implemented in all file systems. virtual bool check() { return true; } // May not be implemented in all file systems. - virtual time_t getCreationTime() { return -1; } // May not be implemented in all file systems. + virtual time_t getCreationTime() { return 0; } // May not be implemented in all file systems. // Filesystems *may* support a timestamp per-file, so allow the user to override with // their own callback for all files on this FS. The default implementation simply