]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Remove win32 GetFileNameFromHandle compatibility function
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 21 Jun 2020 19:29:04 +0000 (21:29 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 22 Jun 2020 19:00:08 +0000 (21:00 +0200)
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.

(cherry picked from commit bd742fbe20493f720ae6af00525fd3a8ee829802)

cmake/GenerateConfigurationFile.cmake
cmake/config.h.in
src/Util.cpp
src/win32compat.cpp

index b698c4fc4fb2ea960d21c9b05ece08b3545488bc..0ef550728797990918abda849424549b7534970c 100644 (file)
@@ -20,7 +20,6 @@ include(CheckFunctionExists)
 set(functions
     asctime_r
     geteuid
-    GetFinalPathNameByHandleW
     getopt_long
     getpwuid
     gettimeofday
index c2e76b94e9aae2278102f0d49c83d77ad6f6b133..4eeecdd8be716850920a984db3d064c3ad2b6be4 100644 (file)
@@ -63,9 +63,6 @@
 /* Define to 1 if you have the `geteuid' function. */
 #cmakedefine HAVE_GETEUID
 
-/* Define to 1 if you have the `GetFinalPathNameByHandleW' function. */
-#cmakedefine HAVE_GETFINALPATHNAMEBYHANDLEW
-
 /* Define to 1 if you have the `getopt_long' function. */
 #cmakedefine HAVE_GETOPT_LONG
 
index a4fa05416bf155f8eb2bee8999ca4cd4c506abb7..9c12e342f3a6f99e13a58fd83598bd637d29dcf9 100644 (file)
@@ -676,13 +676,12 @@ real_path(const std::string& path, bool return_empty_on_error)
                                   FILE_ATTRIBUTE_NORMAL,
                                   NULL);
   if (INVALID_HANDLE_VALUE != path_handle) {
-#  ifdef HAVE_GETFINALPATHNAMEBYHANDLEW
-    GetFinalPathNameByHandle(
+    bool ok = GetFinalPathNameByHandle(
       path_handle, buffer, buffer_size, FILE_NAME_NORMALIZED);
-#  else
-    GetFileNameFromHandle(path_handle, buffer, buffer_size);
-#  endif
     CloseHandle(path_handle);
+    if (!ok) {
+      return path;
+    }
     resolved = buffer + 4; // Strip \\?\ from the file name.
   } else {
     snprintf(buffer, buffer_size, "%s", c_path);
index 5da56c1797c55c19c86a1e8c8be245c386980821..27d9a6bb3877a4a75845ed1c886f1e791a90b4fd 100644 (file)
 
 #ifdef _WIN32
 
-#  include <psapi.h>
-#  include <sys/locking.h>
-#  include <tchar.h>
-
 std::string
 win32_error_message(DWORD error_code)
 {
@@ -42,79 +38,4 @@ win32_error_message(DWORD error_code)
   return message;
 }
 
-#  if !defined(HAVE_REALPATH) && !defined(HAVE_GETFINALPATHNAMEBYHANDLEW)
-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
-
 #endif