-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented windows versions of seekTo and getPos #710
Conversation
std/io.zig
Outdated
const err = system.GetLastError(); | ||
return switch (err) { | ||
system.ERROR.INVALID_PARAMETER => error.BadFd, | ||
else => os.unexpectedErrorPosix(err), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be unexpectedErrorWindows
std/os/windows/index.zig
Outdated
@@ -74,6 +74,9 @@ pub extern "kernel32" stdcallcc fn ReadFile(in_hFile: HANDLE, out_lpBuffer: LPVO | |||
in_nNumberOfBytesToRead: DWORD, out_lpNumberOfBytesRead: &DWORD, | |||
in_out_lpOverlapped: ?&OVERLAPPED) -> BOOL; | |||
|
|||
pub extern "kernel32" stdcallcc fn SetFilePointerEx(in_fFile: HANDLE, in_liDistanceToMove: LARGE_INTEGER, | |||
out_opt_ldNewFilePointer: ?PLARGE_INTEGER, in_dwMoveMethod: DWORD) -> BOOL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The precedent here is to use ?&LARGE_INTEGER
instead of PLARGE_INTEGER
. The P
naming by Microsoft is a bit of an anti-pattern.
std/io.zig
Outdated
@@ -245,6 +263,18 @@ pub const File = struct { | |||
} | |||
return result; | |||
}, | |||
Os.windows => { | |||
var pos : usize = undefined; | |||
if (system.SetFilePointerEx(self.handle, 0, @ptrCast(system.PLARGE_INTEGER, &pos), system.FILE_CURRENT) == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LARGE_INTEGER
is always an i64
. usize
is pointer-sized. These are not necessarily the same byte size. Also the signed-ness is different. So part of the job of this function is to get the i64 from Windows, and then convert it to a usize
, making correct decisions about how to handle the following situations:
- when usize is smaller than i64 (probably return
error.FilePosLargerThanPointerRange
, which is maybe a hint that we should actually return u64 instead of usize from getPos, but that's a separate issue, which could be fixed later) - when the i64 we get from microsoft is negative instead of positive (maybe we assert that it will not happen? maybe we return an error?)
Generally, @ptrCast
should be avoided if possible, and this is a situation where I don't think we need one.
std/io.zig
Outdated
@@ -224,6 +233,15 @@ pub const File = struct { | |||
}; | |||
} | |||
}, | |||
Os.windows => { | |||
if (system.SetFilePointerEx(self.handle, @bitCast(isize, pos), null, system.FILE_BEGIN) == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bitcasting pos
to isize
could produce an invalid value if pos has a 1 for the highest-order bit. We should handle this edge case correctly. perhaps std.math.cast
could be of use.
…e don't want to compare pos to @MaxValue(usize).
No description provided.