Merge pull request #74830 from AThousandShips/win_time_fix

[Windows] Use `GetFileTime` for `FileAccess`
This commit is contained in:
Rémi Verschelde 2024-09-11 12:34:18 +02:00
commit 88e9af6b7c
No known key found for this signature in database
GPG Key ID: C3336907360768E1

View File

@ -412,15 +412,40 @@ uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
file = file.substr(0, file.length() - 1); file = file.substr(0, file.length() - 1);
} }
struct _stat st; HANDLE handle = CreateFileW((LPCWSTR)(file.utf16().get_data()), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
int rv = _wstat((LPCWSTR)(file.utf16().get_data()), &st);
if (rv == 0) { if (handle != INVALID_HANDLE_VALUE) {
return st.st_mtime; FILETIME ft_create, ft_write;
} else {
print_verbose("Failed to get modified time for: " + p_file + ""); bool status = GetFileTime(handle, &ft_create, nullptr, &ft_write);
return 0;
CloseHandle(handle);
if (status) {
uint64_t ret = 0;
// If write time is invalid, fallback to creation time.
if (ft_write.dwHighDateTime == 0 && ft_write.dwLowDateTime == 0) {
ret = ft_create.dwHighDateTime;
ret <<= 32;
ret |= ft_create.dwLowDateTime;
} else {
ret = ft_write.dwHighDateTime;
ret <<= 32;
ret |= ft_write.dwLowDateTime;
}
const uint64_t WINDOWS_TICKS_PER_SECOND = 10000000;
const uint64_t TICKS_TO_UNIX_EPOCH = 116444736000000000LL;
if (ret >= TICKS_TO_UNIX_EPOCH) {
return (ret - TICKS_TO_UNIX_EPOCH) / WINDOWS_TICKS_PER_SECOND;
}
}
} }
print_verbose("Failed to get modified time for: " + p_file);
return 0;
} }
BitField<FileAccess::UnixPermissionFlags> FileAccessWindows::_get_unix_permissions(const String &p_file) { BitField<FileAccess::UnixPermissionFlags> FileAccessWindows::_get_unix_permissions(const String &p_file) {