Make Windows pread and pwrite conform to POSIX

This commit is contained in:
Feoramund
2024-08-28 19:53:20 +02:00
committed by Laytan
parent 5c8c63ae04
commit 861d892ffe
+8 -20
View File
@@ -225,11 +225,13 @@ file_size :: proc(fd: Handle) -> (i64, Error) {
MAX_RW :: 1<<30 MAX_RW :: 1<<30
@(private) @(private)
pread :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Error) { pread :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr_off := seek(fd, 0, 1) or_return
defer seek(fd, curr_off, 0)
buf := data buf := data
if len(buf) > MAX_RW { if len(buf) > MAX_RW {
buf = buf[:MAX_RW] buf = buf[:MAX_RW]
} }
o := win32.OVERLAPPED{ o := win32.OVERLAPPED{
@@ -249,11 +251,13 @@ pread :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Error) {
return int(done), e return int(done), e
} }
@(private) @(private)
pwrite :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Error) { pwrite :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
curr_off := seek(fd, 0, 1) or_return
defer seek(fd, curr_off, 0)
buf := data buf := data
if len(buf) > MAX_RW { if len(buf) > MAX_RW {
buf = buf[:MAX_RW] buf = buf[:MAX_RW]
} }
o := win32.OVERLAPPED{ o := win32.OVERLAPPED{
@@ -273,13 +277,6 @@ pwrite :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Error) {
/* /*
read_at returns n: 0, err: 0 on EOF read_at returns n: 0, err: 0 on EOF
on Windows, read_at changes the position of the file cursor, on *nix, it does not.
bytes: [8]u8{}
read_at(fd, bytes, 0)
read(fd, bytes)
will read from the location twice on *nix, and from two different locations on Windows
*/ */
read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
if offset < 0 { if offset < 0 {
@@ -304,15 +301,6 @@ read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
return return
} }
/*
on Windows, write_at changes the position of the file cursor, on *nix, it does not.
bytes: [8]u8{}
write_at(fd, bytes, 0)
write(fd, bytes)
will write to the location twice on *nix, and to two different locations on Windows
*/
write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
if offset < 0 { if offset < 0 {
return 0, .Invalid_Offset return 0, .Invalid_Offset