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..fdc857bc69 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 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 diff --git a/libraries/LittleFS/src/LittleFS.h b/libraries/LittleFS/src/LittleFS.h index 0a483c9b1d..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(); } @@ -228,6 +248,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;