From 3950d807717eb4bdbe71672dc0f794551a511615 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 20 Sep 2020 20:10:03 +1000 Subject: [PATCH] FileSystem: Fix GetProgramPath() sometimes returning stale paths See https://social.msdn.microsoft.com/Forums/windowshardware/en-US/4b7b3884-d0bb-4812-bc18-2078c61d4b90/queryfullprocessimagename-gives-wrong-path-for-renamed-directory?forum=windowsgeneraldevelopmentissues --- src/common/file_system.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index 6db11ee1a..64278b176 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -1114,16 +1114,13 @@ bool FileSystem::DeleteDirectory(const char* Path, bool Recursive) std::string GetProgramPath() { - const HANDLE hProcess = GetCurrentProcess(); - std::wstring buffer; buffer.resize(MAX_PATH); for (;;) { - DWORD nChars = static_cast(buffer.size()); - if (!QueryFullProcessImageNameW(GetCurrentProcess(), 0, buffer.data(), &nChars) && - GetLastError() == ERROR_INSUFFICIENT_BUFFER) + DWORD nChars = GetModuleFileNameW(nullptr, buffer.data(), static_cast(buffer.size())); + if (nChars == static_cast(buffer.size()) && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { buffer.resize(buffer.size() * 2); continue; @@ -1315,7 +1312,7 @@ bool StatFile(const char* Path, FILESYSTEM_STAT_DATA* pStatData) if (Path[0] == '\0') return false; - // stat file + // stat file #ifdef __HAIKU__ struct stat sysStatData; if (stat(Path, &sysStatData) < 0) @@ -1349,7 +1346,7 @@ bool FileExists(const char* Path) if (Path[0] == '\0') return false; - // stat file + // stat file #ifdef __HAIKU__ struct stat sysStatData; if (stat(Path, &sysStatData) < 0) @@ -1371,13 +1368,13 @@ bool DirectoryExists(const char* Path) if (Path[0] == '\0') return false; - // stat file + // stat file #ifdef __HAIKU__ - struct stat sysStatData; - if (stat(Path, &sysStatData) < 0) + struct stat sysStatData; + if (stat(Path, &sysStatData) < 0) #else - struct stat64 sysStatData; - if (stat64(Path, &sysStatData) < 0) + struct stat64 sysStatData; + if (stat64(Path, &sysStatData) < 0) #endif return false;