From 2d0bd1a13328d5a5ed6c4d1e768937a6627f1086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Tue, 12 Jan 2010 19:14:56 +0100 Subject: QFile::remove: don't fail for unrelated errors remove was checking for errors from close, but without clearing the error state beforehand it ended picking unrelated errors. Task-number: QTBUG-7285 Reviewed-by: qCaro --- src/corelib/io/qfile.cpp | 1 + tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp | 36 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp index 6395cc7..4c7f3f0 100644 --- a/src/corelib/io/qfile.cpp +++ b/src/corelib/io/qfile.cpp @@ -643,6 +643,7 @@ QFile::remove() qWarning("QFile::remove: Empty or null file name"); return false; } + unsetError(); close(); if(error() == QFile::NoError) { if(fileEngine()->remove()) { diff --git a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp index c781108..1304f4e 100644 --- a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp +++ b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp @@ -95,6 +95,7 @@ private slots: void keepOpenMode(); void resetTemplateAfterError(); void setTemplateAfterOpen(); + void autoRemoveAfterFailedRename(); public: }; @@ -558,5 +559,40 @@ void tst_QTemporaryFile::setTemplateAfterOpen() QCOMPARE( temp.fileTemplate(), newTemplate ); } +void tst_QTemporaryFile::autoRemoveAfterFailedRename() +{ + struct CleanOnReturn + { + ~CleanOnReturn() + { + if (!tempName.isEmpty()) + QFile::remove(tempName); + } + + void reset() + { + tempName.clear(); + } + + QString tempName; + }; + + CleanOnReturn cleaner; + + { + QTemporaryFile file; + QVERIFY( file.open() ); + cleaner.tempName = file.fileName(); + + QVERIFY( QFile::exists(cleaner.tempName) ); + QVERIFY( !QFileInfo("i-do-not-exist").isDir() ); + QVERIFY( !file.rename("i-do-not-exist/file.txt") ); + QVERIFY( QFile::exists(cleaner.tempName) ); + } + + QVERIFY( !QFile::exists(cleaner.tempName) ); + cleaner.reset(); +} + QTEST_MAIN(tst_QTemporaryFile) #include "tst_qtemporaryfile.moc" -- cgit v0.12