1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

Add fileCreation/getCreation create-time accessors (#7000)

* Add fileCreation/getCreation create-time accessors

For SDFS and LittleFS, enable a creation time accessor for files and Dir
iterators, similar to the existing fileTime/getLastWrite calls.

Remove spurious Dir::getLastWrite method (the proper and only documented
way is really Dir::fileTime).

Update json to point to new mklittlefs which copies the creation date of
files to the image.

Fixes #6992

* Remove malloc(), use stack vars for temp names

LFS filenames are limited in size and generally very small.  Use a stack
variable instead of a dynamic allocation when performing full-path
computations.

* Replace "Creation" w/"CreationTime" in FS accessor

Per review, `getCreation` -> `getCreationTime`, `fileCreation` ->
`fileCreationTime`.

The names `fileTime()` and `getLastWrite()` are inherited from ESP32
implementation and unchanged.

* Add creation time to listfiles SD example

* Enable SdFat's sateTime callback for timestamping

SdFat requries the dateTimeCallback call (global for everything) to
update dates and times on created files.

Because the callback signature doesn't have space for us to provide
any parameters, we cannot get the the File, Dir, or FS object's
dateTimeCB member.  Instead, just go with `time(null)` as the callback
function which is right in all but the most esoteric cases.

* Correct DOS year/month offset in dateTime callback

* Fix docs to match new xxxCreationTime() API names

Co-authored-by: Develo <deveyes@gmail.com>
This commit is contained in:
Earle F. Philhower, III
2020-02-22 12:49:08 -08:00
committed by GitHub
parent 4eca62cb53
commit bea64dfa69
10 changed files with 176 additions and 83 deletions

View File

@ -156,6 +156,7 @@ public:
format();
_mounted = _fs.begin(_cfg._csPin, _cfg._spiSettings);
}
sdfat::SdFile::dateTimeCallback(dateTimeCB);
return _mounted;
}
@ -204,6 +205,17 @@ public:
return mktime(&tiempo);
}
// Because SdFat has a single, global setting for this we can only use a
// static member of our class to return the time/date. However, since
// this is static, we can't see the time callback variable. Punt for now,
// using time(NULL) as the best we can do.
static void dateTimeCB(uint16_t *dosYear, uint16_t *dosTime) {
time_t now = time(nullptr);
struct tm *tiempo = localtime(&now);
*dosYear = ((tiempo->tm_year - 80) << 9) | ((tiempo->tm_mon + 1) << 5) | tiempo->tm_mday;
*dosTime = (tiempo->tm_hour << 11) | (tiempo->tm_min << 5) | tiempo->tm_sec;
}
protected:
friend class SDFileImpl;
friend class SDFSDirImpl;
@ -363,6 +375,18 @@ public:
return ftime;
}
time_t getCreationTime() override {
time_t ftime = 0;
if (_opened && _fd) {
sdfat::dir_t tmp;
if (_fd.get()->dirEntry(&tmp)) {
ftime = SDFSImpl::FatToTimeT(tmp.creationDate, tmp.creationTime);
}
}
return ftime;
}
protected:
SDFSImpl* _fs;
@ -426,6 +450,14 @@ public:
return _time;
}
time_t fileCreationTime() override
{
if (!_valid) {
return 0;
}
return _creation;
}
bool isFile() const override
{
@ -451,8 +483,10 @@ public:
sdfat::dir_t tmp;
if (file.dirEntry(&tmp)) {
_time = SDFSImpl::FatToTimeT(tmp.lastWriteDate, tmp.lastWriteTime);
_creation = SDFSImpl::FatToTimeT(tmp.creationDate, tmp.creationTime);
} else {
_time = 0;
_creation = 0;
}
file.getName(_lfn, sizeof(_lfn));
file.close();
@ -477,6 +511,7 @@ protected:
bool _valid;
char _lfn[64];
time_t _time;
time_t _creation;
std::shared_ptr<char> _dirPath;
uint32_t _size;
bool _isFile;