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 {
104104 /// Shrinks or expands the file.
105105 /// The file offset after this call is undefined.
106106 pub fn setEndPos(self: File, length: u64) SetEndPosError!void {
107 try os.truncate(self.handle, length);
107 try os.ftruncate(self.handle, length);
108108 }
109109
110110 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 {
439439}
440440
441441pub const TruncateError = error{
442 /// The file descriptor is not open for writing.
443 NotFile,
442 FileTooBig,
443 InputOutput,
444 CannotTruncate,
445 FileBusy,
444446} || UnexpectedError;
445447
446pub fn truncate(fd: fd_t, length: u64) TruncateError!void {
448pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
447449 if (std.Target.current.os.tag == .windows) {
448450 try windows.SetFilePointerEx_BEGIN(fd, length);
449451
......@@ -454,18 +456,22 @@ pub fn truncate(fd: fd_t, length: u64) TruncateError!void {
454456 }
455457
456458 while (true) {
457 const rc = if (builtin.link_libc) blk: {
459 const rc = if (builtin.link_libc)
458460 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))
460462 else
461 break :blk system.ftruncate(fd, @bitCast(off_t, length));
462 } else
463 system.ftruncate(fd, @bitCast(off_t, length))
464 else
463465 system.ftruncate(fd, length);
464466
465467 switch (errno(rc)) {
466468 0 => return,
467469 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,
469475 else => |err| return unexpectedErrno(err),
470476 }
471477 }