From 324e92417859949b67a7ed4e32c4e2ed6ece734b Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Thu, 10 Aug 2023 06:44:12 +0200 Subject: [PATCH] Utils: Fix FilePath::isRootPath() Change-Id: I287bae74469ba501ecb03d51f04f7aaa5f4a7268 Reviewed-by: hjk --- src/libs/utils/filepath.cpp | 32 ++++++++++++++++---- tests/auto/utils/filepath/tst_filepath.cpp | 35 ++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index 049ae325bd8..3995ddde3a7 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -204,7 +204,27 @@ FilePath FilePath::currentWorkingPath() bool FilePath::isRootPath() const { - // FIXME: Make host-independent + if (needsDevice()) { + QStringView path = pathView(); + if (osType() != OsTypeWindows) + return path == QLatin1String("/"); + + // Remote windows paths look like this: "/c:/", so we remove the leading '/' + if (path.startsWith('/')) + path = path.mid(1); + + if (path.length() > 3) + return false; + + if (!startsWithDriveLetter()) + return false; + + if (path.length() == 3 && path[2] != QLatin1Char('/')) + return false; + + return true; + } + return *this == FilePath::fromString(QDir::rootPath()); } @@ -1346,15 +1366,15 @@ bool FilePath::contains(const QString &s) const /*! \brief Checks whether the FilePath starts with a drive letter. - - Defaults to \c false if it is a non-Windows host or represents a path on device - Returns whether FilePath starts with a drive letter */ bool FilePath::startsWithDriveLetter() const { - const QStringView p = pathView(); - return !needsDevice() && p.size() >= 2 && isWindowsDriveLetter(p[0]) && p.at(1) == ':'; + QStringView p = pathView(); + if (needsDevice() && !p.isEmpty()) + p = p.mid(1); + + return p.size() >= 2 && isWindowsDriveLetter(p[0]) && p.at(1) == ':'; } /*! diff --git a/tests/auto/utils/filepath/tst_filepath.cpp b/tests/auto/utils/filepath/tst_filepath.cpp index f9e10d5e207..6a269af7712 100644 --- a/tests/auto/utils/filepath/tst_filepath.cpp +++ b/tests/auto/utils/filepath/tst_filepath.cpp @@ -115,6 +115,8 @@ private slots: void sort(); void sort_data(); + void isRootPath(); + private: QTemporaryDir tempDir; QString rootPath; @@ -1308,6 +1310,14 @@ void tst_filepath::startsWithDriveLetter_data() QTest::newRow("simple-win") << FilePath::fromString("c:/a") << true; QTest::newRow("simple-linux") << FilePath::fromString("/c:/a") << false; QTest::newRow("relative") << FilePath("a/b") << false; + + QTest::newRow("remote-slash") << FilePath::fromString("docker://1234/") << false; + QTest::newRow("remote-single-letter") << FilePath::fromString("docker://1234/c") << false; + QTest::newRow("remote-drive") << FilePath::fromString("docker://1234/c:") << true; + QTest::newRow("remote-invalid-drive") << FilePath::fromString("docker://1234/c:a") << true; + QTest::newRow("remote-with-path") << FilePath::fromString("docker://1234/c:/a") << true; + QTest::newRow("remote-z") << FilePath::fromString("docker://1234/z:") << true; + QTest::newRow("remote-1") << FilePath::fromString("docker://1234/1:") << false; } void tst_filepath::startsWithDriveLetter() @@ -1656,6 +1666,31 @@ void tst_filepath::sort() QCOMPARE(sortedPaths, sorted); } +void tst_filepath::isRootPath() +{ + FilePath localRoot = FilePath::fromString(QDir::rootPath()); + QVERIFY(localRoot.isRootPath()); + + FilePath localNonRoot = FilePath::fromString(QDir::rootPath() + "x"); + QVERIFY(!localNonRoot.isRootPath()); + + if (HostOsInfo::isWindowsHost()) { + FilePath remoteWindowsRoot = FilePath::fromString("device://test/c:/"); + QVERIFY(remoteWindowsRoot.isRootPath()); + + FilePath remoteWindowsRoot1 = FilePath::fromString("device://test/c:"); + QVERIFY(remoteWindowsRoot1.isRootPath()); + + FilePath remoteWindowsNotRoot = FilePath::fromString("device://test/c:/x"); + QVERIFY(!remoteWindowsNotRoot.isRootPath()); + } else { + FilePath remoteRoot = FilePath::fromString("device://test/"); + QVERIFY(remoteRoot.isRootPath()); + + FilePath remotePath = FilePath::fromString("device://test/x"); + QVERIFY(!remotePath.isRootPath()); + } +} void tst_filepath::sort_data() { QTest::addColumn("input");