From a8b9452f336b666d5a927a3fb3fd22da49edcf51 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Tue, 5 Jul 2022 18:13:20 -0400 Subject: [PATCH] kernel32: Use fstat in GetFileSize (#10) Improves mwldeppc performance significantly. Before: real 0m30.322s user 0m7.800s sys 0m22.495s After: real 0m16.679s user 0m4.693s sys 0m11.965s Wine (for comparison): real 0m24.625s user 0m0.048s sys 0m0.034s --- kernel32.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/kernel32.cpp b/kernel32.cpp index 8acdcb7..e0afc73 100644 --- a/kernel32.cpp +++ b/kernel32.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace kernel32 { @@ -442,19 +443,18 @@ namespace kernel32 { */ unsigned int WIN_FUNC GetFileSize(void *hFile, unsigned int *lpFileSizeHigh) { DEBUG_LOG("GetFileSize\n"); + struct stat64 st; FILE *fp = files::fpFromHandle(hFile); - long pos = ftell(fp); - assert(pos >= 0); - int r = fseek(fp, 0L, SEEK_END); - assert(r == 0); - long sz = ftell(fp); - assert(sz >= 0); - fseek(fp, pos, SEEK_SET); - if (lpFileSizeHigh) { - *lpFileSizeHigh = 0; + fflush(fp); + if (fstat64(fileno(fp), &st) == -1 || !S_ISREG(st.st_mode)) { + wibo::lastError = 2; // ERROR_FILE_NOT_FOUND (?) + return ~0u; // INVALID_FILE_SIZE } - DEBUG_LOG("-> %d\n", (int)sz); - return (unsigned int)sz; + DEBUG_LOG("-> %ld\n", st.st_size); + if (lpFileSizeHigh != nullptr) { + *lpFileSizeHigh = st.st_size >> 32; + } + return st.st_size; } struct FILETIME {