From 05d980664fd3958add575712faf2abbb12b20857 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Tue, 29 May 2012 12:01:24 +0200 Subject: Make QIODevice::seek() return false for sequential files. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task-number: QTBUG-18173 Change-Id: Ie3a96d3a6f60995b8ba7823153778869d0c2dc58 Reviewed-by: Andy Shaw Reviewed-by: Jędrzej Nowacki --- src/corelib/io/qiodevice.cpp | 16 +++++++++------- tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index 3c2da7fa85..03bf59ed9d 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -606,18 +606,22 @@ qint64 QIODevice::size() const /*! For random-access devices, this function sets the current position to \a pos, returning true on success, or false if an error occurred. - For sequential devices, the default behavior is to do nothing and - return false. + For sequential devices, the default behavior is to produce a warning + and return false. When subclassing QIODevice, you must call QIODevice::seek() at the start of your function to ensure integrity with QIODevice's - built-in buffer. The base implementation always returns true. + built-in buffer. \sa pos(), isSequential() */ bool QIODevice::seek(qint64 pos) { Q_D(QIODevice); + if (d->isSequential()) { + qWarning("QIODevice::seek: Cannot call seek on a sequential device"); + return false; + } if (d->openMode == NotOpen) { qWarning("QIODevice::seek: The device is not open"); return false; @@ -633,10 +637,8 @@ bool QIODevice::seek(qint64 pos) #endif qint64 offset = pos - d->pos; - if (!d->isSequential()) { - d->pos = pos; - d->devicePos = pos; - } + d->pos = pos; + d->devicePos = pos; if (offset < 0 || offset >= qint64(d->buffer.size())) diff --git a/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp b/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp index 444762e680..22ce512cf9 100644 --- a/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp +++ b/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp @@ -373,9 +373,9 @@ void tst_QSslSocket::constructing() QCOMPARE(socket.readLine(0, 0), qint64(-1)); char buf[10]; QCOMPARE(socket.readLine(buf, sizeof(buf)), qint64(-1)); - QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek: The device is not open"); + QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek: Cannot call seek on a sequential device"); QVERIFY(!socket.reset()); - QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek: The device is not open"); + QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek: Cannot call seek on a sequential device"); QVERIFY(!socket.seek(2)); QCOMPARE(socket.size(), qint64(0)); QVERIFY(!socket.waitForBytesWritten(10)); -- cgit v1.2.3