From 6b448660f0d967d2749a520b49aa6946e46bff6b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20Abecasis?= Date: Wed, 4 Nov 2009 20:28:55 +0100 Subject: [PATCH] Further fixes to file size handling on Windows with fd and FILE* filelength is not available on Windows CE instead, we must fallback to fseek/ftell as was being done previously. Still on Windows CE, we still don't report the file size for file descriptors, but we also won't set a random error string. Changed qt_error_string calls to use errno when errors come from CRT functions. Also, if we're using filelength on FILE* streams, there's no reason not to use it for file descriptors, instead of requesting a native handle. Reviewed-by: Olivier Goffart --- src/corelib/io/qfsfileengine_win.cpp | 43 ++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/corelib/io/qfsfileengine_win.cpp b/src/corelib/io/qfsfileengine_win.cpp index 9fc4500bb0..a6cb5a9f7a 100644 --- a/src/corelib/io/qfsfileengine_win.cpp +++ b/src/corelib/io/qfsfileengine_win.cpp @@ -497,11 +497,30 @@ qint64 QFSFileEnginePrivate::nativeSize() const // ### Don't flush; for buffered files, we should get away with ftell. thatQ->flush(); +#if !defined(Q_OS_WINCE) + // stdlib/stdio mode. + if (fh || fd != -1) { + qint64 fileSize = _filelengthi64(fh ? QT_FILENO(fh) : fd); + if (fileSize == -1) { + fileSize = 0; + thatQ->setError(QFile::UnspecifiedError, qt_error_string(errno)); + } + return fileSize; + } +#else // Q_OS_WINCE // Buffered stdlib mode. if (fh) { - qint64 fileSize = _filelengthi64(QT_FILENO(fh)); - return (fileSize == -1) ? 0 : fileSize; + QT_OFF_T oldPos = QT_FTELL(fh); + QT_FSEEK(fh, 0, SEEK_END); + qint64 fileSize = (qint64)QT_FTELL(fh); + QT_FSEEK(fh, oldPos, SEEK_SET); + if (fileSize == -1) { + fileSize = 0; + thatQ->setError(QFile::UnspecifiedError, qt_error_string(errno)); + } + return fileSize; } +#endif // Not-open mode, where the file name is known: We'll check the // file system directly. @@ -541,23 +560,13 @@ qint64 QFSFileEnginePrivate::nativeSize() const return 0; } - // Unbuffed stdio mode. - if(fd != -1) { -#if !defined(Q_OS_WINCE) - HANDLE handle = (HANDLE)_get_osfhandle(fd); - if (handle != INVALID_HANDLE_VALUE) { - BY_HANDLE_FILE_INFORMATION fileInfo; - if (GetFileInformationByHandle(handle, &fileInfo)) { - qint64 size = fileInfo.nFileSizeHigh; - size <<= 32; - size += fileInfo.nFileSizeLow; - return size; - } - } -#endif - thatQ->setError(QFile::UnspecifiedError, qt_error_string()); +#if defined(Q_OS_WINCE) + // Unbuffed stdio mode + if (fd != -1) { + thatQ->setError(QFile::UnspecifiedError, QLatin1String("Not implemented!")); return 0; } +#endif // Windows native mode. if (fileHandle == INVALID_HANDLE_VALUE) -- 2.11.4.GIT