authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 23:44:38-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 23:46:43-08:00
logace08ba642361d57268b1f102c371b060a1f9a2c
treedd32ca5898cee7afe81b6139b32df398514b1448
parenta901e9241396a11f807bf40bbd548e25f120deab

std.Io.Threaded: prevent 0-byte file writes

these cause EINVAL on darwin and are generally a wasted syscall. and remove a bogus error from File.Writer.Error.

2 files changed, 6 insertions(+), 3 deletions(-)

lib/std/Io/File/Writer.zig-1
...@@ -26,7 +26,6 @@ pub const Error = error{...@@ -26,7 +26,6 @@ pub const Error = error{
26 InputOutput,26 InputOutput,
27 NoSpaceLeft,27 NoSpaceLeft,
28 DeviceBusy,28 DeviceBusy,
29 InvalidArgument,
30 /// File descriptor does not hold the required rights to write to it.29 /// File descriptor does not hold the required rights to write to it.
31 AccessDenied,30 AccessDenied,
32 PermissionDenied,31 PermissionDenied,
lib/std/Io/Threaded.zig+6-2
...@@ -7449,6 +7449,8 @@ fn fileWritePositional(...@@ -7449,6 +7449,8 @@ fn fileWritePositional(
7449 },7449 },
7450 };7450 };
74517451
7452 if (iovlen == 0) return 0;
7453
7452 if (native_os == .wasi and !builtin.link_libc) {7454 if (native_os == .wasi and !builtin.link_libc) {
7453 var n_written: usize = undefined;7455 var n_written: usize = undefined;
7454 try current_thread.beginSyscall();7456 try current_thread.beginSyscall();
...@@ -7502,7 +7504,7 @@ fn fileWritePositional(...@@ -7502,7 +7504,7 @@ fn fileWritePositional(
7502 else => |e| {7504 else => |e| {
7503 current_thread.endSyscall();7505 current_thread.endSyscall();
7504 switch (e) {7506 switch (e) {
7505 .INVAL => return error.InvalidArgument,7507 .INVAL => |err| return errnoBug(err),
7506 .FAULT => |err| return errnoBug(err),7508 .FAULT => |err| return errnoBug(err),
7507 .AGAIN => return error.WouldBlock,7509 .AGAIN => return error.WouldBlock,
7508 .BADF => return error.NotOpenForWriting, // Usually a race condition.7510 .BADF => return error.NotOpenForWriting, // Usually a race condition.
...@@ -7569,6 +7571,8 @@ fn fileWriteStreaming(...@@ -7569,6 +7571,8 @@ fn fileWriteStreaming(
7569 },7571 },
7570 };7572 };
75717573
7574 if (iovlen == 0) return 0;
7575
7572 if (native_os == .wasi and !builtin.link_libc) {7576 if (native_os == .wasi and !builtin.link_libc) {
7573 var n_written: usize = undefined;7577 var n_written: usize = undefined;
7574 try current_thread.beginSyscall();7578 try current_thread.beginSyscall();
...@@ -7619,7 +7623,7 @@ fn fileWriteStreaming(...@@ -7619,7 +7623,7 @@ fn fileWriteStreaming(
7619 else => |e| {7623 else => |e| {
7620 current_thread.endSyscall();7624 current_thread.endSyscall();
7621 switch (e) {7625 switch (e) {
7622 .INVAL => return error.InvalidArgument,7626 .INVAL => |err| return errnoBug(err),
7623 .FAULT => |err| return errnoBug(err),7627 .FAULT => |err| return errnoBug(err),
7624 .AGAIN => return error.WouldBlock,7628 .AGAIN => return error.WouldBlock,
7625 .BADF => return error.NotOpenForWriting, // Can be a race condition.7629 .BADF => return error.NotOpenForWriting, // Can be a race condition.