[RFC][PATCH] Change to buffer lookup for given temporary files

Enrico Forestieri forenr at lyx.org
Sun Feb 23 10:40:04 UTC 2020


On Sun, Feb 23, 2020 at 12:35:53AM +0100, Enrico Forestieri wrote:
> On Tue, Feb 18, 2020 at 07:55:13PM +0100, Enrico Forestieri wrote:
> > 
> > Still, why realPath() returns a short path name in one case and not in the
> > other case remains a mystery.
> 
> Mystery solved. On Windows, our implementation of realPath() works only with
> file names but does not work with directory names. On the other hand,
> QFileInfo::canonicalFilePath() does not resolve junctions (directory
> symlinks).

Dropping support for Windows Xp, the attached patch works for me with
both files and directories. I am going to apply it, as I think it is
not controversial, given that MS dropped support for Win7 these year
(while we still support both WinVista and Win7).

-- 
Enrico
-------------- next part --------------
diff --git a/src/support/os.cpp b/src/support/os.cpp
index 7e8fb8af57..616b9c6936 100644
--- a/src/support/os.cpp
+++ b/src/support/os.cpp
@@ -11,6 +11,10 @@
 
 #include <config.h>
 
+#ifdef _WIN32
+# define _WIN32_WINNT 0x0600
+#endif
+
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/filetools.h"
diff --git a/src/support/os_win32.cpp b/src/support/os_win32.cpp
index e15af13acf..b57ddba9b0 100644
--- a/src/support/os_win32.cpp
+++ b/src/support/os_win32.cpp
@@ -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();
diff --git a/src/support/os_win32.h b/src/support/os_win32.h
index 42016f7094..6f92b90abb 100644
--- a/src/support/os_win32.h
+++ b/src/support/os_win32.h
@@ -36,13 +36,13 @@
  */
 #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>


More information about the lyx-devel mailing list