Skip to content

Commit 88e9af6

Browse files
committed
Merge pull request #74830 from AThousandShips/win_time_fix
[Windows] Use `GetFileTime` for `FileAccess`
2 parents 97ef3c8 + 7139f46 commit 88e9af6

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

drivers/windows/file_access_windows.cpp

+32-7
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,40 @@ uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
412412
file = file.substr(0, file.length() - 1);
413413
}
414414

415-
struct _stat st;
416-
int rv = _wstat((LPCWSTR)(file.utf16().get_data()), &st);
415+
HANDLE handle = CreateFileW((LPCWSTR)(file.utf16().get_data()), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
417416

418-
if (rv == 0) {
419-
return st.st_mtime;
420-
} else {
421-
print_verbose("Failed to get modified time for: " + p_file + "");
422-
return 0;
417+
if (handle != INVALID_HANDLE_VALUE) {
418+
FILETIME ft_create, ft_write;
419+
420+
bool status = GetFileTime(handle, &ft_create, nullptr, &ft_write);
421+
422+
CloseHandle(handle);
423+
424+
if (status) {
425+
uint64_t ret = 0;
426+
427+
// If write time is invalid, fallback to creation time.
428+
if (ft_write.dwHighDateTime == 0 && ft_write.dwLowDateTime == 0) {
429+
ret = ft_create.dwHighDateTime;
430+
ret <<= 32;
431+
ret |= ft_create.dwLowDateTime;
432+
} else {
433+
ret = ft_write.dwHighDateTime;
434+
ret <<= 32;
435+
ret |= ft_write.dwLowDateTime;
436+
}
437+
438+
const uint64_t WINDOWS_TICKS_PER_SECOND = 10000000;
439+
const uint64_t TICKS_TO_UNIX_EPOCH = 116444736000000000LL;
440+
441+
if (ret >= TICKS_TO_UNIX_EPOCH) {
442+
return (ret - TICKS_TO_UNIX_EPOCH) / WINDOWS_TICKS_PER_SECOND;
443+
}
444+
}
423445
}
446+
447+
print_verbose("Failed to get modified time for: " + p_file);
448+
return 0;
424449
}
425450

426451
BitField<FileAccess::UnixPermissionFlags> FileAccessWindows::_get_unix_permissions(const String &p_file) {

0 commit comments

Comments
 (0)