Utils: Make FileName::canonicalPath a member

Change-Id: I8d7450dec5c4c14ae3e007d1d66f1a9c3c98f807
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2019-05-17 13:44:09 +02:00
parent 22cd81ffbc
commit 36daca80f3
5 changed files with 12 additions and 13 deletions

View File

@@ -234,17 +234,17 @@ FileName FileUtils::resolveSymlinks(const FileName &path)
} }
/*! /*!
Recursively resolves possibly present symlinks in \a filePath. Recursively resolves possibly present symlinks in this file name.
Unlike QFileInfo::canonicalFilePath(), this function will not return an empty Unlike QFileInfo::canonicalFilePath(), this function will not return an empty
string if path doesn't exist. string if path doesn't exist.
Returns the canonical path. Returns the canonical path.
*/ */
FileName FileUtils::canonicalPath(const FileName &path) FileName FileName::canonicalPath() const
{ {
const QString result = path.toFileInfo().canonicalFilePath(); const QString result = toFileInfo().canonicalFilePath();
if (result.isEmpty()) if (result.isEmpty())
return path; return *this;
return FileName::fromString(result); return FileName::fromString(result);
} }
@@ -356,7 +356,7 @@ FileName FileUtils::commonPath(const FileName &oldCommonPath, const FileName &fi
FileName newCommonPath = oldCommonPath; FileName newCommonPath = oldCommonPath;
while (!newCommonPath.isEmpty() && !fileName.isChildOf(newCommonPath)) while (!newCommonPath.isEmpty() && !fileName.isChildOf(newCommonPath))
newCommonPath = newCommonPath.parentDir(); newCommonPath = newCommonPath.parentDir();
return canonicalPath(newCommonPath); return newCommonPath.canonicalPath();
} }
// Copied from qfilesystemengine_win.cpp // Copied from qfilesystemengine_win.cpp

View File

@@ -102,6 +102,8 @@ public:
FileName pathAppended(const QString &str) const; FileName pathAppended(const QString &str) const;
FileName stringAppended(const QString &str) const; FileName stringAppended(const QString &str) const;
FileName canonicalPath() const;
void clear() { m_data.clear(); } void clear() { m_data.clear(); }
bool isEmpty() const { return m_data.isEmpty(); } bool isEmpty() const { return m_data.isEmpty(); }
@@ -129,7 +131,6 @@ public:
const std::function<bool (QFileInfo, QFileInfo, QString *)> &copyHelper = nullptr); const std::function<bool (QFileInfo, QFileInfo, QString *)> &copyHelper = nullptr);
static bool isFileNewerThan(const FileName &filePath, const QDateTime &timeStamp); static bool isFileNewerThan(const FileName &filePath, const QDateTime &timeStamp);
static FileName resolveSymlinks(const FileName &path); static FileName resolveSymlinks(const FileName &path);
static FileName canonicalPath(const FileName &path);
static QString shortNativePath(const FileName &path); static QString shortNativePath(const FileName &path);
static QString fileSystemFriendlyName(const QString &name); static QString fileSystemFriendlyName(const QString &name);
static int indexOfQmakeUnfriendly(const QString &name, int startpos = 0); static int indexOfQmakeUnfriendly(const QString &name, int startpos = 0);

View File

@@ -248,7 +248,7 @@ QList<void *> CMakeProjectImporter::examineDirectory(const Utils::FileName &impo
} }
const auto homeDir const auto homeDir
= Utils::FileName::fromUserInput(QString::fromUtf8(CMakeConfigItem::valueOf("CMAKE_HOME_DIRECTORY", config))); = Utils::FileName::fromUserInput(QString::fromUtf8(CMakeConfigItem::valueOf("CMAKE_HOME_DIRECTORY", config)));
const Utils::FileName canonicalProjectDirectory = Utils::FileUtils::canonicalPath(projectDirectory()); const Utils::FileName canonicalProjectDirectory = projectDirectory().canonicalPath();
if (homeDir != canonicalProjectDirectory) { if (homeDir != canonicalProjectDirectory) {
qCDebug(cmInputLog()) << "Wrong source directory:" << homeDir.toUserOutput() qCDebug(cmInputLog()) << "Wrong source directory:" << homeDir.toUserOutput()
<< "expected:" << canonicalProjectDirectory.toUserOutput(); << "expected:" << canonicalProjectDirectory.toUserOutput();

View File

@@ -250,8 +250,8 @@ CMakeConfig TeaLeafReader::takeParsedConfiguration()
const FileName sourceOfBuildDir const FileName sourceOfBuildDir
= FileName::fromUtf8(CMakeConfigItem::valueOf("CMAKE_HOME_DIRECTORY", result)); = FileName::fromUtf8(CMakeConfigItem::valueOf("CMAKE_HOME_DIRECTORY", result));
const FileName canonicalSourceOfBuildDir = FileUtils::canonicalPath(sourceOfBuildDir); const FileName canonicalSourceOfBuildDir = sourceOfBuildDir.canonicalPath();
const FileName canonicalSourceDirectory = FileUtils::canonicalPath(m_parameters.sourceDirectory); const FileName canonicalSourceDirectory = m_parameters.sourceDirectory.canonicalPath();
if (canonicalSourceOfBuildDir != canonicalSourceDirectory) { // Uses case-insensitive compare where appropriate if (canonicalSourceOfBuildDir != canonicalSourceDirectory) { // Uses case-insensitive compare where appropriate
emit errorOccured(tr("The build directory is not for %1 but for %2") emit errorOccured(tr("The build directory is not for %1 but for %2")
.arg(canonicalSourceOfBuildDir.toUserOutput(), .arg(canonicalSourceOfBuildDir.toUserOutput(),

View File

@@ -181,10 +181,8 @@ Utils::FileName jsonObjectFilename(const QJsonObject &object)
const QString workingDir = QDir::fromNativeSeparators(object["directory"].toString()); const QString workingDir = QDir::fromNativeSeparators(object["directory"].toString());
Utils::FileName fileName = Utils::FileName::fromString( Utils::FileName fileName = Utils::FileName::fromString(
QDir::fromNativeSeparators(object["file"].toString())); QDir::fromNativeSeparators(object["file"].toString()));
if (fileName.toFileInfo().isRelative()) { if (fileName.toFileInfo().isRelative())
fileName = Utils::FileUtils::canonicalPath( fileName = Utils::FileName::fromString(workingDir + "/" + fileName.toString()).canonicalPath();
Utils::FileName::fromString(workingDir + "/" + fileName.toString()));
}
return fileName; return fileName;
} }