From 022fa9c37bf1789ad13563086e174264e865ff0d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20Abecasis?= Date: Mon, 16 Nov 2009 16:11:09 +0100 Subject: [PATCH] Fix regression introduced in c08e708037d33271825ce6a6a1ac640e96b70c36 When writing nothing to a file, not actually writing anything is not an error. Also, from a change introduced in the same commit, there is no point in checking for EOF when writing. Task-number: QTBUG-5847 Reviewed-by: Olivier Goffart (cherry picked from commit 72ee915bb0465549b7ca8e62d2af146cc44fdeac) --- src/corelib/io/qfsfileengine.cpp | 6 ++---- tests/auto/qfile/tst_qfile.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/corelib/io/qfsfileengine.cpp b/src/corelib/io/qfsfileengine.cpp index 9ab831fcd2..3cf9b7ec15 100644 --- a/src/corelib/io/qfsfileengine.cpp +++ b/src/corelib/io/qfsfileengine.cpp @@ -762,12 +762,10 @@ qint64 QFSFileEnginePrivate::writeFdFh(const char *data, qint64 len) // Buffered stdlib mode. size_t result; - bool eof; do { result = fwrite(data + writtenBytes, 1, size_t(len - writtenBytes), fh); writtenBytes += result; - eof = feof(fh); - } while (!eof && (result == 0 ? errno == EINTR : writtenBytes < len)); + } while (result == 0 ? errno == EINTR : writtenBytes < len); } else if (fd != -1) { // Unbuffered stdio mode. @@ -783,7 +781,7 @@ qint64 QFSFileEnginePrivate::writeFdFh(const char *data, qint64 len) || (result > 0 && (writtenBytes += result) < len)); } - if (writtenBytes == 0) { + if (len && writtenBytes == 0) { writtenBytes = -1; q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, qt_error_string(errno)); } diff --git a/tests/auto/qfile/tst_qfile.cpp b/tests/auto/qfile/tst_qfile.cpp index 880498e033..c4e6d50989 100644 --- a/tests/auto/qfile/tst_qfile.cpp +++ b/tests/auto/qfile/tst_qfile.cpp @@ -210,6 +210,7 @@ private slots: void task167217(); void openDirectory(); + void writeNothing(); public: // disabled this test for the moment... it hangs @@ -2840,5 +2841,16 @@ void tst_QFile::openStandardStreams() } } +void tst_QFile::writeNothing() +{ + for (int i = 0; i < 3; ++i) { + QFile file("file.txt"); + QVERIFY( openFile(file, QIODevice::WriteOnly | QIODevice::Unbuffered, FileType(i)) ); + QVERIFY( 0 == file.write((char *)0, 0) ); + QCOMPARE( file.error(), QFile::NoError ); + closeFile(file); + } +} + QTEST_MAIN(tst_QFile) #include "tst_qfile.moc" -- 2.11.4.GIT