std.posix.ftruncate: handle NonResizable properly

This commit is contained in:
Andrew Kelley 2025-07-25 14:14:40 -07:00
parent 8752d80c84
commit 733b208256
2 changed files with 7 additions and 3 deletions

View File

@ -2029,7 +2029,10 @@ pub const Writer = struct {
switch (w.mode) {
.positional,
.positional_reading,
=> try w.file.setEndPos(w.pos),
=> w.file.setEndPos(w.pos) catch |err| switch (err) {
error.NonResizable => return,
else => |e| return e,
},
.streaming,
.streaming_reading,

View File

@ -1052,6 +1052,7 @@ pub const TruncateError = error{
FileBusy,
AccessDenied,
PermissionDenied,
NonResizable,
} || UnexpectedError;
/// Length must be positive when treated as an i64.
@ -1091,7 +1092,7 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
.PERM => return error.PermissionDenied,
.TXTBSY => return error.FileBusy,
.BADF => unreachable, // Handle not open for writing
.INVAL => unreachable, // Handle not open for writing, negative length, or non-resizable handle
.INVAL => return error.NonResizable,
.NOTCAPABLE => return error.AccessDenied,
else => |err| return unexpectedErrno(err),
}
@ -1107,7 +1108,7 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
.PERM => return error.PermissionDenied,
.TXTBSY => return error.FileBusy,
.BADF => unreachable, // Handle not open for writing
.INVAL => unreachable, // Handle not open for writing, negative length, or non-resizable handle
.INVAL => return error.NonResizable, // This is returned for /dev/null for example.
else => |err| return unexpectedErrno(err),
}
}