VFS: Implement utime()

This commit is contained in:
Roland Dobai
2018-10-25 11:53:52 +02:00
parent ada3bc3fb5
commit fb334baa7c
13 changed files with 342 additions and 2 deletions
+65
View File
@@ -649,4 +649,69 @@ TEST_CASE("mtime is updated when file is opened", "[spiffs]")
test_teardown();
}
TEST_CASE("utime() works well", "[spiffs]")
{
const char filename[] = "/spiffs/utime.txt";
struct stat achieved_stat;
struct tm desired_tm;
struct utimbuf desired_time = {
.actime = 0, // access time is not supported
.modtime = 0,
};
time_t false_now = 0;
memset(&desired_tm, 0, sizeof(struct tm));
test_setup();
{
// Setting up a false actual time - used when the file is created and for modification with the current time
desired_tm.tm_mon = 10 - 1;
desired_tm.tm_mday = 31;
desired_tm.tm_year = 2018 - 1900;
desired_tm.tm_hour = 10;
desired_tm.tm_min = 35;
desired_tm.tm_sec = 23;
false_now = mktime(&desired_tm);
struct timeval now = { .tv_sec = false_now };
settimeofday(&now, NULL);
}
test_spiffs_create_file_with_text(filename, "");
// 00:00:00. January 1st, 1900
desired_tm.tm_mon = 1 - 1;
desired_tm.tm_mday = 1;
desired_tm.tm_year = 0;
desired_tm.tm_hour = 0;
desired_tm.tm_min = 0;
desired_tm.tm_sec = 0;
printf("Testing mod. time: %s", asctime(&desired_tm));
desired_time.modtime = mktime(&desired_tm);
TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
// 23:59:08. December 31st, 2145
desired_tm.tm_mon = 12 - 1;
desired_tm.tm_mday = 31;
desired_tm.tm_year = 2145 - 1900;
desired_tm.tm_hour = 23;
desired_tm.tm_min = 59;
desired_tm.tm_sec = 8;
printf("Testing mod. time: %s", asctime(&desired_tm));
desired_time.modtime = mktime(&desired_tm);
TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
// Current time
TEST_ASSERT_EQUAL(0, utime(filename, NULL));
TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
printf("Mod. time changed to (false actual time): %s", ctime(&achieved_stat.st_mtime));
TEST_ASSERT_NOT_EQUAL(desired_time.modtime, achieved_stat.st_mtime);
TEST_ASSERT(false_now - achieved_stat.st_mtime <= 2); // two seconds of tolerance are given
test_teardown();
}
#endif // CONFIG_SPIFFS_USE_MTIME