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

Hook up custom timestamp proc for SD/SDFS (#7686)

The SDFS implementation didn't include plumbing to support user-supplied
timestamp generators (for file create/write times) because the SDFat
library doesn't support a callback parameter.

Work around it by using a single, global (inside sdfs namespace) that
the static timestamp callback member can see and use.

Add a test of the feature in CI.

Fixes #7682
This commit is contained in:
Earle F. Philhower, III
2020-10-31 08:05:34 -07:00
committed by GitHub
parent 996211f132
commit 1bb0815fed
3 changed files with 46 additions and 4 deletions

View File

@ -37,6 +37,8 @@ FS SDFS = FS(FSImplPtr(new sdfs::SDFSImpl()));
namespace sdfs {
// Required to be global because SDFAT doesn't allow a this pointer in it's own time call
time_t (*__sdfs_timeCallback)(void) = nullptr;
FileImplPtr SDFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode)
{

View File

@ -205,12 +205,21 @@ public:
return mktime(&tiempo);
}
virtual void setTimeCallback(time_t (*cb)(void)) override {
extern time_t (*__sdfs_timeCallback)(void);
__sdfs_timeCallback = cb;
}
// 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 member of our class to return the time/date.
static void dateTimeCB(uint16_t *dosYear, uint16_t *dosTime) {
time_t now = time(nullptr);
time_t now;
extern time_t (*__sdfs_timeCallback)(void);
if (__sdfs_timeCallback) {
now = __sdfs_timeCallback();
} else {
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;