From d014293f6d93a34ea021ce287030a1f575f403c3 Mon Sep 17 00:00:00 2001 From: Hannah von Reth Date: Tue, 5 Jan 2021 17:43:31 +0100 Subject: [PATCH 1/3] Ensure pathtoUNC is called with an absolute path --- src/common/filesystembase.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/common/filesystembase.cpp b/src/common/filesystembase.cpp index 9c3c33f30bec2..8bbca4922cfa1 100644 --- a/src/common/filesystembase.cpp +++ b/src/common/filesystembase.cpp @@ -18,6 +18,7 @@ #include "filesystembase.h" #include "utility.h" +#include "common/asserts.h" #include #include @@ -509,6 +510,7 @@ bool FileSystem::isJunction(const QString &filename) #ifdef Q_OS_WIN QString FileSystem::pathtoUNC(const QString &_str) { + ASSERT(QFileInfo(_str).isAbsolute()); if (_str.isEmpty()) { return _str; } @@ -522,6 +524,7 @@ QString FileSystem::pathtoUNC(const QString &_str) // prepend \\?\ and to support long names if (str.at(0) == sep) { + // should not happen as we require the path to be absolute return QStringLiteral(R"(\\?)") + str; } return QStringLiteral(R"(\\?\)") + str; From 03182ea714be4c6fc5c4e16dd193d268e11cd1d0 Mon Sep 17 00:00:00 2001 From: Hannah von Reth Date: Wed, 17 Feb 2021 12:06:05 +0100 Subject: [PATCH 2/3] Use longWinPath in more places --- src/common/filesystembase.cpp | 16 +++++----------- src/common/utility.cpp | 3 ++- src/libsync/owncloudpropagator.cpp | 4 ++-- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/common/filesystembase.cpp b/src/common/filesystembase.cpp index 8bbca4922cfa1..5eb9dbd29804d 100644 --- a/src/common/filesystembase.cpp +++ b/src/common/filesystembase.cpp @@ -205,14 +205,8 @@ bool FileSystem::uncheckedRenameReplace(const QString &originFileName, (wchar_t *)dest.utf16(), MOVEFILE_REPLACE_EXISTING + MOVEFILE_COPY_ALLOWED + MOVEFILE_WRITE_THROUGH); if (!ok) { - wchar_t *string = 0; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, - nullptr, ::GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR)&string, 0, nullptr); - - *errorString = QString::fromWCharArray(string); + *errorString = Utility::formatWinError(GetLastError()); qCWarning(lcFileSystem) << "Renaming temp file to final failed: " << *errorString; - LocalFree((HLOCAL)string); return false; } #endif @@ -449,13 +443,13 @@ bool FileSystem::moveToTrash(const QString &fileName, QString *errorString) bool FileSystem::isFileLocked(const QString &fileName) { #ifdef Q_OS_WIN - const wchar_t *wuri = reinterpret_cast(fileName.utf16()); // Check if file exists - DWORD attr = GetFileAttributesW(wuri); + const QString fName = longWinPath(fileName); + DWORD attr = GetFileAttributesW(reinterpret_cast(fName.utf16())); if (attr != INVALID_FILE_ATTRIBUTES) { // Try to open the file with as much access as possible.. HANDLE win_h = CreateFileW( - wuri, + reinterpret_cast(fName.utf16()), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, @@ -493,7 +487,7 @@ bool FileSystem::isJunction(const QString &filename) { #ifdef Q_OS_WIN WIN32_FIND_DATA findData; - HANDLE hFind = FindFirstFileEx((const wchar_t *)filename.utf16(), FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, 0); + HANDLE hFind = FindFirstFileEx(reinterpret_cast(longWinPath(filename).utf16()), FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, 0); if (hFind != INVALID_HANDLE_VALUE) { FindClose(hFind); return false; diff --git a/src/common/utility.cpp b/src/common/utility.cpp index c9b0a81fd8c9e..e31534ca3808e 100644 --- a/src/common/utility.cpp +++ b/src/common/utility.cpp @@ -19,6 +19,7 @@ #include "config.h" #include "common/utility.h" +#include "common/filesystembase.h" #include "version.h" // Note: This file must compile without QtGui @@ -229,7 +230,7 @@ qint64 Utility::freeDiskSpace(const QString &path) #elif defined(Q_OS_WIN) ULARGE_INTEGER freeBytes; freeBytes.QuadPart = 0L; - if (GetDiskFreeSpaceEx(reinterpret_cast(path.utf16()), &freeBytes, nullptr, nullptr)) { + if (GetDiskFreeSpaceEx(reinterpret_cast(FileSystem::longWinPath(path).utf16()), &freeBytes, nullptr, nullptr)) { return freeBytes.QuadPart; } #endif diff --git a/src/libsync/owncloudpropagator.cpp b/src/libsync/owncloudpropagator.cpp index 4c35a3956fa9b..dcc5dc7d6c191 100644 --- a/src/libsync/owncloudpropagator.cpp +++ b/src/libsync/owncloudpropagator.cpp @@ -548,7 +548,7 @@ bool OwncloudPropagator::localFileNameClash(const QString &relFile) WIN32_FIND_DATA FindFileData; HANDLE hFind; - hFind = FindFirstFileW((wchar_t *)file.utf16(), &FindFileData); + hFind = FindFirstFileW(reinterpret_cast(FileSystem::longWinPath(file).utf16()), &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { // returns false. } else { @@ -582,7 +582,7 @@ bool OwncloudPropagator::hasCaseClashAccessibilityProblem(const QString &relfile WIN32_FIND_DATA FindFileData; HANDLE hFind; - hFind = FindFirstFileW(reinterpret_cast(file.utf16()), &FindFileData); + hFind = FindFirstFileW(reinterpret_cast(FileSystem::longWinPath(file).utf16()), &FindFileData); if (hFind != INVALID_HANDLE_VALUE) { QString firstFile = QString::fromWCharArray(FindFileData.cFileName); if (FindNextFile(hFind, &FindFileData)) { From 22c634935bcadcf100cb9eef1e60ed5f501c7eb3 Mon Sep 17 00:00:00 2001 From: Hannah von Reth Date: Thu, 18 Mar 2021 18:14:01 +0100 Subject: [PATCH 3/3] Run expansive assert only in dev builds --- src/common/filesystembase.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/filesystembase.cpp b/src/common/filesystembase.cpp index 5eb9dbd29804d..78c94fe77c9f3 100644 --- a/src/common/filesystembase.cpp +++ b/src/common/filesystembase.cpp @@ -504,7 +504,7 @@ bool FileSystem::isJunction(const QString &filename) #ifdef Q_OS_WIN QString FileSystem::pathtoUNC(const QString &_str) { - ASSERT(QFileInfo(_str).isAbsolute()); + Q_ASSERT(QFileInfo(_str).isAbsolute()); if (_str.isEmpty()) { return _str; }