Skip to content

Commit 43956d8

Browse files
committed
Implement os.lseek with SetFilePointerEx on Windows
1 parent 11f457c commit 43956d8

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

Modules/posixmodule.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11406,7 +11406,7 @@ static Py_off_t
1140611406
os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
1140711407
/*[clinic end generated code: output=971e1efb6b30bd2f input=f096e754c5367504]*/
1140811408
{
11409-
Py_off_t result;
11409+
Py_off_t result = 0;
1141011410

1141111411
#ifdef SEEK_SET
1141211412
/* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
@@ -11420,7 +11420,36 @@ os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
1142011420
Py_BEGIN_ALLOW_THREADS
1142111421
_Py_BEGIN_SUPPRESS_IPH
1142211422
#ifdef MS_WINDOWS
11423-
result = _lseeki64(fd, position, how);
11423+
switch (how) {
11424+
case SEEK_SET:
11425+
how = FILE_BEGIN;
11426+
break;
11427+
case SEEK_CUR:
11428+
how = FILE_CURRENT;
11429+
break;
11430+
case SEEK_END:
11431+
how = FILE_END;
11432+
break;
11433+
}
11434+
HANDLE h = (HANDLE)_get_osfhandle(fd);
11435+
if (h == INVALID_HANDLE_VALUE) {
11436+
result = -1;
11437+
}
11438+
if (result >= 0) {
11439+
if (GetFileType(h) != FILE_TYPE_DISK) {
11440+
// Only file is seekable
11441+
result = -1;
11442+
}
11443+
}
11444+
if (result >= 0) {
11445+
LARGE_INTEGER distance, newdistance;
11446+
distance.QuadPart = position;
11447+
if (SetFilePointerEx(h, distance, &newdistance, how)) {
11448+
result = newdistance.QuadPart;
11449+
} else {
11450+
result = -1;
11451+
}
11452+
}
1142411453
#else
1142511454
result = lseek(fd, position, how);
1142611455
#endif

0 commit comments

Comments
 (0)