From 1073f8988684f69febe05e49b0bae9f73b58f1e5 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 2 Sep 2022 15:25:41 +0200 Subject: [PATCH] Utils: Try harder in FilePath::canonicalPath() On Windows, we now also resolve SUBST drives and NTFS alias mounts. Task-number: QTCREATORBUG-27869 Task-number: QTCREATORBUG-28031 Change-Id: Ia6b16787e8a691118d56579c0825bb20b066f9b8 Reviewed-by: Eike Ziller Reviewed-by: David Schulz --- src/libs/utils/filepath.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index a48849e2098..4f955f6b1d8 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -24,6 +24,8 @@ #ifdef QTCREATOR_PCH_H #define CALLBACK WINAPI #endif +#include +#include #include #include #endif @@ -1532,6 +1534,7 @@ FilePath FilePath::resolveSymlinks() const /*! * \brief Recursively resolves possibly present symlinks in this file name. +* On Windows, also resolves SUBST and re-mounted NTFS drives. * Unlike QFileInfo::canonicalFilePath(), this function will not return an empty * string if path doesn't exist. * @@ -1543,10 +1546,26 @@ FilePath FilePath::canonicalPath() const // FIXME: Not a full solution, but it stays on the right device. return *this; } + +#ifdef Q_OS_WINDOWS + QFile f(toString()); + if (f.open(QIODevice::ReadOnly)) { + TCHAR normalizedPath[MAX_PATH]; + const auto length = GetFinalPathNameByHandleW( + reinterpret_cast(_get_osfhandle(f.handle())), + normalizedPath, + MAX_PATH, + FILE_NAME_NORMALIZED); + if (length > 0) + return fromUserInput(QString::fromStdWString(std::wstring(normalizedPath, length))); + } +#endif + const QString result = toFileInfo().canonicalFilePath(); - if (result.isEmpty()) - return *this; - return FilePath::fromString(result); + if (!result.isEmpty()) + return fromString(result); + + return *this; } FilePath FilePath::operator/(const QString &str) const