src/corelib/io/qfsfileengine_win.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
--- a/src/corelib/io/qfsfileengine_win.cpp	Tue Jan 26 12:42:25 2010 +0200
+++ b/src/corelib/io/qfsfileengine_win.cpp	Tue Feb 02 00:43:10 2010 +0200
@@ -455,17 +455,10 @@
 
     // Windows native mode.
     bool ok = true;
-    if ((fileHandle == INVALID_HANDLE_VALUE || !CloseHandle(fileHandle))
-#ifdef Q_USE_DEPRECATED_MAP_API
-            && (fileMapHandle == INVALID_HANDLE_VALUE || !CloseHandle(fileMapHandle))
-#endif
-        ) {
+    if ((fileHandle == INVALID_HANDLE_VALUE || !::CloseHandle(fileHandle))) {
         q->setError(QFile::UnspecifiedError, qt_error_string());
         ok = false;
     }
-#ifdef Q_USE_DEPRECATED_MAP_API
-    fileMapHandle = INVALID_HANDLE_VALUE;
-#endif
     fileHandle = INVALID_HANDLE_VALUE;
     cachedFd = -1;              // gets closed by CloseHandle above
 
@@ -504,14 +497,30 @@
     // ### 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) {
         QT_OFF_T oldPos = QT_FTELL(fh);
         QT_FSEEK(fh, 0, SEEK_END);
-        QT_OFF_T fileSize = QT_FTELL(fh);
+        qint64 fileSize = (qint64)QT_FTELL(fh);
         QT_FSEEK(fh, oldPos, SEEK_SET);
-        return qint64(fileSize);
+        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.
@@ -551,23 +560,13 @@
         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)
@@ -799,27 +798,18 @@
 bool QFSFileEnginePrivate::nativeIsSequential() const
 {
 #if !defined(Q_OS_WINCE)
-    // stdlib / Windows native mode.
-    if (fh || fileHandle != INVALID_HANDLE_VALUE) {
-        if (fh == stdin || fh == stdout || fh == stderr)
-            return true;
+    HANDLE handle = fileHandle;
+    if (fh || fd != -1)
+        handle = (HANDLE)_get_osfhandle(fh ? QT_FILENO(fh) : fd);
+    if (handle == INVALID_HANDLE_VALUE)
+        return false;
 
-        HANDLE handle = fileHandle;
-        if (fileHandle == INVALID_HANDLE_VALUE) {
-            // Rare case: using QFile::open(FILE*) to open a pipe.
-            handle = (HANDLE)_get_osfhandle(QT_FILENO(fh));
-            return false;
-        }
-
-        DWORD fileType = GetFileType(handle);
-        return fileType == FILE_TYPE_PIPE;
-    }
-
-    // stdio mode.
-    if (fd != -1)
-        return isSequentialFdFh();
+    DWORD fileType = GetFileType(handle);
+    return (fileType == FILE_TYPE_CHAR)
+            || (fileType == FILE_TYPE_PIPE);
+#else
+    return false;
 #endif
-    return false;
 }
 
 bool QFSFileEngine::remove()
@@ -1931,42 +1921,42 @@
         return 0;
     }
 
+    if (mapHandle == INVALID_HANDLE_VALUE) {
+        // get handle to the file
+        HANDLE handle = fileHandle;
 
-    // get handle to the file
-    HANDLE handle = fileHandle;
 #ifndef Q_OS_WINCE
-    if (handle == INVALID_HANDLE_VALUE && fh)
-        handle = (HANDLE)_get_osfhandle(QT_FILENO(fh));
+        if (handle == INVALID_HANDLE_VALUE && fh)
+            handle = (HANDLE)::_get_osfhandle(QT_FILENO(fh));
 #endif
 
 #ifdef Q_USE_DEPRECATED_MAP_API
-    if (fileMapHandle == INVALID_HANDLE_VALUE) {
         nativeClose();
-        fileMapHandle = CreateFileForMapping((const wchar_t*)nativeFilePath.constData(),
+        // handle automatically closed by kernel with mapHandle (below).
+        handle = ::CreateFileForMapping((const wchar_t*)nativeFilePath.constData(),
                 GENERIC_READ | (openMode & QIODevice::WriteOnly ? GENERIC_WRITE : 0),
                 0,
                 NULL,
                 OPEN_EXISTING,
                 FILE_ATTRIBUTE_NORMAL,
                 NULL);
-    }
-    handle = fileMapHandle;
 #endif
 
-    if (handle == INVALID_HANDLE_VALUE) {
-        q->setError(QFile::PermissionsError, qt_error_string(ERROR_ACCESS_DENIED));
-        return 0;
-    }
+        if (handle == INVALID_HANDLE_VALUE) {
+            q->setError(QFile::PermissionsError, qt_error_string(ERROR_ACCESS_DENIED));
+            return 0;
+        }
 
-    // first create the file mapping handle
-    DWORD protection = (openMode & QIODevice::WriteOnly) ? PAGE_READWRITE : PAGE_READONLY;
-    HANDLE mapHandle = ::CreateFileMapping(handle, 0, protection, 0, 0, 0);
-    if (mapHandle == NULL) {
-        q->setError(QFile::PermissionsError, qt_error_string());
+        // first create the file mapping handle
+        DWORD protection = (openMode & QIODevice::WriteOnly) ? PAGE_READWRITE : PAGE_READONLY;
+        mapHandle = ::CreateFileMapping(handle, 0, protection, 0, 0, 0);
+        if (mapHandle == INVALID_HANDLE_VALUE) {
+            q->setError(QFile::PermissionsError, qt_error_string());
 #ifdef Q_USE_DEPRECATED_MAP_API
-        mapHandleClose();
+            ::CloseHandle(handle);
 #endif
-        return 0;
+            return 0;
+        }
     }
 
     // setup args to map
@@ -1978,17 +1968,17 @@
     DWORD offsetLo = offset & Q_UINT64_C(0xffffffff);
     SYSTEM_INFO sysinfo;
     ::GetSystemInfo(&sysinfo);
-    int mask = sysinfo.dwAllocationGranularity - 1;
-    int extra = offset & mask;
+    DWORD mask = sysinfo.dwAllocationGranularity - 1;
+    DWORD extra = offset & mask;
     if (extra)
         offsetLo &= ~mask;
 
     // attempt to create the map
-    LPVOID mapAddress = MapViewOfFile(mapHandle, access,
+    LPVOID mapAddress = ::MapViewOfFile(mapHandle, access,
                                       offsetHi, offsetLo, size + extra);
     if (mapAddress) {
         uchar *address = extra + static_cast<uchar*>(mapAddress);
-        maps[address] = QPair<int, HANDLE>(extra, mapHandle);
+        maps[address] = extra;
         return address;
     }
 
@@ -2001,10 +1991,8 @@
     default:
         q->setError(QFile::UnspecifiedError, qt_error_string());
     }
-    CloseHandle(mapHandle);
-#ifdef Q_USE_DEPRECATED_MAP_API
-    mapHandleClose();
-#endif
+
+    ::CloseHandle(mapHandle);
     return 0;
 }
 
@@ -2015,32 +2003,19 @@
         q->setError(QFile::PermissionsError, qt_error_string(ERROR_ACCESS_DENIED));
         return false;
     }
-    uchar *start = ptr - maps[ptr].first;
+    uchar *start = ptr - maps[ptr];
     if (!UnmapViewOfFile(start)) {
         q->setError(QFile::PermissionsError, qt_error_string());
         return false;
     }
 
-    if (!CloseHandle((HANDLE)maps[ptr].second)) {
-        q->setError(QFile::UnspecifiedError, qt_error_string());
-        return false;
+    maps.remove(ptr);
+    if (maps.isEmpty()) {
+        ::CloseHandle(mapHandle);
+        mapHandle = INVALID_HANDLE_VALUE;
     }
-    maps.remove(ptr);
 
-#ifdef Q_USE_DEPRECATED_MAP_API
-    mapHandleClose();
-#endif
     return true;
 }
 
-#ifdef Q_USE_DEPRECATED_MAP_API
-void QFSFileEnginePrivate::mapHandleClose()
-{
-    if (maps.isEmpty()) {
-        CloseHandle(fileMapHandle);
-        fileMapHandle = INVALID_HANDLE_VALUE;
-    }
-}
-#endif
-
 QT_END_NAMESPACE