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 <david.schulz@qt.io>
This commit is contained in:
hjk
2021-07-01 08:59:32 +02:00
parent 8ed5836746
commit 92904480f0
7 changed files with 21 additions and 25 deletions

View File

@@ -197,7 +197,7 @@ bool BuildableHelperLibrary::copyFiles(const QString &sourcePath,
QString *errorMessage) QString *errorMessage)
{ {
// try remove the directory // try remove the directory
if (!FileUtils::removeRecursively(FilePath::fromString(targetDirectory), errorMessage)) if (!FilePath::fromString(targetDirectory).removeRecursively(errorMessage))
return false; return false;
if (!QDir().mkpath(targetDirectory)) { if (!QDir().mkpath(targetDirectory)) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::DebuggingHelperLibrary", "The target directory %1 could not be created.").arg(targetDirectory); *errorMessage = QCoreApplication::translate("ProjectExplorer::DebuggingHelperLibrary", "The target directory %1 could not be created.").arg(targetDirectory);

View File

@@ -76,14 +76,7 @@ static DeviceFileHooks s_deviceHooks;
*/ */
/*! static bool removeRecursivelyLocal(const FilePath &filePath, QString *error)
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)
{ {
QTC_ASSERT(!filePath.needsDevice(), return false); QTC_ASSERT(!filePath.needsDevice(), return false);
QFileInfo fileInfo = filePath.toFileInfo(); QFileInfo fileInfo = filePath.toFileInfo();
@@ -111,7 +104,7 @@ bool FileUtils::removeRecursively(const FilePath &filePath, QString *error)
const QStringList fileNames = dir.entryList( const QStringList fileNames = dir.entryList(
QDir::Files | QDir::Hidden | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot); QDir::Files | QDir::Hidden | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot);
for (const QString &fileName : fileNames) { for (const QString &fileName : fileNames) {
if (!removeRecursively(filePath / fileName, error)) if (!removeRecursivelyLocal(filePath / fileName, error))
return false; return false;
} }
if (!QDir::root().rmdir(dir.path())) { if (!QDir::root().rmdir(dir.path())) {
@@ -1443,13 +1436,20 @@ bool FilePath::removeFile() const
return QFile::remove(path()); 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()) { if (needsDevice()) {
QTC_ASSERT(s_deviceHooks.removeRecursively, return false); QTC_ASSERT(s_deviceHooks.removeRecursively, return false);
return s_deviceHooks.removeRecursively(*this); return s_deviceHooks.removeRecursively(*this);
} }
return FileUtils::removeRecursively(*this); return removeRecursivelyLocal(*this, error);
} }
bool FilePath::copyFile(const FilePath &target) const bool FilePath::copyFile(const FilePath &target) const

View File

@@ -175,7 +175,7 @@ public:
QDateTime lastModified() const; QDateTime lastModified() const;
QFile::Permissions permissions() const; QFile::Permissions permissions() const;
bool removeFile() const; bool removeFile() const;
bool removeRecursively() const; bool removeRecursively(QString *error = nullptr) const;
bool copyFile(const FilePath &target) const; bool copyFile(const FilePath &target) const;
bool renameFile(const FilePath &target) const; bool renameFile(const FilePath &target) const;
@@ -242,7 +242,6 @@ public:
}; };
#endif // QT_GUI_LIB #endif // QT_GUI_LIB
static bool removeRecursively(const FilePath &filePath, QString *error = nullptr);
static bool copyRecursively(const FilePath &srcFilePath, static bool copyRecursively(const FilePath &srcFilePath,
const FilePath &tgtFilePath, const FilePath &tgtFilePath,
QString *error = nullptr); QString *error = nullptr);

View File

@@ -137,7 +137,7 @@ void AndroidPackageInstallationStep::doRun()
FilePath androidDir = FilePath::fromString(dir); FilePath androidDir = FilePath::fromString(dir);
if (!dir.isEmpty() && androidDir.exists()) { if (!dir.isEmpty() && androidDir.exists()) {
emit addOutput(tr("Removing directory %1").arg(dir), OutputFormat::NormalMessage); emit addOutput(tr("Removing directory %1").arg(dir), OutputFormat::NormalMessage);
if (!FileUtils::removeRecursively(androidDir, &error)) { if (!androidDir.removeRecursively(&error)) {
emit addOutput(error, OutputFormat::Stderr); emit addOutput(error, OutputFormat::Stderr);
TaskHub::addTask(BuildSystemTask(Task::Error, error)); TaskHub::addTask(BuildSystemTask(Task::Error, error));
emit finished(false); emit finished(false);

View File

@@ -47,7 +47,7 @@ VirtualFileSystemOverlay::VirtualFileSystemOverlay(const QString &rootPattern)
void VirtualFileSystemOverlay::update() void VirtualFileSystemOverlay::update()
{ {
Utils::FileUtils::removeRecursively(overlayFilePath()); overlayFilePath().removeRecursively();
QFile overlayFile(m_overlayFilePath.toString()); QFile overlayFile(m_overlayFilePath.toString());
if (!overlayFile.open(QFile::ReadWrite)) if (!overlayFile.open(QFile::ReadWrite))
return; return;
@@ -61,8 +61,7 @@ void VirtualFileSystemOverlay::update()
documentRoots[doc->filePath().absolutePath()] << doc; documentRoots[doc->filePath().absolutePath()] << doc;
AutoSavedPath saved = m_saved.take(document); AutoSavedPath saved = m_saved.take(document);
if (saved.revision != document->document()->revision()) { if (saved.revision != document->document()->revision()) {
if (saved.path.exists()) saved.path.removeRecursively();
Utils::FileUtils::removeRecursively(saved.path);
saved.revision = document->document()->revision(); saved.revision = document->document()->revision();
QString error; QString error;
saved.path = Utils::FilePath::fromString(m_root.path()) saved.path = Utils::FilePath::fromString(m_root.path())
@@ -79,7 +78,7 @@ void VirtualFileSystemOverlay::update()
for (const AutoSavedPath &path : qAsConst(m_saved)) { for (const AutoSavedPath &path : qAsConst(m_saved)) {
QString error; QString error;
if (!Utils::FileUtils::removeRecursively(path.path, &error)) if (!path.path.removeRecursively(&error))
qCDebug(LOG) << error; qCDebug(LOG) << error;
} }
m_saved = newSaved; m_saved = newSaved;

View File

@@ -513,7 +513,7 @@ void CMakeBuildSystem::clearCMakeCache()
stopParsingAndClearState(); stopParsingAndClearState();
const QList<FilePath> pathsToDelete = { const FilePath pathsToDelete[] = {
m_parameters.buildDirectory / "CMakeCache.txt", m_parameters.buildDirectory / "CMakeCache.txt",
m_parameters.buildDirectory / "CMakeCache.txt.prev", m_parameters.buildDirectory / "CMakeCache.txt.prev",
m_parameters.buildDirectory / "CMakeFiles", m_parameters.buildDirectory / "CMakeFiles",
@@ -521,10 +521,8 @@ void CMakeBuildSystem::clearCMakeCache()
m_parameters.buildDirectory / ".cmake/api/v1/reply.prev" m_parameters.buildDirectory / ".cmake/api/v1/reply.prev"
}; };
for (const FilePath &path : pathsToDelete) { for (const FilePath &path : pathsToDelete)
if (path.exists()) path.removeRecursively();
FileUtils::removeRecursively(path);
}
} }
std::unique_ptr<CMakeProjectNode> CMakeBuildSystem::generateProjectTree( std::unique_ptr<CMakeProjectNode> CMakeBuildSystem::generateProjectTree(

View File

@@ -100,7 +100,7 @@ void CppToolsPlugin::initTestCase()
void CppToolsPlugin::cleanupTestCase() void CppToolsPlugin::cleanupTestCase()
{ {
Utils::FileUtils::removeRecursively(Utils::FilePath::fromString(baseTestDir())); Utils::FilePath::fromString(baseTestDir()).removeRecursively();
CppFileSettings *fs = fileSettings(); CppFileSettings *fs = fileSettings();
fs->headerSearchPaths.removeLast(); fs->headerSearchPaths.removeLast();
fs->headerSearchPaths.removeLast(); fs->headerSearchPaths.removeLast();