From 7986ab58b9a5d0828291c857d3ce86bfa1af4e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Fri, 16 Oct 2009 18:08:30 +0200 Subject: _close(fd) closes the associated handle and not the other way around ... according to the online MSDN documentation. Hid the cachedFd member in private data under WinCE, since it's never used there. Task-number: QTBUG-9085 Reviewed-by: Zeno Albisser --- src/corelib/io/qfsfileengine.cpp | 2 ++ src/corelib/io/qfsfileengine_p.h | 4 +++ src/corelib/io/qfsfileengine_win.cpp | 18 ++++++++++++-- tests/auto/qfile/tst_qfile.cpp | 47 ++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/corelib/io/qfsfileengine.cpp b/src/corelib/io/qfsfileengine.cpp index 5e14804..a1ffb81 100644 --- a/src/corelib/io/qfsfileengine.cpp +++ b/src/corelib/io/qfsfileengine.cpp @@ -126,8 +126,10 @@ void QFSFileEnginePrivate::init() fileAttrib = INVALID_FILE_ATTRIBUTES; fileHandle = INVALID_HANDLE_VALUE; mapHandle = INVALID_HANDLE_VALUE; +#ifndef Q_OS_WINCE cachedFd = -1; #endif +#endif } /*! diff --git a/src/corelib/io/qfsfileengine_p.h b/src/corelib/io/qfsfileengine_p.h index d07c3a0..55c779e 100644 --- a/src/corelib/io/qfsfileengine_p.h +++ b/src/corelib/io/qfsfileengine_p.h @@ -112,7 +112,11 @@ public: HANDLE fileHandle; HANDLE mapHandle; QHash maps; + +#ifndef Q_OS_WINCE mutable int cachedFd; +#endif + mutable DWORD fileAttrib; #else QHash > maps; diff --git a/src/corelib/io/qfsfileengine_win.cpp b/src/corelib/io/qfsfileengine_win.cpp index 8d34486..9fc8dc3 100644 --- a/src/corelib/io/qfsfileengine_win.cpp +++ b/src/corelib/io/qfsfileengine_win.cpp @@ -450,13 +450,27 @@ bool QFSFileEnginePrivate::nativeClose() // Windows native mode. bool ok = true; + +#ifndef Q_OS_WINCE + if (cachedFd != -1) { + if (::_close(cachedFd) && !::CloseHandle(fileHandle)) { + q->setError(QFile::UnspecifiedError, qt_error_string()); + ok = false; + } + + // System handle is closed with associated file descriptor. + fileHandle = INVALID_HANDLE_VALUE; + cachedFd = -1; + + return ok; + } +#endif + if ((fileHandle == INVALID_HANDLE_VALUE || !::CloseHandle(fileHandle))) { q->setError(QFile::UnspecifiedError, qt_error_string()); ok = false; } fileHandle = INVALID_HANDLE_VALUE; - cachedFd = -1; // gets closed by CloseHandle above - return ok; } diff --git a/tests/auto/qfile/tst_qfile.cpp b/tests/auto/qfile/tst_qfile.cpp index f407b12..8722a86 100644 --- a/tests/auto/qfile/tst_qfile.cpp +++ b/tests/auto/qfile/tst_qfile.cpp @@ -196,6 +196,7 @@ private slots: void miscWithUncPathAsCurrentDir(); void standarderror(); void handle(); + void nativeHandleLeaks(); void readEof_data(); void readEof(); @@ -392,6 +393,7 @@ void tst_QFile::cleanupTestCase() QFile::remove("resources"); QFile::remove("qfile_map_testfile"); QFile::remove("readAllBuffer.txt"); + QFile::remove("qt_file.tmp"); } //------------------------------------------ @@ -2538,6 +2540,51 @@ void tst_QFile::handle() #endif } +void tst_QFile::nativeHandleLeaks() +{ + int fd1, fd2; + +#ifdef Q_OS_WIN + HANDLE handle1, handle2; +#endif + + { + QFile file("qt_file.tmp"); + QVERIFY( file.open(QIODevice::ReadWrite) ); + + fd1 = file.handle(); + QVERIFY( -1 != fd1 ); + } + +#ifdef Q_OS_WIN + handle1 = ::CreateFileA("qt_file.tmp", GENERIC_READ, 0, NULL, + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + QVERIFY( INVALID_HANDLE_VALUE != handle1 ); + QVERIFY( ::CloseHandle(handle1) ); +#endif + + { + QFile file("qt_file.tmp"); + QVERIFY( file.open(QIODevice::ReadOnly) ); + + fd2 = file.handle(); + QVERIFY( -1 != fd2 ); + } + +#ifdef Q_OS_WIN + handle2 = ::CreateFileA("qt_file.tmp", GENERIC_READ, 0, NULL, + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + QVERIFY( INVALID_HANDLE_VALUE != handle2 ); + QVERIFY( ::CloseHandle(handle2) ); +#endif + + QCOMPARE( fd2, fd1 ); + +#ifdef Q_OS_WIN + QCOMPARE( handle2, handle1 ); +#endif +} + void tst_QFile::readEof_data() { QTest::addColumn("filename"); -- cgit v0.12