From 92904480f0a8d696b3f0dcd46353accb64a44cee Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 1 Jul 2021 08:59:32 +0200 Subject: [PATCH] Utils: Merge FileUtils::removeRecursively() into FilePath This simplify the interface by removing a possibly wrong choice ensures it works also on remote paths. Change-Id: I01e198958900a91b99dcf2dbb491a593485493ba Reviewed-by: David Schulz --- src/libs/utils/buildablehelperlibrary.cpp | 2 +- src/libs/utils/fileutils.cpp | 22 +++++++++---------- src/libs/utils/fileutils.h | 3 +-- .../androidpackageinstallationstep.cpp | 2 +- .../clangtools/virtualfilesystemoverlay.cpp | 7 +++--- .../cmakeprojectmanager/cmakebuildsystem.cpp | 8 +++---- src/plugins/cpptools/cppheadersource_test.cpp | 2 +- 7 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/libs/utils/buildablehelperlibrary.cpp b/src/libs/utils/buildablehelperlibrary.cpp index f08e7347459..7a2f504af3d 100644 --- a/src/libs/utils/buildablehelperlibrary.cpp +++ b/src/libs/utils/buildablehelperlibrary.cpp @@ -197,7 +197,7 @@ bool BuildableHelperLibrary::copyFiles(const QString &sourcePath, QString *errorMessage) { // try remove the directory - if (!FileUtils::removeRecursively(FilePath::fromString(targetDirectory), errorMessage)) + if (!FilePath::fromString(targetDirectory).removeRecursively(errorMessage)) return false; if (!QDir().mkpath(targetDirectory)) { *errorMessage = QCoreApplication::translate("ProjectExplorer::DebuggingHelperLibrary", "The target directory %1 could not be created.").arg(targetDirectory); diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index 1f32637cbf2..374f495dfd2 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -76,14 +76,7 @@ static DeviceFileHooks s_deviceHooks; */ -/*! - Removes the directory \a filePath and its subdirectories recursively. - - \note The \a error parameter is optional. - - Returns whether the operation succeeded. -*/ -bool FileUtils::removeRecursively(const FilePath &filePath, QString *error) +static bool removeRecursivelyLocal(const FilePath &filePath, QString *error) { QTC_ASSERT(!filePath.needsDevice(), return false); QFileInfo fileInfo = filePath.toFileInfo(); @@ -111,7 +104,7 @@ bool FileUtils::removeRecursively(const FilePath &filePath, QString *error) const QStringList fileNames = dir.entryList( QDir::Files | QDir::Hidden | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot); for (const QString &fileName : fileNames) { - if (!removeRecursively(filePath / fileName, error)) + if (!removeRecursivelyLocal(filePath / fileName, error)) return false; } if (!QDir::root().rmdir(dir.path())) { @@ -1443,13 +1436,20 @@ bool FilePath::removeFile() const return QFile::remove(path()); } -bool FilePath::removeRecursively() const +/*! + Removes the directory this filePath refers too and its subdirectories recursively. + + \note The \a error parameter is optional. + + Returns whether the operation succeeded. +*/ +bool FilePath::removeRecursively(QString *error) const { if (needsDevice()) { QTC_ASSERT(s_deviceHooks.removeRecursively, return false); return s_deviceHooks.removeRecursively(*this); } - return FileUtils::removeRecursively(*this); + return removeRecursivelyLocal(*this, error); } bool FilePath::copyFile(const FilePath &target) const diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h index 96c27dc9d00..ae77ac507db 100644 --- a/src/libs/utils/fileutils.h +++ b/src/libs/utils/fileutils.h @@ -175,7 +175,7 @@ public: QDateTime lastModified() const; QFile::Permissions permissions() const; bool removeFile() const; - bool removeRecursively() const; + bool removeRecursively(QString *error = nullptr) const; bool copyFile(const FilePath &target) const; bool renameFile(const FilePath &target) const; @@ -242,7 +242,6 @@ public: }; #endif // QT_GUI_LIB - static bool removeRecursively(const FilePath &filePath, QString *error = nullptr); static bool copyRecursively(const FilePath &srcFilePath, const FilePath &tgtFilePath, QString *error = nullptr); diff --git a/src/plugins/android/androidpackageinstallationstep.cpp b/src/plugins/android/androidpackageinstallationstep.cpp index bb196834dd3..d8247de83c5 100644 --- a/src/plugins/android/androidpackageinstallationstep.cpp +++ b/src/plugins/android/androidpackageinstallationstep.cpp @@ -137,7 +137,7 @@ void AndroidPackageInstallationStep::doRun() FilePath androidDir = FilePath::fromString(dir); if (!dir.isEmpty() && androidDir.exists()) { emit addOutput(tr("Removing directory %1").arg(dir), OutputFormat::NormalMessage); - if (!FileUtils::removeRecursively(androidDir, &error)) { + if (!androidDir.removeRecursively(&error)) { emit addOutput(error, OutputFormat::Stderr); TaskHub::addTask(BuildSystemTask(Task::Error, error)); emit finished(false); diff --git a/src/plugins/clangtools/virtualfilesystemoverlay.cpp b/src/plugins/clangtools/virtualfilesystemoverlay.cpp index faf43db05bf..357f8eec40e 100644 --- a/src/plugins/clangtools/virtualfilesystemoverlay.cpp +++ b/src/plugins/clangtools/virtualfilesystemoverlay.cpp @@ -47,7 +47,7 @@ VirtualFileSystemOverlay::VirtualFileSystemOverlay(const QString &rootPattern) void VirtualFileSystemOverlay::update() { - Utils::FileUtils::removeRecursively(overlayFilePath()); + overlayFilePath().removeRecursively(); QFile overlayFile(m_overlayFilePath.toString()); if (!overlayFile.open(QFile::ReadWrite)) return; @@ -61,8 +61,7 @@ void VirtualFileSystemOverlay::update() documentRoots[doc->filePath().absolutePath()] << doc; AutoSavedPath saved = m_saved.take(document); if (saved.revision != document->document()->revision()) { - if (saved.path.exists()) - Utils::FileUtils::removeRecursively(saved.path); + saved.path.removeRecursively(); saved.revision = document->document()->revision(); QString error; saved.path = Utils::FilePath::fromString(m_root.path()) @@ -79,7 +78,7 @@ void VirtualFileSystemOverlay::update() for (const AutoSavedPath &path : qAsConst(m_saved)) { QString error; - if (!Utils::FileUtils::removeRecursively(path.path, &error)) + if (!path.path.removeRecursively(&error)) qCDebug(LOG) << error; } m_saved = newSaved; diff --git a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp index e1b38dfc86d..2ec4e68e6c3 100644 --- a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp +++ b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp @@ -513,7 +513,7 @@ void CMakeBuildSystem::clearCMakeCache() stopParsingAndClearState(); - const QList pathsToDelete = { + const FilePath pathsToDelete[] = { m_parameters.buildDirectory / "CMakeCache.txt", m_parameters.buildDirectory / "CMakeCache.txt.prev", m_parameters.buildDirectory / "CMakeFiles", @@ -521,10 +521,8 @@ void CMakeBuildSystem::clearCMakeCache() m_parameters.buildDirectory / ".cmake/api/v1/reply.prev" }; - for (const FilePath &path : pathsToDelete) { - if (path.exists()) - FileUtils::removeRecursively(path); - } + for (const FilePath &path : pathsToDelete) + path.removeRecursively(); } std::unique_ptr CMakeBuildSystem::generateProjectTree( diff --git a/src/plugins/cpptools/cppheadersource_test.cpp b/src/plugins/cpptools/cppheadersource_test.cpp index 37244f4fba2..be0bdd03c2e 100644 --- a/src/plugins/cpptools/cppheadersource_test.cpp +++ b/src/plugins/cpptools/cppheadersource_test.cpp @@ -100,7 +100,7 @@ void CppToolsPlugin::initTestCase() void CppToolsPlugin::cleanupTestCase() { - Utils::FileUtils::removeRecursively(Utils::FilePath::fromString(baseTestDir())); + Utils::FilePath::fromString(baseTestDir()).removeRecursively(); CppFileSettings *fs = fileSettings(); fs->headerSearchPaths.removeLast(); fs->headerSearchPaths.removeLast();