Skip to content

Commit

Permalink
Track creation time of LittleFS FS (#7873)
Browse files Browse the repository at this point in the history
  • Loading branch information
qistoph authored Mar 3, 2021
1 parent 22442f0 commit c90c329
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cores/esp8266/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions libraries/LittleFS/src/LittleFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,46 @@ 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();
}

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;
Expand Down

0 comments on commit c90c329

Please sign in to comment.