aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Jenssen <tim.jenssen@qt.io>2023-12-21 12:43:08 +0100
committerTim Jenssen <tim.jenssen@qt.io>2023-12-21 12:57:43 +0000
commit9c34d664097b60b5d6c380478dcad9e2fd2d6c6e (patch)
tree86fe6a14e35dac8e16f96172288e156bfc62f12c
parent0034ade5d63f8c56a181d6b4549e41a45adc3490 (diff)
Utils: add more removeRecursively checks
Change-Id: I0b8bbe99132157cc5a73355c9b396201ec259aaa Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
-rw-r--r--src/libs/utils/devicefileaccess.cpp49
1 files changed, 40 insertions, 9 deletions
diff --git a/src/libs/utils/devicefileaccess.cpp b/src/libs/utils/devicefileaccess.cpp
index 82e6a24a71..6feda42327 100644
--- a/src/libs/utils/devicefileaccess.cpp
+++ b/src/libs/utils/devicefileaccess.cpp
@@ -33,6 +33,7 @@
#include <qplatformdefs.h>
#endif
+#include <QStandardPaths>
#include <algorithm>
#include <array>
@@ -561,6 +562,44 @@ bool DesktopDeviceFileAccess::removeFile(const FilePath &filePath) const
return QFile::remove(filePath.path());
}
+static bool checkToRefuseRemoveStandardLocationDirectory(const QString &dirPath,
+ QStandardPaths::StandardLocation location,
+ QString *error)
+{
+ if (QStandardPaths::standardLocations(location).contains(dirPath)) {
+ if (error) {
+ *error = Tr::tr("Refusing to remove your %1 directory.").arg(
+ QStandardPaths::displayName(location));
+ }
+ return false;
+ }
+ return true;
+}
+
+static bool checkToRefuseRemoveDirectory(const QDir &dir, QString *error)
+{
+ if (dir.isRoot()) {
+ if (error)
+ *error = Tr::tr("Refusing to remove root directory.");
+ return false;
+ }
+ const QString dirPath = dir.path();
+ if (dirPath == QDir::home().canonicalPath()) {
+ if (error)
+ *error = Tr::tr("Refusing to remove your home directory.");
+ return false;
+ }
+ if (checkToRefuseRemoveStandardLocationDirectory(dirPath, QStandardPaths::DocumentsLocation, error))
+ return false;
+ if (checkToRefuseRemoveStandardLocationDirectory(dirPath, QStandardPaths::DownloadLocation, error))
+ return false;
+ if (checkToRefuseRemoveStandardLocationDirectory(dirPath, QStandardPaths::AppDataLocation, error))
+ return false;
+ if (checkToRefuseRemoveStandardLocationDirectory(dirPath, QStandardPaths::AppLocalDataLocation, error))
+ return false;
+ return true;
+}
+
bool DesktopDeviceFileAccess::removeRecursively(const FilePath &filePath, QString *error) const
{
QTC_ASSERT(!filePath.needsDevice(), return false);
@@ -573,16 +612,8 @@ bool DesktopDeviceFileAccess::removeRecursively(const FilePath &filePath, QStrin
if (fileInfo.isDir()) {
QDir dir(fileInfo.absoluteFilePath());
dir.setPath(dir.canonicalPath());
- if (dir.isRoot()) {
- if (error)
- *error = Tr::tr("Refusing to remove root directory.");
- return false;
- }
- if (dir.path() == QDir::home().canonicalPath()) {
- if (error)
- *error = Tr::tr("Refusing to remove your home directory.");
+ if (checkToRefuseRemoveDirectory(dir, error))
return false;
- }
const QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden | QDir::System
| QDir::Dirs | QDir::NoDotAndDotDot);