1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-26 07:02:15 +03:00

Add time to filesystem API (#6544)

* Add time to filesystem API

Support the ESP32 File::getLastWrite() call and setting the time on
all filesystems automatically (assuming the system clock has
been set properly and time(NULL) returns the proper time!).

Adds Dir::fileTime() to get the time of a file being listed, similar to
Dir::fileName() and Dir::fileSize().

Adds ::setTimeCallback(time_t (*cb)()) to File, Dir, and FS, allowing
users to override the default timestamp on a per-file, directory, or
filesystem basis. By default, a simple callback returning time(nullptr)
is implemented.

LittleFS uses the 't' attribute and should be backwards compatible.

SD/SDFS work and include wrappers for obsolete SdFat timestamp callbacks
using the MSDOS time.

This PR does not update SPIFFS, due to compatability concerns and a
possible massive rewrite which would make it possible to determine if an
old-style ot metadata enabled FS is present at mount time.

Includes an updated SD/listfiles and LittleFS_time example.

Replaces #6315

* Add links to new mklittlefs w/timestamp support

Include the update mklittlefs which generated 't' metadata on imported
files.
	../tools/sdk/lwip2/include/netif/lowpan6_opts.h

* Add explicit note about timestamp being local time

* Address review concerns

Clean up some awkward object instantiations.

Remove the _enableTime flag/setter from SPIFFS.

Clean up the FSConfig constructors using C++ style init lists.
This commit is contained in:
Earle F. Philhower, III
2019-10-31 06:09:52 -07:00
committed by david gauchard
parent b4c28e74d6
commit 72dd589599
12 changed files with 576 additions and 114 deletions

View File

@ -45,22 +45,9 @@ class SDFSDirImpl;
class SDFSConfig : public FSConfig
{
public:
SDFSConfig() {
_type = SDFSConfig::fsid::FSId;
_autoFormat = false;
_csPin = 4;
_spiSettings = SD_SCK_MHZ(10);
_part = 0;
}
SDFSConfig(uint8_t csPin, SPISettings spi) {
_type = SDFSConfig::fsid::FSId;
_autoFormat = false;
_csPin = csPin;
_spiSettings = spi;
_part = 0;
}
static constexpr uint32_t FSId = 0x53444653;
enum fsid { FSId = 0x53444653 };
SDFSConfig(uint8_t csPin = 4, SPISettings spi = SD_SCK_MHZ(10)) : FSConfig(FSId, false), _csPin(csPin), _part(0), _spiSettings(spi) { }
SDFSConfig setAutoFormat(bool val = true) {
_autoFormat = val;
@ -152,7 +139,7 @@ public:
bool setConfig(const FSConfig &cfg) override
{
if ((cfg._type != SDFSConfig::fsid::FSId) || _mounted) {
if ((cfg._type != SDFSConfig::FSId) || _mounted) {
DEBUGV("SDFS::setConfig: invalid config or already mounted\n");
return false;
}
@ -203,6 +190,20 @@ public:
return (clusterSize() * totalClusters());
}
// Helper function, takes FAT and makes standard time_t
static time_t FatToTimeT(uint16_t d, uint16_t t) {
struct tm tiempo;
memset(&tiempo, 0, sizeof(tiempo));
tiempo.tm_sec = (((int)t) << 1) & 0x3e;
tiempo.tm_min = (((int)t) >> 5) & 0x3f;
tiempo.tm_hour = (((int)t) >> 11) & 0x1f;
tiempo.tm_mday = (int)(d & 0x1f);
tiempo.tm_mon = ((int)(d >> 5) & 0x0f) - 1;
tiempo.tm_year = ((int)(d >> 9) & 0x7f) + 80;
tiempo.tm_isdst = -1;
return mktime(&tiempo);
}
protected:
friend class SDFileImpl;
friend class SDFSDirImpl;
@ -212,6 +213,7 @@ protected:
return &_fs;
}
static uint8_t _getFlags(OpenMode openMode, AccessMode accessMode) {
uint8_t mode = 0;
if (openMode & OM_CREATE) {
@ -350,6 +352,17 @@ public:
return _opened ? _fd->isDirectory() : false;
}
time_t getLastWrite() override {
time_t ftime = 0;
if (_opened && _fd) {
sdfat::dir_t tmp;
if (_fd.get()->dirEntry(&tmp)) {
ftime = SDFSImpl::FatToTimeT(tmp.lastWriteDate, tmp.lastWriteTime);
}
}
return ftime;
}
protected:
SDFSImpl* _fs;
@ -425,6 +438,12 @@ public:
_size = file.fileSize();
_isFile = file.isFile();
_isDirectory = file.isDirectory();
sdfat::dir_t tmp;
if (file.dirEntry(&tmp)) {
_time = SDFSImpl::FatToTimeT(tmp.lastWriteDate, tmp.lastWriteTime);
} else {
_time = 0;
}
file.getName(_lfn, sizeof(_lfn));
file.close();
} else {
@ -447,6 +466,7 @@ protected:
std::shared_ptr<sdfat::File> _dir;
bool _valid;
char _lfn[64];
time_t _time;
std::shared_ptr<char> _dirPath;
uint32_t _size;
bool _isFile;