Skip to content

Commit c90c329

Browse files
authoredMar 3, 2021
Track creation time of LittleFS FS (#7873)
1 parent 22442f0 commit c90c329

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed
 

Diff for: ‎cores/esp8266/FS.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,13 @@ bool FS::rename(const String& pathFrom, const String& pathTo) {
441441
return rename(pathFrom.c_str(), pathTo.c_str());
442442
}
443443

444+
time_t FS::getCreationTime() {
445+
if (!_impl) {
446+
return 0;
447+
}
448+
return _impl->getCreationTime();
449+
}
450+
444451
void FS::setTimeCallback(time_t (*cb)(void)) {
445452
if (!_impl)
446453
return;

Diff for: ‎cores/esp8266/FS.h

+2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ class FS
235235
bool gc();
236236
bool check();
237237

238+
time_t getCreationTime();
239+
238240
void setTimeCallback(time_t (*cb)(void));
239241

240242
friend class ::SDClass; // More of a frenemy, but SD needs internal implementation to get private FAT bits

Diff for: ‎cores/esp8266/FSImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class FSImpl {
115115
virtual bool rmdir(const char* path) = 0;
116116
virtual bool gc() { return true; } // May not be implemented in all file systems.
117117
virtual bool check() { return true; } // May not be implemented in all file systems.
118+
virtual time_t getCreationTime() { return 0; } // May not be implemented in all file systems.
118119

119120
// Filesystems *may* support a timestamp per-file, so allow the user to override with
120121
// their own callback for all files on this FS. The default implementation simply

Diff for: ‎libraries/LittleFS/src/LittleFS.h

+33
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,46 @@ class LittleFSImpl : public FSImpl
221221
return false;
222222
}
223223

224+
if(_timeCallback && _tryMount()) {
225+
// Mounting is required to set attributes
226+
227+
time_t t = _timeCallback();
228+
rc = lfs_setattr(&_lfs, "/", 'c', &t, 8);
229+
if (rc != 0) {
230+
DEBUGV("lfs_format, lfs_setattr 'c': rc=%d\n", rc);
231+
return false;
232+
}
233+
234+
rc = lfs_setattr(&_lfs, "/", 't', &t, 8);
235+
if (rc != 0) {
236+
DEBUGV("lfs_format, lfs_setattr 't': rc=%d\n", rc);
237+
return false;
238+
}
239+
240+
lfs_unmount(&_lfs);
241+
_mounted = false;
242+
}
243+
224244
if (wasMounted) {
225245
return _tryMount();
226246
}
227247

228248
return true;
229249
}
230250

251+
time_t getCreationTime() override {
252+
time_t t;
253+
uint32_t t32b;
254+
255+
if (lfs_getattr(&_lfs, "/", 'c', &t, 8) == 8) {
256+
return t;
257+
} else if (lfs_getattr(&_lfs, "/", 'c', &t32b, 4) == 4) {
258+
return (time_t)t32b;
259+
} else {
260+
return 0;
261+
}
262+
}
263+
231264

232265
protected:
233266
friend class LittleFSFileImpl;

0 commit comments

Comments
 (0)