]> git.lyx.org Git - lyx.git/commitdiff
Fix FileName::realPath() on Windows
authorEnrico Forestieri <forenr@lyx.org>
Sun, 23 Feb 2020 21:48:35 +0000 (22:48 +0100)
committerEnrico Forestieri <forenr@lyx.org>
Sun, 23 Feb 2020 21:59:03 +0000 (22:59 +0100)
The realPath() implementation on Windows works only for files and
not for directories. By using an API available starting from Vista
it is possible to fix it in a simple way.

I also took into account using the Qt QFileInfo::canonicalFilePath(),
but it turns out to not work when a path component is a junction
(tested with Qt 5.14.1).

Due to this, it is not possible compiling or using LyX on Windows
versions earlier than Vista.

README
src/support/os.cpp
src/support/os_win32.cpp
src/support/os_win32.h

diff --git a/README b/README
index dfc57d323213ec4d23ceebe17afb613809c5b8d8..222a9932a5c01c4921698dfec8cc89e0d94ef2a4 100644 (file)
--- a/README
+++ b/README
@@ -27,7 +27,7 @@ What do I need to run LyX?
 
     Either:
     * a Unix-like system (including Windows with Cygwin)
-    * Windows 2000 or newer
+    * Windows Vista or newer
     * Mac OS 10.4 or newer
 
     A decent LaTeX2e installation (e.g. TeX Live for Linux, MikTeX for
index 7e8fb8af57bf21e2b61a3338ca019778d219e2f3..616b9c6936d4a16594f239fbb2dc1d2332db1358 100644 (file)
 
 #include <config.h>
 
+#ifdef _WIN32
+# define _WIN32_WINNT 0x0600
+#endif
+
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/filetools.h"
index e15af13acfbdd0fd0f6f4d241bf712fa6f3d9d66..82e57d2eb69adfe1a72074505bae21856b58016b 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <io.h>
 #include <direct.h> // _getdrive
+#include <fileapi.h> // GetFinalPathNameByHandle
 #include <shlobj.h>  // SHGetFolderPath
 #include <windef.h>
 #include <shellapi.h>
@@ -594,45 +595,18 @@ string real_path(string const & path)
        // See http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx
        QString const qpath = get_long_path(toqstr(path));
        HANDLE hpath = CreateFileW((wchar_t *) qpath.utf16(), GENERIC_READ,
-                               FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
+                               FILE_SHARE_READ, NULL, OPEN_EXISTING,
+                               FILE_FLAG_BACKUP_SEMANTICS, NULL);
 
        if (hpath == INVALID_HANDLE_VALUE) {
                // The file cannot be accessed.
                return path;
        }
 
-       // Get the file size.
-       DWORD size_hi = 0;
-       DWORD size_lo = GetFileSize(hpath, &size_hi);
-
-       if (size_lo == 0 && size_hi == 0) {
-               // A zero-length file cannot be mapped.
-               CloseHandle(hpath);
-               return path;
-       }
-
-       // Create a file mapping object.
-       HANDLE hmap = CreateFileMapping(hpath, NULL, PAGE_READONLY, 0, 1, NULL);
-
-       if (!hmap) {
-               CloseHandle(hpath);
-               return path;
-       }
-
-       // Create a file mapping to get the file name.
-       void * pmem = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 1);
-
-       if (!pmem) {
-               CloseHandle(hmap);
-               CloseHandle(hpath);
-               return path;
-       }
-
        TCHAR realpath[MAX_PATH + 1];
 
-       if (!GetMappedFileName(GetCurrentProcess(), pmem, realpath, MAX_PATH)) {
-               UnmapViewOfFile(pmem);
-               CloseHandle(hmap);
+       DWORD size = GetFinalPathNameByHandle(hpath, realpath, MAX_PATH, VOLUME_NAME_NT);
+       if (size > MAX_PATH) {
                CloseHandle(hpath);
                return path;
        }
@@ -678,8 +652,6 @@ string real_path(string const & path)
                        while (*p++) ;
                } while (!found && *p);
        }
-       UnmapViewOfFile(pmem);
-       CloseHandle(hmap);
        CloseHandle(hpath);
        string const retpath = subst(string(realpath), '\\', '/');
        return FileName::fromFilesystemEncoding(retpath).absFileName();
index 42016f7094eb7f33abe56afd7fb77b4fdcd8d5ee..6f92b90abbd28ddca89fe1fe6b9e1816208525d7 100644 (file)
  */
 #if defined(__MINGW32__)  || defined(__CYGWIN__) || defined(__CYGWIN32__)
 # if defined(WINVER)
-#  if WINVER < 0x0500
-#   error WINVER must be >= 0x0500
+#  if WINVER < 0x0600
+#   error WINVER must be >= 0x0600
 #  endif
 # else
-#  define WINVER 0x0500
+#  define WINVER 0x0600
 # endif
-# define _WIN32_IE 0x0500
+# define _WIN32_IE 0x0600
 #endif
 
 #include <windows.h>