Fix QIODevice warning when running rcc.
authorMitch Curtis <mitch.curtis@nokia.com>
Mon, 11 Jun 2012 11:32:17 +0000 (13:32 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 26 Jun 2012 17:30:00 +0000 (19:30 +0200)
When opening a QFile on stdout, for example,
we must not call seek as it is a sequential device.
This has been flagged as a warning since commit Ie3a96d3a
and has resulted in spurious warnings being emitted.

In the case of opening a QFile in Append mode, QIODevice::open
already sets the position marker, so calling seek is redundant.
This is also true for the file engine's open function (called
through openExternalFile()), which also ensures the handle or
descriptor is repositioned appropriately.

Task-number: QTBUG-26104
Change-Id: I71040c399efe54e7538f54433368b432e959e08d
Reviewed-by: Kent Hansen <kent.hansen@nokia.com>
Reviewed-by: Joerg Bornemann <joerg.bornemann@nokia.com>
src/corelib/io/qfile.cpp
tests/auto/corelib/io/qfile/tst_qfile.cpp

index 22b8ae5..dc926ca 100644 (file)
@@ -902,12 +902,12 @@ bool QFile::open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
     }
     if (d->openExternalFile(mode, fh, handleFlags)) {
         QIODevice::open(mode);
-        if (mode & Append) {
-            seek(size());
-        } else {
+        if (!(mode & Append) && !isSequential()) {
             qint64 pos = (qint64)QT_FTELL(fh);
-            if (pos != -1)
-                seek(pos);
+            if (pos != -1) {
+                // Skip redundant checks in QFileDevice::seek().
+                QIODevice::seek(pos);
+            }
         }
         return true;
     }
@@ -960,12 +960,12 @@ bool QFile::open(int fd, OpenMode mode, FileHandleFlags handleFlags)
     }
     if (d->openExternalFile(mode, fd, handleFlags)) {
         QIODevice::open(mode);
-        if (mode & Append) {
-            seek(size());
-        } else {
+        if (!(mode & Append) && !isSequential()) {
             qint64 pos = (qint64)QT_LSEEK(fd, QT_OFF_T(0), SEEK_CUR);
-            if (pos != -1)
-                seek(pos);
+            if (pos != -1) {
+                // Skip redundant checks in QFileDevice::seek().
+                QIODevice::seek(pos);
+            }
         }
         return true;
     }
index ae80159..bb28010 100644 (file)
@@ -3065,6 +3065,40 @@ static qint64 streamCurrentPosition(FILE *f)
     return 0;
 }
 
+class MessageHandler {
+public:
+    MessageHandler(QtMessageHandler messageHandler = handler)
+    {
+        ok = true;
+        oldMessageHandler = qInstallMessageHandler(messageHandler);
+    }
+
+    ~MessageHandler()
+    {
+        qInstallMessageHandler(oldMessageHandler);
+    }
+
+    static bool testPassed()
+    {
+        return ok;
+    }
+protected:
+    static void handler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
+    {
+        if (msg == QString::fromLatin1("QIODevice::seek: Cannot call seek on a sequential device"))
+            ok = false;
+        // Defer to old message handler.
+        if (oldMessageHandler)
+            oldMessageHandler(type, context, msg);
+    }
+
+    static QtMessageHandler oldMessageHandler;
+    static bool ok;
+};
+
+bool MessageHandler::ok = true;
+QtMessageHandler MessageHandler::oldMessageHandler = 0;
+
 void tst_QFile::openStandardStreamsFileDescriptors()
 {
 #ifdef Q_OS_WINCE
@@ -3074,6 +3108,9 @@ void tst_QFile::openStandardStreamsFileDescriptors()
     QSKIP("Opening standard streams on Windows CE via descriptor not implemented");
 #endif
 
+    // Check that QIODevice::seek() isn't called when opening a sequential device (QFile).
+    MessageHandler msgHandler;
+
     {
         QFile in;
         in.open(STDIN_FILENO, QIODevice::ReadOnly);
@@ -3094,6 +3131,8 @@ void tst_QFile::openStandardStreamsFileDescriptors()
         QCOMPARE( err.pos(), streamCurrentPosition(STDERR_FILENO) );
         QCOMPARE( err.size(), streamExpectedSize(STDERR_FILENO) );
     }
+
+    QVERIFY(msgHandler.testPassed());
 }
 
 void tst_QFile::openStandardStreamsBufferedStreams()
@@ -3101,6 +3140,9 @@ void tst_QFile::openStandardStreamsBufferedStreams()
 #ifdef Q_OS_WINCE
     QSKIP("Not tested on Windows CE.");
 #endif
+    // Check that QIODevice::seek() isn't called when opening a sequential device (QFile).
+    MessageHandler msgHandler;
+
     // Using streams
     {
         QFile in;
@@ -3122,6 +3164,8 @@ void tst_QFile::openStandardStreamsBufferedStreams()
         QCOMPARE( err.pos(), streamCurrentPosition(stderr) );
         QCOMPARE( err.size(), streamExpectedSize(QT_FILENO(stderr)) );
     }
+
+    QVERIFY(msgHandler.testPassed());
 }
 
 void tst_QFile::writeNothing()