diff options
| author | LemonBoy <thatlemon@gmail.com> | 2020-03-12 22:46:12 +0100 |
|---|---|---|
| committer | LemonBoy <thatlemon@gmail.com> | 2020-03-13 08:45:37 +0100 |
| commit | de53537f10e22cc003cfbc77468349b758470f24 (patch) | |
| tree | 8d8685fa2ec6d44ea0b73be7a719da990a7f5663 /lib/std/os.zig | |
| parent | bd0b51477a6cb0dc7ea7739f61a70110d39ba601 (diff) | |
| download | zig-de53537f10e22cc003cfbc77468349b758470f24.tar.gz zig-de53537f10e22cc003cfbc77468349b758470f24.zip | |
Add NtDll-based ftruncate implementation
Diffstat (limited to 'lib/std/os.zig')
| -rw-r--r-- | lib/std/os.zig | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/lib/std/os.zig b/lib/std/os.zig index 6614453a38..2c18a13250 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -447,10 +447,25 @@ pub const TruncateError = error{ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void { if (std.Target.current.os.tag == .windows) { - try windows.SetFilePointerEx_BEGIN(fd, length); + var io_status_block: windows.IO_STATUS_BLOCK = undefined; + var eof_info = windows.FILE_END_OF_FILE_INFORMATION{ + .EndOfFile = @bitCast(windows.LARGE_INTEGER, length), + }; + + const rc = windows.ntdll.NtSetInformationFile( + fd, + &io_status_block, + &eof_info, + @sizeOf(windows.FILE_END_OF_FILE_INFORMATION), + .FileEndOfFileInformation, + ); - if (windows.kernel32.SetEndOfFile(fd) == 0) - return TruncateError.Unexpected; + switch (rc) { + .SUCCESS => {}, + .INVALID_HANDLE => unreachable, // Handle not open for writing + .ACCESS_DENIED => return error.CannotTruncate, + else => return windows.unexpectedStatus(rc), + } return; } @@ -471,7 +486,8 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void { EIO => return error.InputOutput, EPERM => return error.CannotTruncate, ETXTBSY => return error.FileBusy, - EBADF, EINVAL => unreachable, + EBADF => unreachable, // Handle not open for writing + EINVAL => unreachable, // Handle not open for writing else => |err| return unexpectedErrno(err), } } |
