From bd742fbe20493f720ae6af00525fd3a8ee829802 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 21 Jun 2020 21:29:04 +0200 Subject: [PATCH] Remove win32 GetFileNameFromHandle compatibility function MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The replacement function seems to be buggy, triggering failures in x_realpath at least when the unit tests are run in Wine. The real GetFinalPathNameByHandle function is allegedly supported in Windows Vista and newer, so let’s just use it. --- configure.ac | 1 - src/util.c | 92 ++++------------------------------------------------ 2 files changed, 6 insertions(+), 87 deletions(-) diff --git a/configure.ac b/configure.ac index 01098842e..8278592c8 100644 --- a/configure.ac +++ b/configure.ac @@ -177,7 +177,6 @@ fi dnl Linking on Windows needs ws2_32 if test x${windows_os} = xyes; then LIBS="$LIBS -lws2_32" - AC_CHECK_FUNCS(GetFinalPathNameByHandleW,[],[LIBS="$LIBS -lpsapi"]) fi AC_C_BIGENDIAN diff --git a/src/util.c b/src/util.c index 4aa99f424..9f014bdc2 100644 --- a/src/util.c +++ b/src/util.c @@ -1083,87 +1083,6 @@ parse_size_with_suffix(const char *str, uint64_t *size) return true; } - -#if !defined(HAVE_REALPATH) && \ - defined(_WIN32) && \ - !defined(HAVE_GETFINALPATHNAMEBYHANDLEW) -static BOOL GetFileNameFromHandle(HANDLE file_handle, TCHAR *filename, - WORD cch_filename) -{ - BOOL success = FALSE; - - // Get the file size. - DWORD file_size_hi = 0; - DWORD file_size_lo = GetFileSize(file_handle, &file_size_hi); - if (file_size_lo == 0 && file_size_hi == 0) { - // Cannot map a file with a length of zero. - return FALSE; - } - - // Create a file mapping object. - HANDLE file_map = - CreateFileMapping(file_handle, NULL, PAGE_READONLY, 0, 1, NULL); - if (!file_map) { - return FALSE; - } - - // Create a file mapping to get the file name. - void *mem = MapViewOfFile(file_map, FILE_MAP_READ, 0, 0, 1); - if (mem) { - if (GetMappedFileName(GetCurrentProcess(), - mem, - filename, - cch_filename)) { - // Translate path with device name to drive letters. - TCHAR temp[512]; - temp[0] = '\0'; - - if (GetLogicalDriveStrings(512-1, temp)) { - TCHAR name[MAX_PATH]; - TCHAR drive[3] = TEXT(" :"); - BOOL found = FALSE; - TCHAR *p = temp; - - do { - // Copy the drive letter to the template string. - *drive = *p; - - // Look up each device name. - if (QueryDosDevice(drive, name, MAX_PATH)) { - size_t name_len = _tcslen(name); - if (name_len < MAX_PATH) { - found = _tcsnicmp(filename, name, name_len) == 0 - && *(filename + name_len) == _T('\\'); - if (found) { - // Reconstruct filename using temp_file and replace device path - // with DOS path. - TCHAR temp_file[MAX_PATH]; - _sntprintf(temp_file, - MAX_PATH - 1, - TEXT("%s%s"), - drive, - filename+name_len); - strcpy(filename, temp_file); - } - } - } - - // Go to the next NULL character. - while (*p++) { - // Do nothing. - } - } while (!found && *p); // End of string. - } - } - success = TRUE; - UnmapViewOfFile(mem); - } - - CloseHandle(file_map); - return success; -} -#endif - // A sane realpath() function, trying to cope with stupid path limits and a // broken API. Caller frees. char * @@ -1183,12 +1102,13 @@ x_realpath(const char *path) path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE != path_handle) { -#ifdef HAVE_GETFINALPATHNAMEBYHANDLEW - GetFinalPathNameByHandle(path_handle, ret, maxlen, FILE_NAME_NORMALIZED); -#else - GetFileNameFromHandle(path_handle, ret, maxlen); -#endif + bool ok = GetFinalPathNameByHandle( + path_handle, ret, maxlen, FILE_NAME_NORMALIZED) != 0; CloseHandle(path_handle); + if (!ok) { + free(ret); + return x_strdup(path); + } p = ret + 4; // Strip \\?\ from the file name. } else { snprintf(ret, maxlen, "%s", path); -- 2.47.2