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 <david.schulz@qt.io>
This commit is contained in:
hjk
2021-07-01 11:26:59 +02:00
parent 9a35983b09
commit 9287b1a3be
2 changed files with 20 additions and 7 deletions

View File

@@ -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;
}

View File

@@ -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<FilePath> dirEntries(const QStringList &nameFilters,
QDir::Filters filters,