authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-03-12 19:40:42+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-03-12 19:40:42+01:00
logbd0b51477a6cb0dc7ea7739f61a70110d39ba601
treea323e5300f31f73132ab371b8475ee42c219dbb2
parent11df0d0cf8c786d5b502c21a42d47631657a42f4

Address review comments


2 files changed, 15 insertions(+), 9 deletions(-)

lib/std/fs/file.zig+1-1
...@@ -104,7 +104,7 @@ pub const File = struct {...@@ -104,7 +104,7 @@ pub const File = struct {
104 /// Shrinks or expands the file.104 /// Shrinks or expands the file.
105 /// The file offset after this call is undefined.105 /// The file offset after this call is undefined.
106 pub fn setEndPos(self: File, length: u64) SetEndPosError!void {106 pub fn setEndPos(self: File, length: u64) SetEndPosError!void {
107 try os.truncate(self.handle, length);107 try os.ftruncate(self.handle, length);
108 }108 }
109109
110 pub const SeekError = os.SeekError;110 pub const SeekError = os.SeekError;
lib/std/os.zig+14-8
...@@ -439,11 +439,13 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {...@@ -439,11 +439,13 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
439}439}
440440
441pub const TruncateError = error{441pub const TruncateError = error{
442 /// The file descriptor is not open for writing.442 FileTooBig,
443 NotFile,443 InputOutput,
444 CannotTruncate,
445 FileBusy,
444} || UnexpectedError;446} || UnexpectedError;
445447
446pub fn truncate(fd: fd_t, length: u64) TruncateError!void {448pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
447 if (std.Target.current.os.tag == .windows) {449 if (std.Target.current.os.tag == .windows) {
448 try windows.SetFilePointerEx_BEGIN(fd, length);450 try windows.SetFilePointerEx_BEGIN(fd, length);
449451
...@@ -454,18 +456,22 @@ pub fn truncate(fd: fd_t, length: u64) TruncateError!void {...@@ -454,18 +456,22 @@ pub fn truncate(fd: fd_t, length: u64) TruncateError!void {
454 }456 }
455457
456 while (true) {458 while (true) {
457 const rc = if (builtin.link_libc) blk: {459 const rc = if (builtin.link_libc)
458 if (std.Target.current.os.tag == .linux)460 if (std.Target.current.os.tag == .linux)
459 break :blk system.ftruncate64(fd, @bitCast(off_t, length))461 system.ftruncate64(fd, @bitCast(off_t, length))
460 else462 else
461 break :blk system.ftruncate(fd, @bitCast(off_t, length));463 system.ftruncate(fd, @bitCast(off_t, length))
462 } else464 else
463 system.ftruncate(fd, length);465 system.ftruncate(fd, length);
464466
465 switch (errno(rc)) {467 switch (errno(rc)) {
466 0 => return,468 0 => return,
467 EINTR => continue,469 EINTR => continue,
468 EBADF, EINVAL => return error.NotFile,470 EFBIG => return error.FileTooBig,
471 EIO => return error.InputOutput,
472 EPERM => return error.CannotTruncate,
473 ETXTBSY => return error.FileBusy,
474 EBADF, EINVAL => unreachable,
469 else => |err| return unexpectedErrno(err),475 else => |err| return unexpectedErrno(err),
470 }476 }
471 }477 }