From 3522097a8b7df7a621800e906727b5ffef6caea6 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 13 Sep 2022 11:52:44 +0200 Subject: [PATCH] Utils: Normalize backslashes on FilePath parsing again Change-Id: I8ee0bc53244785d78acfad406664a907ab088301 Reviewed-by: Christian Stenger --- src/libs/utils/filepath.cpp | 26 +++++++++++--------- tests/auto/utils/fileutils/tst_fileutils.cpp | 7 +++--- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index 0d9c27ada6e..f1945d98023 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -809,13 +809,17 @@ void FilePath::setPath(QStringView path) m_path = path.toString(); } -void FilePath::setFromString(const QString &filename) +void FilePath::setFromString(const QString &unnormalizedFileName) { static const QLatin1String qtcDevSlash("__qtc_devices__/"); static const QLatin1String colonSlashSlash("://"); + QString fileName = unnormalizedFileName; + if (fileName.contains('\\')) + fileName.replace('\\', '/'); + const QChar slash('/'); - const QStringView fileNameView(filename); + const QStringView fileNameView(fileName); bool startsWithQtcSlashDev = false; QStringView withoutQtcDeviceRoot = fileNameView; @@ -848,23 +852,23 @@ void FilePath::setFromString(const QString &filename) m_scheme.clear(); m_host.clear(); - m_path = filename; + m_path = fileName; return; } - const int firstSlash = filename.indexOf(slash); - const int schemeEnd = filename.indexOf(colonSlashSlash); + const int firstSlash = fileName.indexOf(slash); + const int schemeEnd = fileName.indexOf(colonSlashSlash); if (schemeEnd != -1 && schemeEnd < firstSlash) { // This is a pseudo Url, we can't use QUrl here sadly. - m_scheme = filename.left(schemeEnd); - const int hostEnd = filename.indexOf(slash, schemeEnd + 3); - m_host = filename.mid(schemeEnd + 3, hostEnd - schemeEnd - 3); + m_scheme = fileName.left(schemeEnd); + const int hostEnd = fileName.indexOf(slash, schemeEnd + 3); + m_host = fileName.mid(schemeEnd + 3, hostEnd - schemeEnd - 3); if (hostEnd != -1) - setPath(QStringView(filename).mid(hostEnd)); + setPath(QStringView(fileName).mid(hostEnd)); return; } - setPath(filename); + setPath(fileName); return; } @@ -1599,7 +1603,7 @@ QString FilePath::shortNativePath() const */ bool FilePath::isRelativePath() const { - if (m_path.startsWith('/') || m_path.startsWith('\\')) + if (m_path.startsWith('/')) return false; if (m_path.size() > 1 && isWindowsDriveLetter(m_path[0]) && m_path.at(1) == ':') return false; diff --git a/tests/auto/utils/fileutils/tst_fileutils.cpp b/tests/auto/utils/fileutils/tst_fileutils.cpp index ef375913fcd..1e8f2f405ee 100644 --- a/tests/auto/utils/fileutils/tst_fileutils.cpp +++ b/tests/auto/utils/fileutils/tst_fileutils.cpp @@ -410,9 +410,9 @@ void tst_fileutils::fromString_data() QTest::newRow("unix-folder") << D("/tmp", "", "", "/tmp"); QTest::newRow("unix-folder-with-trailing-slash") << D("/tmp/", "", "", "/tmp/"); - QTest::newRow("windows-root") << D("c:", "", "", "c:/", FailEverywhere); - QTest::newRow("windows-folder") << D("c:\\Windows", "", "", "c:/Windows", FailEverywhere); - QTest::newRow("windows-folder-with-trailing-slash") << D("c:\\Windows\\", "", "", "c:/Windows\\", FailEverywhere); + QTest::newRow("windows-root") << D("c:", "", "", "c:"); + QTest::newRow("windows-folder") << D("c:\\Windows", "", "", "c:/Windows"); + QTest::newRow("windows-folder-with-trailing-slash") << D("c:\\Windows\\", "", "", "c:/Windows/"); QTest::newRow("windows-folder-slash") << D("C:/Windows", "", "", "C:/Windows"); QTest::newRow("docker-root-url") << D("docker://1234/", "docker", "1234", "/"); @@ -426,6 +426,7 @@ void tst_fileutils::fromString_data() QTest::newRow("qtc-dev-type-dev-linux") << D("/__qtc_devices__/docker/1234", "docker", "1234", "/"); QTest::newRow("qtc-dev-type-dev-win") << D("c:/__qtc_devices__/docker/1234", "docker", "1234", "/"); + // "Remote Windows" is currently truly not supported. QTest::newRow("cross-os-linux") << D("/__qtc_devices__/docker/1234/c:/test.txt", "docker", "1234", "c:/test.txt", FailEverywhere); QTest::newRow("cross-os-win")