Skip to content

Commit fb47fb6

Browse files
committed
[Windows] Use GetFileSizeEx instead of GetFileSize
GetFileSizeEx is available in all supported Windows versions and has the advantage is also present in UWP, then allows build also in UWP with the same common code. It also simplifies error checking and makes handling files larger than 4GB easier.
1 parent 93454fe commit fb47fb6

1 file changed

Lines changed: 9 additions & 21 deletions

File tree

Modules/mmapmodule.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -806,20 +806,12 @@ mmap_mmap_size_impl(mmap_object *self)
806806

807807
#ifdef MS_WINDOWS
808808
if (self->file_handle != INVALID_HANDLE_VALUE) {
809-
DWORD low,high;
810-
long long size;
811-
low = GetFileSize(self->file_handle, &high);
812-
if (low == INVALID_FILE_SIZE) {
813-
/* It might be that the function appears to have failed,
814-
when indeed its size equals INVALID_FILE_SIZE */
815-
DWORD error = GetLastError();
816-
if (error != NO_ERROR)
817-
return PyErr_SetFromWindowsErr(error);
809+
LARGE_INTEGER size;
810+
if (!GetFileSizeEx(self->file_handle, &size)) {
811+
DWORD error = GetLastError();
812+
return PyErr_SetFromWindowsErr(error);
818813
}
819-
if (!high && low < LONG_MAX)
820-
return PyLong_FromLong((long)low);
821-
size = (((long long)high)<<32) + low;
822-
return PyLong_FromLongLong(size);
814+
return PyLong_FromLongLong(size.QuadPart);
823815
}
824816
#endif /* MS_WINDOWS */
825817

@@ -2127,18 +2119,14 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
21272119
m_obj->file_handle = fh;
21282120
}
21292121
if (!map_size) {
2130-
DWORD low,high;
2131-
low = GetFileSize(fh, &high);
2132-
/* low might just happen to have the value INVALID_FILE_SIZE;
2133-
so we need to check the last error also. */
2134-
if (low == INVALID_FILE_SIZE &&
2135-
(dwErr = GetLastError()) != NO_ERROR)
2122+
LARGE_INTEGER li;
2123+
if (!GetFileSizeEx(fh, &li))
21362124
{
2125+
dwErr = GetLastError();
21372126
Py_DECREF(m_obj);
21382127
return PyErr_SetFromWindowsErr(dwErr);
21392128
}
2140-
2141-
size = (((long long) high) << 32) + low;
2129+
size = li.QuadPart;
21422130
if (size == 0) {
21432131
PyErr_SetString(PyExc_ValueError,
21442132
"cannot mmap an empty file");

0 commit comments

Comments
 (0)