windows: fix pread/pwrite
PATCH 1/1 windows: fix pread/pwrite
The pread and pwrite functions for Windows posix emulation never actually seek to the requested offset. Fix this so that the psync ioengine works correctly on Windows.
--- os/windows/posix.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
os/windows/posix.c +8 -2
@@ -830,18 +830,24 @@ ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) {int64_t pos =_telli64 (fildes); -ssize_t len =_write (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =_write (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }ssize_t pread (int fildes,void *buf,size_t nbyte,off_t offset) {int64_t pos =_telli64 (fildes); -ssize_t len =read (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =read (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }
Signature · 1 line
2.47.2
Quoted 29 lines · Vincent Fu
The pread and pwrite functions for Windows posix emulation never actually seek to the requested offset. Fix this so that the psync ioengine works correctly on Windows.
Signed-off-by Vincent Fu <[email protected]>--- os/windows/posix.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)os/windows/posix.c +4
-1@@ -830,18 +830,24 @@ ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) {int64_t pos =_telli64 (fildes); -ssize_t len =_write (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =_write (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }ssize_t pread (int fildes,void *buf,size_t nbyte,off_t offset) {
os/windows/posix.c ssize_t pwrite(int fildes, const void *buf, size_t nbyte, · line 852
Quoted 10 lines
os/windows/posix.c +4
-1int64_t pos =_telli64 (fildes); -ssize_t len =read (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =read (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }
lol, how broken is that... Does windows have any offset read/write variants, or is lseek required?
Signature · 1 line
Jens Axboe
Quoted 45 lines · Jens Axboe
Quoted 29 lines · Vincent Fu
The pread and pwrite functions for Windows posix emulation never actually seek to the requested offset. Fix this so that the psync ioengine works correctly on Windows.
Signed-off-by Vincent Fu <[email protected]>--- os/windows/posix.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)os/windows/posix.c +4
-1@@ -830,18 +830,24 @@ ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) {int64_t pos =_telli64 (fildes); -ssize_t len =_write (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =_write (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }ssize_t pread (int fildes,void *buf,size_t nbyte,off_t offset) {os/windows/posix.c ssize_t pwrite(int fildes, const void *buf, size_t nbyte, · line 852
Quoted 10 lines
os/windows/posix.c +4
-1int64_t pos =_telli64 (fildes); -ssize_t len =read (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =read (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }lol, how broken is that... Does windows have any offset read/write variants, or is lseek required?
Quoted 3 lines
--
Jens Axboe
I could not find a better way to do this but I am hoping someone who knows more about Windows than I do can weigh in. Sitsofe?
Vincent
Quoted 46 lines · Vincent Fu
Quoted 45 lines · On Thu, May 8, 2025 at 1:18?PM Jens Axboe <[email protected]> wrote:
Quoted 29 lines · Vincent Fu
The pread and pwrite functions for Windows posix emulation never actually seek to the requested offset. Fix this so that the psync ioengine works correctly on Windows.
Signed-off-by Vincent Fu <[email protected]>--- os/windows/posix.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)os/windows/posix.c +4
-1@@ -830,18 +830,24 @@ ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) {int64_t pos =_telli64 (fildes); -ssize_t len =_write (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =_write (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }ssize_t pread (int fildes,void *buf,size_t nbyte,off_t offset) {os/windows/posix.c ssize_t pwrite(int fildes, const void *buf, size_t nbyte, · line 852
Quoted 10 lines
os/windows/posix.c +4
-1int64_t pos =_telli64 (fildes); -ssize_t len =read (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =read (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }lol, how broken is that... Does windows have any offset read/write variants, or is lseek required?
Quoted 6 lines
Quoted 3 lines
--
Jens AxboeI could not find a better way to do this but I am hoping someone who knows more about Windows than I do can weigh in. Sitsofe?
It's probably not a big deal. I did a quick search and doesn't seem like one exists for that.
Signature · 1 line
Jens Axboe
Quoted 51 lines · Jens Axboe
Quoted 50 lines · Vincent Fu
Quoted 48 lines · On Thu, May 8, 2025 at 1:18?PM Jens Axboe <[email protected]> wrote:
Quoted 29 lines · Vincent Fu
The pread and pwrite functions for Windows posix emulation never actually seek to the requested offset. Fix this so that the psync ioengine works correctly on Windows.
Signed-off-by Vincent Fu <[email protected]>--- os/windows/posix.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)os/windows/posix.c +4
-1@@ -830,18 +830,24 @@ ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) {int64_t pos =_telli64 (fildes); -ssize_t len =_write (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =_write (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }ssize_t pread (int fildes,void *buf,size_t nbyte,off_t offset) {os/windows/posix.c ssize_t pwrite(int fildes, const void *buf, size_t nbyte, · line 852
Quoted 10 lines
os/windows/posix.c +4
-1int64_t pos =_telli64 (fildes); -ssize_t len =read (fildes, buf, nbyte); +ssize_t len; +_lseeki64 (fildes, offset,SEEK_SET ); + len =read (fildes, buf, nbyte);_lseeki64 (fildes, pos,SEEK_SET ); +return len; }lol, how broken is that... Does windows have any offset read/write variants, or is lseek required?
--
Jens Axboe
Quoted 6 lines
Quoted 2 lines
I could not find a better way to do this but I am hoping someone who knows more about Windows than I do can weigh in. Sitsofe?
It's probably not a big deal. I did a quick search and doesn't seem like one exists for that.
Ok thanks. I will go ahead with it then.
Vincent
Hi Vincent,
Quoted 7 lines · Vincent Fu
Quoted 5 lines · Jens Axboe
Quoted 3 lines · Vincent Fu
The pread and pwrite functions for Windows posix emulation never actually seek to the requested offset. Fix this so that the psync ioengine works correctly on Windows.
Oof! Good catch!
[...]
Quoted 5 lines
Quoted 2 lines
lol, how broken is that... Does windows have any offset read/write variants, or is lseek required?
I could not find a better way to do this but I am hoping someone who knows more about Windows than I do can weigh in. Sitsofe?
Sadly (?) I'm not a Windows expert (even if I've occasionally played one) but according to an answer on
https://stackoverflow.com/questions/3852758/equivalent-to-pread-pwrite-in-msvc
the only thing that can take an offset with the request is ReadFile/WriteFile and this is done by passing an OVERLAPPED structure
(https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-overlapped
) which in turn can specify an offset even for synchronous I/O (see
https://learn.microsoft.com/en-gb/windows/win32/api/fileapi/nf-fileapi-writefile?redirectedfrom=MSDN#synchronization-and-file-position
). Unfortunately ReadFile/WriteFile only work with Windows handles but I think you can get one from a file descriptor via _get_osfhandle (see
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-170).
--
Sitsofe
Quoted 33 lines · Sitsofe Wheeler
Hi Vincent,
Quoted 7 lines · Vincent Fu
Quoted 5 lines · Jens Axboe
Quoted 3 lines · Vincent Fu
The pread and pwrite functions for Windows posix emulation never actually seek to the requested offset. Fix this so that the psync ioengine works correctly on Windows.
Oof! Good catch!
[...]
Quoted 5 lines
Quoted 2 lines
lol, how broken is that... Does windows have any offset read/write variants, or is lseek required?
I could not find a better way to do this but I am hoping someone who knows more about Windows than I do can weigh in. Sitsofe?
Sadly (?) I'm not a Windows expert (even if I've occasionally played one) but according to an answer on
https://stackoverflow.com/questions/3852758/equivalent-to-pread-pwrite-in-msvc
the only thing that can take an offset with the request is ReadFile/WriteFile and this is done by passing an OVERLAPPED structure
(https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-overlapped
) which in turn can specify an offset even for synchronous I/O (see
https://learn.microsoft.com/en-gb/windows/win32/api/fileapi/nf-fileapi-writefile?redirectedfrom=MSDN#synchronization-and-file-position
). Unfortunately ReadFile/WriteFile only work with Windows handles but I think you can get one from a file descriptor via_get_osfhandle(see
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-170).
Quoted 2 lines
--
Sitsofe
Sitsofe, many thanks for taking a look. I think my patch is good enough for now but I'm pleased to know that someone who really caress about this may be able to eventually improve on it.
Vincent
Re: fio CI test result
In reply to Vincent FuThe result of fio's continuous integration tests was: success
For more details see https://github.com/fiotestbot/fio/actions/runs/14912501902