Fix QDir::operator==(const QDir &) const
authorJoão Abecasis <joao.abecasis@nokia.com>
Thu, 11 Aug 2011 13:49:37 +0000 (15:49 +0200)
committerQt by Nokia <qt-info@nokia.com>
Thu, 6 Oct 2011 09:24:20 +0000 (11:24 +0200)
We can't rely on absolute paths when comparing directories for equality
as these don't take into account symbolic links and may also bypass ../
and ./ simplification.

Instead, canonical paths must be computed and can then be compared
according to the case sensitivity rules for the platform or file engine,
as is done in QFileInfo.

Task-number: QTBUG-20495
Reviewed-by: Prasanth Ullattil
(cherry-picked from dcee6e1371d899eb79717b8e3f3eec08b765db82)

Change-Id: Ib5f2a6ee11311c55782ea5dd0e9c3b45f9231686
Reviewed-on: http://codereview.qt-project.org/5812
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
src/corelib/io/qdir.cpp
src/corelib/io/qfileinfo.cpp
tests/auto/corelib/io/qdir/tst_qdir.cpp

index 9faa9f1..0c70500 100644 (file)
@@ -1599,9 +1599,9 @@ bool QDir::operator==(const QDir &dir) const
     if (d->filters == other->filters
        && d->sort == other->sort
        && d->nameFilters == other->nameFilters) {
-        d->resolveAbsoluteEntry();
-        other->resolveAbsoluteEntry();
-        return d->absoluteDirEntry.filePath().compare(other->absoluteDirEntry.filePath(), sensitive) == 0;
+
+        // Fallback to expensive canonical path computation
+        return canonicalPath().compare(dir.canonicalPath(), sensitive) == 0;
     }
     return false;
 }
index c83c391..b560a81 100644 (file)
@@ -418,6 +418,7 @@ bool QFileInfo::operator==(const QFileInfo &fileinfo) const
     if (fileinfo.size() != size()) //if the size isn't the same...
         return false;
 
+   // Fallback to expensive canonical path computation
    return canonicalFilePath().compare(fileinfo.canonicalFilePath(), sensitive) == 0;
 }
 
index 2c137d1..dd616c8 100644 (file)
@@ -418,9 +418,15 @@ void tst_QDir::QDir_default()
 void tst_QDir::compare()
 {
     // operator==
+
+    // Not using QCOMPARE to test result of QDir::operator==
+
     QDir dir;
     dir.makeAbsolute();
     QVERIFY(dir == QDir::currentPath());
+
+    QVERIFY(QDir() == QDir(QDir::currentPath()));
+    QVERIFY(QDir("../") == QDir(QDir::currentPath() + "/.."));
 }
 
 static QStringList filterLinks(const QStringList &list)