From 16d9570cc2b6e63f8f1de49c9759a099241fa455 Mon Sep 17 00:00:00 2001 From: Ewan Parker <34030942+ewpa@users.noreply.github.com> Date: Mon, 20 May 2019 18:55:25 +0100 Subject: [PATCH 1/2] Support reading and writing ESP32 file timestamps stored in the metadata bytes by using CONFIG_SPIFFS_USE_MTIME=1 and CONFIG_SPIFFS_META_LENGTH=4 compile flags. --- main.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/main.cpp b/main.cpp index d8a6aae..d8dbb21 100644 --- a/main.cpp +++ b/main.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -25,6 +26,12 @@ #include #endif +#if CONFIG_SPIFFS_USE_MTIME +#if SPIFFS_OBJ_META_LEN != 4 +#error You must set both CONFIG_SPIFFS_USE_MTIME=1 and CONFIG_SPIFFS_META_LENGTH=4 for ESP IDF MTIME compatibility. +#endif +#endif + static std::vector s_flashmem; static std::string s_dirName; @@ -154,6 +161,13 @@ int addFile(char* name, const char* path) std::cout << "file size: " << size << std::endl; } + // read and update file modification time + #if CONFIG_SPIFFS_USE_MTIME + struct stat sbuff; + stat(path, &sbuff); + SPIFFS_fupdate_meta(&s_fs, dst, &sbuff.st_mtime); + #endif + size_t left = size; uint8_t data_byte; while (left > 0) { @@ -369,6 +383,15 @@ bool unpackFile(spiffs_dirent *spiffsFile, const char *destPath) // Close file. fclose(dst); + // Update file modification time. + #if CONFIG_SPIFFS_USE_MTIME + spiffs_stat sstat; + SPIFFS_stat(&s_fs, (const char*)spiffsFile->name, &sstat); + struct utimbuf times; + memcpy(×.actime, sstat.meta, 4); + memcpy(×.modtime, sstat.meta, 4); + utime(destPath, ×); + #endif return true; } From b593dc706a22585e217ecbbabbcd6e1a3fec5b4d Mon Sep 17 00:00:00 2001 From: Ewan Parker <34030942+ewpa@users.noreply.github.com> Date: Wed, 22 May 2019 21:55:06 +0100 Subject: [PATCH 2/2] When interchanging file modified timestamps to and from the host system and the SPIFFS image file, take into account that the word size and endianness of the host architecture may be different to that stored in the image file. --- main.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index d8dbb21..1883bce 100644 --- a/main.cpp +++ b/main.cpp @@ -165,7 +165,10 @@ int addFile(char* name, const char* path) #if CONFIG_SPIFFS_USE_MTIME struct stat sbuff; stat(path, &sbuff); - SPIFFS_fupdate_meta(&s_fs, dst, &sbuff.st_mtime); + uint8_t meta[4]; // mtime is stored in the meta character array in little-endian order + meta[0] = sbuff.st_mtime & 0xFF; meta[1] = (sbuff.st_mtime>>8) & 0xFF; + meta[2] = (sbuff.st_mtime>>16) & 0xFF; meta[3] = (sbuff.st_mtime>>24) & 0xFF; + SPIFFS_fupdate_meta(&s_fs, dst, meta); #endif size_t left = size; @@ -387,9 +390,9 @@ bool unpackFile(spiffs_dirent *spiffsFile, const char *destPath) #if CONFIG_SPIFFS_USE_MTIME spiffs_stat sstat; SPIFFS_stat(&s_fs, (const char*)spiffsFile->name, &sstat); + uint32_t meta = (sstat.meta[0]<<0) | (sstat.meta[1]<<8) | (sstat.meta[2]<<16) | (sstat.meta[3]<<24); // mtime is retrieved from the meta character array in little-endian order struct utimbuf times; - memcpy(×.actime, sstat.meta, 4); - memcpy(×.modtime, sstat.meta, 4); + times.actime = times.modtime = meta; utime(destPath, ×); #endif