From 6bc7d7c69851bda16949b82c9159dc75d6c25b76 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Wed, 14 Jun 2023 02:19:57 +0200 Subject: [PATCH] fs::utime: fallback to 01/01/1980 00:00:00 if the date is too low --- Utilities/File.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index 4d1d9acd2a..58951da672 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -1072,7 +1072,23 @@ bool fs::utime(const std::string& path, s64 atime, s64 mtime) FILETIME _mtime = from_time(mtime); if (!SetFileTime(handle, nullptr, &_atime, &_mtime)) { - g_tls_error = to_error(GetLastError()); + const DWORD last_error = GetLastError(); + g_tls_error = to_error(last_error); + + // Some filesystems fail to set a date lower than 01/01/1980 00:00:00 + if (last_error == ERROR_INVALID_PARAMETER && (atime < 315532800 || mtime < 315532800)) + { + // Try again with 01/01/1980 00:00:00 + _atime = from_time(std::max(atime, 315532800)); + _mtime = from_time(std::max(mtime, 315532800)); + + if (SetFileTime(handle, nullptr, &_atime, &_mtime)) + { + CloseHandle(handle); + return true; + } + } + CloseHandle(handle); return false; }