QDir::removeRecursively(): Retry file deletion with write permission set.
On Windows, having read-only files in a directory can cause removal to fail. When file deletion fails, check on the permissions, set write permissions and retry. Split apart code paths by OS in tst_QDir::removeRecursivelyFailure(); deletion of the read-only directory on UNIX should still fail. Change-Id: I36e54be5229a7b552e90fd5f42722b868fa0b6ee Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
parent
d6553d2cd8
commit
26bcc0565f
@ -1503,11 +1503,19 @@ bool QDir::removeRecursively()
|
||||
while (di.hasNext()) {
|
||||
di.next();
|
||||
const QFileInfo& fi = di.fileInfo();
|
||||
const QString &filePath = di.filePath();
|
||||
bool ok;
|
||||
if (fi.isDir() && !fi.isSymLink())
|
||||
ok = QDir(di.filePath()).removeRecursively(); // recursive
|
||||
else
|
||||
ok = QFile::remove(di.filePath());
|
||||
if (fi.isDir() && !fi.isSymLink()) {
|
||||
ok = QDir(filePath).removeRecursively(); // recursive
|
||||
} else {
|
||||
ok = QFile::remove(filePath);
|
||||
if (!ok) { // Read-only files prevent directory deletion on Windows, retry with Write permission.
|
||||
const QFile::Permissions permissions = QFile::permissions(filePath);
|
||||
if (!(permissions & QFile::WriteUser))
|
||||
ok = QFile::setPermissions(filePath, permissions | QFile::WriteUser)
|
||||
&& QFile::remove(filePath);
|
||||
}
|
||||
}
|
||||
if (!ok)
|
||||
success = false;
|
||||
}
|
||||
|
@ -470,21 +470,22 @@ void tst_QDir::removeRecursivelyFailure()
|
||||
#ifdef Q_OS_UNIX
|
||||
QFile dirAsFile(path); // yay, I have to use QFile to change a dir's permissions...
|
||||
QVERIFY(dirAsFile.setPermissions(QFile::Permissions(0))); // no permissions
|
||||
#else
|
||||
QVERIFY(file.setPermissions(QFile::ReadOwner));
|
||||
#endif
|
||||
|
||||
QVERIFY(!QDir().rmdir(path));
|
||||
QDir dir(path);
|
||||
QVERIFY(!dir.removeRecursively()); // didn't work
|
||||
QVERIFY(dir.exists()); // still exists
|
||||
|
||||
#ifdef Q_OS_UNIX
|
||||
QVERIFY(dirAsFile.setPermissions(QFile::Permissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner)));
|
||||
#else
|
||||
QVERIFY(file.setPermissions(QFile::ReadOwner | QFile::WriteOwner));
|
||||
#endif
|
||||
QVERIFY(dir.removeRecursively());
|
||||
QVERIFY(!dir.exists());
|
||||
#else // Q_OS_UNIX
|
||||
QVERIFY(file.setPermissions(QFile::ReadOwner));
|
||||
QVERIFY(!QDir().rmdir(path));
|
||||
QDir dir(path);
|
||||
QVERIFY(dir.removeRecursively());
|
||||
QVERIFY(!dir.exists());
|
||||
#endif // !Q_OS_UNIX
|
||||
}
|
||||
|
||||
void tst_QDir::removeRecursivelySymlink()
|
||||
|
@ -215,6 +215,8 @@ void tst_QTemporaryDir::autoRemove()
|
||||
QFile file(dirName + "/dir1/file");
|
||||
QVERIFY(file.open(QIODevice::WriteOnly));
|
||||
QCOMPARE(file.write("Hello"), 5LL);
|
||||
file.close();
|
||||
QVERIFY(file.setPermissions(QFile::ReadUser));
|
||||
}
|
||||
#ifdef Q_OS_WIN
|
||||
QTRY_VERIFY(!QDir(dirName).exists());
|
||||
|
Loading…
Reference in New Issue
Block a user