diff --git a/main.cpp b/main.cpp index d8a6aae..1883bce 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,16 @@ 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); + 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; uint8_t data_byte; while (left > 0) { @@ -369,6 +386,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); + 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; + times.actime = times.modtime = meta; + utime(destPath, ×); + #endif return true; }