From 0b67ad9db2647a69a56bd4ebb4609bb259932cb0 Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Tue, 6 Sep 2011 16:43:29 +0100 Subject: [PATCH] Merge fixes for QDir::operator== There were two fixes in 4.8 which each fixed a part of the problem. Comparing canonical paths is more correct, but is only possible where both directories exist. If neither directory exists, then compare absolute paths instead. Changed a regression test, because /tmp is a symbolic link on MacOS. I.E. "/tmp/.." is canonically "/private" and not "/" as expected. Task-Number: QTBUG-20495 Reviewed-By: joao (cherry-picked from ad35d25e78c8252a72108a4ba931934047c4707e) Change-Id: Ia4986e8337f0e512e1a3398a5a4dd36e62680b9c Reviewed-on: http://codereview.qt-project.org/5813 Reviewed-by: Qt Sanity Bot Reviewed-by: Shane Kearns --- src/corelib/io/qdir.cpp | 19 +++++++++++++++++-- src/corelib/io/qfileinfo.cpp | 5 +++++ tests/auto/corelib/io/qdir/tst_qdir.cpp | 22 +++++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 0c7050095d..68f015ff53 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -1600,8 +1600,23 @@ bool QDir::operator==(const QDir &dir) const && d->sort == other->sort && d->nameFilters == other->nameFilters) { - // Fallback to expensive canonical path computation - return canonicalPath().compare(dir.canonicalPath(), sensitive) == 0; + // Assume directories are the same if path is the same + if (d->dirEntry.filePath() == other->dirEntry.filePath()) + return true; + + if (exists()) { + if (!dir.exists()) + return false; //can't be equal if only one exists + // Both exist, fallback to expensive canonical path computation + return canonicalPath().compare(dir.canonicalPath(), sensitive) == 0; + } else { + if (dir.exists()) + return false; //can't be equal if only one exists + // Neither exists, compare absolute paths rather than canonical (which would be empty strings) + d->resolveAbsoluteEntry(); + other->resolveAbsoluteEntry(); + return d->absoluteDirEntry.filePath().compare(other->absoluteDirEntry.filePath(), sensitive) == 0; + } } return false; } diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp index b560a81ac5..0ee851fd14 100644 --- a/src/corelib/io/qfileinfo.cpp +++ b/src/corelib/io/qfileinfo.cpp @@ -403,6 +403,11 @@ bool QFileInfo::operator==(const QFileInfo &fileinfo) const return true; if (d->isDefaultConstructed || fileinfo.d_ptr->isDefaultConstructed) return false; + + // Assume files are the same if path is the same + if (d->fileEntry.filePath() == fileinfo.d_ptr->fileEntry.filePath()) + return true; + Qt::CaseSensitivity sensitive; if (d->fileEngine == 0 || fileinfo.d_ptr->fileEngine == 0) { if (d->fileEngine != fileinfo.d_ptr->fileEngine) // one is native, the other is a custom file-engine diff --git a/tests/auto/corelib/io/qdir/tst_qdir.cpp b/tests/auto/corelib/io/qdir/tst_qdir.cpp index dd616c8f09..79e19acbce 100644 --- a/tests/auto/corelib/io/qdir/tst_qdir.cpp +++ b/tests/auto/corelib/io/qdir/tst_qdir.cpp @@ -1776,10 +1776,30 @@ void tst_QDir::equalityOperator_data() << "." << "*.cpp" << int(QDir::Name) << int(QDir::Files) << true; - QTest::newRow("QTBUG-20495-root") << QDir::rootPath() + "tmp/.." << "*.cpp" << int(QDir::Name) << int(QDir::Files) + //need a path in the root directory that is unlikely to be a symbolic link. +#if defined (Q_OS_WIN) + QString pathinroot("c:/windows/.."); +#elif defined (Q_OS_SYMBIAN) + QString pathinroot("c:/data/.."); +#else + QString pathinroot("/sbin/.."); +#endif + QTest::newRow("QTBUG-20495-root") << pathinroot << "*.cpp" << int(QDir::Name) << int(QDir::Files) << QDir::rootPath() << "*.cpp" << int(QDir::Name) << int(QDir::Files) << true; + QTest::newRow("slashdot") << QDir::rootPath() + "." << "*.cpp" << int(QDir::Name) << int(QDir::Files) + << QDir::rootPath() << "*.cpp" << int(QDir::Name) << int(QDir::Files) + << true; + + QTest::newRow("slashdotslash") << QDir::rootPath() + "./" << "*.cpp" << int(QDir::Name) << int(QDir::Files) + << QDir::rootPath() << "*.cpp" << int(QDir::Name) << int(QDir::Files) + << true; + + QTest::newRow("nonexistantpaths") << "dir-that-dont-exist" << "*.cpp" << int(QDir::Name) << int(QDir::Files) + << "another-dir-that-dont-exist" << "*.cpp" << int(QDir::Name) << int(QDir::Files) + << false; + QTest::newRow("diff-filters") << SRCDIR << "*.cpp" << int(QDir::Name) << int(QDir::Files) << SRCDIR << "*.cpp" << int(QDir::Name) << int(QDir::Dirs) << false;