mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-16 00:43:00 +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:
committed by
david gauchard
parent
b4c28e74d6
commit
72dd589599
@ -47,11 +47,8 @@ class LittleFSDirImpl;
|
||||
class LittleFSConfig : public FSConfig
|
||||
{
|
||||
public:
|
||||
LittleFSConfig(bool autoFormat = true) {
|
||||
_type = LittleFSConfig::fsid::FSId;
|
||||
_autoFormat = autoFormat;
|
||||
}
|
||||
enum fsid { FSId = 0x4c495454 };
|
||||
static constexpr uint32_t FSId = 0x4c495454;
|
||||
LittleFSConfig(bool autoFormat = true) : FSConfig(FSId, autoFormat) { }
|
||||
};
|
||||
|
||||
class LittleFSImpl : public FSImpl
|
||||
@ -176,7 +173,7 @@ public:
|
||||
}
|
||||
|
||||
bool setConfig(const FSConfig &cfg) override {
|
||||
if ((cfg._type != LittleFSConfig::fsid::FSId) || _mounted) {
|
||||
if ((cfg._type != LittleFSConfig::FSId) || _mounted) {
|
||||
return false;
|
||||
}
|
||||
_cfg = *static_cast<const LittleFSConfig *>(&cfg);
|
||||
@ -422,9 +419,27 @@ public:
|
||||
lfs_file_close(_fs->getFS(), _getFD());
|
||||
_opened = false;
|
||||
DEBUGV("lfs_file_close: fd=%p\n", _getFD());
|
||||
if (timeCallback) {
|
||||
// Add metadata with last write time
|
||||
time_t now = timeCallback();
|
||||
int rc = lfs_setattr(_fs->getFS(), _name.get(), 't', (const void *)&now, sizeof(now));
|
||||
if (rc < 0) {
|
||||
DEBUGV("Unable to set time on '%s' to %d\n", _name.get(), now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
time_t getLastWrite() override {
|
||||
time_t ftime = 0;
|
||||
if (_opened && _fd) {
|
||||
int rc = lfs_getattr(_fs->getFS(), _name.get(), 't', (void *)&ftime, sizeof(ftime));
|
||||
if (rc != sizeof(ftime))
|
||||
ftime = 0; // Error, so clear read value
|
||||
}
|
||||
return ftime;
|
||||
}
|
||||
|
||||
const char* name() const override {
|
||||
if (!_opened) {
|
||||
return nullptr;
|
||||
@ -520,6 +535,27 @@ public:
|
||||
return _dirent.size;
|
||||
}
|
||||
|
||||
time_t fileTime() override {
|
||||
if (!_valid) {
|
||||
return 0;
|
||||
}
|
||||
int nameLen = 3; // Slashes, terminator
|
||||
nameLen += _dirPath.get() ? strlen(_dirPath.get()) : 0;
|
||||
nameLen += strlen(_dirent.name);
|
||||
char *tmpName = (char*)malloc(nameLen);
|
||||
if (!tmpName) {
|
||||
return 0;
|
||||
}
|
||||
snprintf(tmpName, nameLen, "%s%s%s", _dirPath.get() ? _dirPath.get() : "", _dirPath.get()&&_dirPath.get()[0]?"/":"", _dirent.name);
|
||||
time_t ftime = 0;
|
||||
int rc = lfs_getattr(_fs->getFS(), tmpName, 't', (void *)&ftime, sizeof(ftime));
|
||||
if (rc != sizeof(ftime))
|
||||
ftime = 0; // Error, so clear read value
|
||||
free(tmpName);
|
||||
return ftime;
|
||||
}
|
||||
|
||||
|
||||
bool isFile() const override {
|
||||
return _valid && (_dirent.type == LFS_TYPE_REG);
|
||||
}
|
||||
|
Reference in New Issue
Block a user