From 0d54b0f4ddb3ec3e14f39d954eb43b10100bbce9 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> Date: Thu, 10 Dec 2015 14:58:12 +0100 Subject: [PATCH] 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> --- src/widgets/dialogs/qfilesystemmodel.cpp | 20 +++++++----- .../qfilesystemmodel/tst_qfilesystemmodel.cpp | 31 +++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index 5dd8c057349..995d0c77006 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -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; } /*! diff --git a/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp index 3252650d125..996d9195913 100644 --- a/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp +++ b/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp @@ -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" -- GitLab