QFileSystemModel: do not unwatch directories if removal fails

... otherwise we would not detect subsequent file/directories added
into the non-removed one.

Change-Id: I43018dfb9a9c6c0399190800da3f0d572ec5d8d8
Task-number: QTBUG-49307
Reviewed-by: David Faure <david.faure@kdab.com>
bb10
Giuseppe D'Angelo 2015-12-10 14:58:12 +01:00
parent f0c75bb050
commit 0d54b0f4dd
2 changed files with 43 additions and 8 deletions

View File

@ -198,13 +198,14 @@ QFileInfo QFileSystemModel::fileInfo(const QModelIndex &index) const
bool QFileSystemModel::remove(const QModelIndex &aindex)
{
const QString path = filePath(aindex);
const bool success = QFileInfo(path).isFile() ? QFile::remove(path) : QDir(path).removeRecursively();
#ifndef QT_NO_FILESYSTEMWATCHER
QFileSystemModelPrivate * d = const_cast<QFileSystemModelPrivate*>(d_func());
d->fileInfoGatherer.removePath(path);
if (success) {
QFileSystemModelPrivate * d = const_cast<QFileSystemModelPrivate*>(d_func());
d->fileInfoGatherer.removePath(path);
}
#endif
if (QFileInfo(path).isFile())
return QFile::remove(path);
return QDir(path).removeRecursively();
return success;
}
/*!
@ -1607,11 +1608,14 @@ bool QFileSystemModel::event(QEvent *event)
bool QFileSystemModel::rmdir(const QModelIndex &aindex)
{
QString path = filePath(aindex);
const bool success = QDir().rmdir(path);
#ifndef QT_NO_FILESYSTEMWATCHER
QFileSystemModelPrivate * d = const_cast<QFileSystemModelPrivate*>(d_func());
d->fileInfoGatherer.removePath(path);
if (success) {
QFileSystemModelPrivate * d = const_cast<QFileSystemModelPrivate*>(d_func());
d->fileInfoGatherer.removePath(path);
}
#endif
return QDir().rmdir(path);
return success;
}
/*!

View File

@ -123,6 +123,8 @@ private slots:
void permissions_data();
void permissions();
void doNotUnwatchOnFailedRmdir();
protected:
bool createFiles(const QString &test_path, const QStringList &initial_files, int existingFileCount = 0, const QStringList &intial_dirs = QStringList());
@ -1043,6 +1045,35 @@ void tst_QFileSystemModel::permissions() // checks QTBUG-20503
QCOMPARE(fileInfoPermissions, modelPermissions);
}
void tst_QFileSystemModel::doNotUnwatchOnFailedRmdir()
{
const QString tmp = flatDirTestPath;
QFileSystemModel model;
const QTemporaryDir tempDir(tmp + '/' + QStringLiteral("doNotUnwatchOnFailedRmdir-XXXXXX"));
QVERIFY(tempDir.isValid());
const QModelIndex rootIndex = model.setRootPath(tempDir.path());
// create a file in the directory so to prevent it from deletion
{
QFile file(tempDir.path() + '/' + QStringLiteral("file1"));
QVERIFY(file.open(QIODevice::WriteOnly));
}
QCOMPARE(model.rmdir(rootIndex), false);
// create another file
{
QFile file(tempDir.path() + '/' + QStringLiteral("file2"));
QVERIFY(file.open(QIODevice::WriteOnly));
}
// the model must now detect this second file
QTRY_COMPARE(model.rowCount(rootIndex), 2);
}
QTEST_MAIN(tst_QFileSystemModel)
#include "tst_qfilesystemmodel.moc"