Fix BC break with QAbstractFileEngine "mount points"
authorShane Kearns <ext-shane.2.kearns@nokia.com>
Tue, 17 Jan 2012 19:51:39 +0000 (19:51 +0000)
committerQt by Nokia <qt-info@nokia.com>
Sun, 29 Jan 2012 14:22:19 +0000 (15:22 +0100)
File system cached metadata can't be trusted when custom file engines
are in use, because the custom file engine may want to override the
metadata. (e.g. present an archive file as a directory)

Therefore, check if a file engine should be instantiated for each
result in QDirIterator. This is a fast check if no custom file engines
are registered.
When pushing a directory (using QDirIterator::SubDirectories) the
file engine needs to be instantiated also.

Task-number: QTBUG-23688
Task-number: ou1cimx1#965023
Change-Id: I0114c8df6258535553783a2486131c4194926649
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
(cherry picked from 3864ad09d578210b52e5f58fca2ee8a1144f5be2)

src/corelib/io/qfileinfo_p.h
tests/auto/corelib/io/qabstractfileengine/tst_qabstractfileengine.cpp

index befda9f..69aa152 100644 (file)
@@ -108,10 +108,15 @@ public:
         : QSharedData(),
         fileEntry(file),
         metaData(data),
+        fileEngine(QFileSystemEngine::resolveEntryAndCreateLegacyEngine(fileEntry, metaData)),
         cachedFlags(0),
         isDefaultConstructed(false),
         cache_enabled(true), fileFlags(0), fileSize(0)
     {
+        //If the file engine is not null, this maybe a "mount point" for a custom file engine
+        //in which case we can't trust the metadata
+        if (fileEngine)
+            metaData = QFileSystemMetaData();
     }
 
     inline void clearFlags() const {
index f7d3c69..6ca14a4 100644 (file)
 #include <QtCore/QSharedPointer>
 #include <QtCore/QScopedPointer>
 #include <QtCore/QHash>
+#include <QtCore/QDir>
+#include <QtCore/QDirIterator>
 
 #include <QtTest/QTest>
 
 #include <QtCore/QDebug>
+#include "../../../../shared/filesystem.h"
 
 class tst_QAbstractFileEngine
     : public QObject
@@ -65,6 +68,8 @@ private slots:
     void fileIO_data();
     void fileIO();
 
+    void mounting_data();
+    void mounting();
 private:
     QStringList filesForRemoval;
 };
@@ -74,7 +79,7 @@ class ReferenceFileEngine
 {
 public:
     ReferenceFileEngine(const QString &fileName)
-        : fileName_(fileName)
+        : fileName_(QDir::cleanPath(fileName))
         , position_(-1)
         , openForRead_(false)
         , openForWrite_(false)
@@ -491,6 +496,60 @@ private:
     mutable QSharedPointer<File> openFile_;
 };
 
+class MountingFileEngine : public QFSFileEngine
+{
+public:
+    class Iterator : public QAbstractFileEngineIterator
+    {
+    public:
+        Iterator(QDir::Filters filters, const QStringList &filterNames)
+            : QAbstractFileEngineIterator(filters, filterNames)
+        {
+            names.append("foo");
+            names.append("bar");
+            index = -1;
+        }
+        QString currentFileName() const
+        {
+            return names.at(index);
+        }
+        bool hasNext() const
+        {
+            return index < names.size() - 1;
+        }
+        QString next()
+        {
+            if (!hasNext())
+                return QString();
+            ++index;
+            return currentFilePath();
+        }
+        QStringList names;
+        int index;
+    };
+    MountingFileEngine(QString fileName)
+        : QFSFileEngine(fileName)
+    {
+    }
+    Iterator *beginEntryList(QDir::Filters filters, const QStringList &filterNames)
+    {
+        return new Iterator(filters, filterNames);
+    }
+    FileFlags fileFlags(FileFlags type) const
+    {
+        if (fileName(DefaultName).endsWith(".tar")) {
+            FileFlags ret = QFSFileEngine::fileFlags(type);
+            //make this file in file system appear to be a directory
+            ret &= ~FileType;
+            ret |= DirectoryType;
+            return ret;
+        } else {
+            //file inside the archive
+            return ExistsFlag | FileType;
+        }
+    }
+};
+
 QMutex ReferenceFileEngine::fileSystemMutex;
 QHash<uint, QString> ReferenceFileEngine::fileSystemUsers, ReferenceFileEngine::fileSystemGroups;
 QHash<QString, QSharedPointer<ReferenceFileEngine::File> > ReferenceFileEngine::fileSystem;
@@ -500,6 +559,8 @@ class FileEngineHandler
 {
     QAbstractFileEngine *create(const QString &fileName) const
     {
+        if (fileName.endsWith(".tar") || fileName.contains(".tar/"))
+            return new MountingFileEngine(fileName);
         if (fileName.startsWith("QFSFileEngine:"))
             return new QFSFileEngine(fileName.mid(14));
         if (fileName.startsWith("reference-file-engine:"))
@@ -803,6 +864,36 @@ void tst_QAbstractFileEngine::fileIO()
     //
 }
 
+void tst_QAbstractFileEngine::mounting_data()
+{
+    QTest::addColumn<QString>("fileName");
+    QTest::newRow("native") << "test.tar";
+    QTest::newRow("Forced QFSFileEngine") << "QFSFileEngine:test.tar";
+}
+
+void tst_QAbstractFileEngine::mounting()
+{
+    FileSystem fs;
+    QVERIFY(fs.createFile("test.tar"));
+    FileEngineHandler handler;
+
+    QFETCH(QString, fileName);
+
+    QVERIFY(QFileInfo(fileName).isDir());
+    QDir dir(fileName);
+    QCOMPARE(dir.entryList(), (QStringList() << "bar" << "foo"));
+    QDir dir2;
+    bool found = false;
+    foreach (QFileInfo info, dir2.entryInfoList()) {
+        if (info.fileName() == QLatin1String("test.tar")) {
+            QVERIFY(!found);
+            found = true;
+            QVERIFY(info.isDir());
+        }
+    }
+    QVERIFY(found);
+}
+
 QTEST_APPLESS_MAIN(tst_QAbstractFileEngine)
 #include "tst_qabstractfileengine.moc"