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

Rationalize File timestamp callback (#7785)

Fixes #7775

Clean up the passing/setting of custom File time callbacks and add a
host test verifying they work.  Existing core was not passing custom
timeCallbacks set at the FS level down to open()ed files, resulting in
them calling the default time(nullptr) and reporting wrong file modify
times.
This commit is contained in:
Earle F. Philhower, III
2020-12-22 01:41:11 -08:00
committed by GitHub
parent f1dc6e90ee
commit 35d22edeec
7 changed files with 58 additions and 52 deletions

View File

@ -162,35 +162,4 @@ TEST_CASE("SD.h FILE_WRITE macro is append", "[fs]")
REQUIRE(u == 0);
}
// SDFS timestamp setter (#7682)
static time_t _my_time(void)
{
struct tm t;
bzero(&t, sizeof(t));
t.tm_year = 120;
t.tm_mon = 9;
t.tm_mday = 22;
t.tm_hour = 12;
t.tm_min = 13;
t.tm_sec = 14;
return mktime(&t);
}
TEST_CASE("SDFS timeCallback")
{
SDFS_MOCK_DECLARE(64, 8, 512, "");
REQUIRE(SDFS.begin());
REQUIRE(SD.begin(4));
SDFS.setTimeCallback(_my_time);
File f = SD.open("/file.txt", "w");
f.write("Had we but world enough, and time,");
f.close();
time_t expected = _my_time();
f = SD.open("/file.txt", "r");
REQUIRE(abs(f.getCreationTime() - expected) < 60); // FAT has less precision in timestamp than time_t
REQUIRE(abs(f.getLastWrite() - expected) < 60); // FAT has less precision in timestamp than time_t
f.close();
}
};

View File

@ -220,6 +220,40 @@ TEST_CASE(TESTPRE "Rewriting file frees space immediately (#7426)", TESTPAT)
}
}
#if FSTYPE != SPIFFS
// Timestamp setter (#7682, #7775)
static time_t _my_time(void)
{
struct tm t;
bzero(&t, sizeof(t));
t.tm_year = 120;
t.tm_mon = 9;
t.tm_mday = 22;
t.tm_hour = 12;
t.tm_min = 13;
t.tm_sec = 14;
return mktime(&t);
}
TEST_CASE("Verify timeCallback works properly")
{
FS_MOCK_DECLARE(64, 8, 512, "");
REQUIRE(FSTYPE.begin());
FSTYPE.setTimeCallback(_my_time);
File f = FSTYPE.open("/file.txt", "w");
f.write("Had we but world enough, and time,");
f.close();
time_t expected = _my_time();
f = FSTYPE.open("/file.txt", "r");
REQUIRE(abs(f.getCreationTime() - expected) < 60); // FAT has less precision in timestamp than time_t
REQUIRE(abs(f.getLastWrite() - expected) < 60); // FAT has less precision in timestamp than time_t
f.close();
}
#endif
#ifdef FS_HAS_DIRS
#if FSTYPE != SDFS