From 9287b1a3be7d8fc85036738d0503ec9fd66945c5 Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 1 Jul 2021 11:26:59 +0200 Subject: [PATCH] Utils: Introduce FilePath::is{Relative,Absolute}Path To operate correctly with remote target files systems that do not match the host OS. Change-Id: Ia4ea284dc38399deacb50410c9618e1e139f4e13 Reviewed-by: David Schulz --- src/libs/utils/fileutils.cpp | 24 +++++++++++++++++------- src/libs/utils/fileutils.h | 3 +++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index 8ea7cedf584..9e68b91853d 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -358,22 +358,32 @@ QString FileUtils::normalizePathName(const QString &name) #endif } -bool FileUtils::isRelativePath(const QString &path) +static bool isRelativePathHelper(const QString &path, OsType osType) { - if (path.startsWith(QLatin1Char('/'))) + if (path.startsWith('/')) return false; - if (HostOsInfo::isWindowsHost()) { - if (path.startsWith(QLatin1Char('\\'))) + if (osType == OsType::OsTypeWindows) { + if (path.startsWith('\\')) return false; // Unlike QFileInfo, this won't accept a relative path with a drive letter. // Such paths result in a royal mess anyway ... - if (path.length() >= 3 && path.at(1) == QLatin1Char(':') && path.at(0).isLetter() - && (path.at(2) == QLatin1Char('/') || path.at(2) == QLatin1Char('\\'))) + if (path.length() >= 3 && path.at(1) == ':' && path.at(0).isLetter() + && (path.at(2) == '/' || path.at(2) == '\\')) return false; } return true; } +bool FileUtils::isRelativePath(const QString &path) +{ + return isRelativePathHelper(path, HostOsInfo::hostOs()); +} + +bool FilePath::isRelativePath() const +{ + return isRelativePathHelper(m_data, osType()); +} + FilePath FilePath::resolvePath(const QString &fileName) const { if (fileName.isEmpty()) @@ -1055,7 +1065,7 @@ FilePath FilePath::absoluteFilePath() const FilePath FilePath::absoluteFilePath(const FilePath &tail) const { - if (FileUtils::isRelativePath(tail.m_data)) + if (isRelativePathHelper(tail.m_data, osType())) return pathAppended(tail.m_data); return tail; } diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h index b08974009fe..ab429a706b1 100644 --- a/src/libs/utils/fileutils.h +++ b/src/libs/utils/fileutils.h @@ -143,6 +143,9 @@ public: bool isExecutableFile() const; bool isReadableFile() const; bool isReadableDir() const; + bool isRelativePath() const; + bool isAbsolutePath() const { return !isRelativePath(); } + bool createDir() const; QList dirEntries(const QStringList &nameFilters, QDir::Filters filters,