authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-10-07 11:13:26+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-10-07 11:13:26+02:00
log03762da2af8753ecf7f4bc2005dd00d570c51ae7
tree32f0ae1d26475b2fe40849ac5a597e3c82a2f38d
parent1f7ec0de706eded887bc8dbcf5f45a5546d1be5c

New review round


3 files changed, 15 insertions(+), 25 deletions(-)

lib/std/c/darwin.zig-2
......@@ -24,8 +24,6 @@ pub const COPYFILE_XATTR = 1 << 2;
2424pub const COPYFILE_DATA = 1 << 3;
2525
2626pub const copyfile_state_t = *@Type(.Opaque);
27pub extern "c" fn copyfile_state_alloc() copyfile_state_t;
28pub extern "c" fn copyfile_state_free(state: copyfile_state_t) c_int;
2927pub extern "c" fn fcopyfile(from: fd_t, to: fd_t, state: ?copyfile_state_t, flags: u32) c_int;
3028
3129pub extern "c" fn @"realpath$DARWIN_EXTSN"(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
lib/std/fs.zig+3-2
......@@ -2265,8 +2265,9 @@ pub fn realpathAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 {
22652265
22662266const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError;
22672267
2268/// Transfer all the data between two file descriptors in the most efficient way.
2269/// No metadata is transferred over.
2268// Transfer all the data between two file descriptors in the most efficient way.
2269// The copy starts at offset 0, the initial offsets are preserved.
2270// No metadata is transferred over.
22702271fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void {
22712272 if (comptime std.Target.current.isDarwin()) {
22722273 const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA);
lib/std/os.zig+12-21
......@@ -4945,7 +4945,9 @@ pub fn sendfile(
49454945pub const CopyFileRangeError = error{
49464946 FileTooBig,
49474947 InputOutput,
4948 InvalidFileDescriptor,
4948 /// `fd_in` is not open for reading; or `fd_out` is not open for writing;
4949 /// or the `O_APPEND` flag is set for `fd_out`.
4950 FilesOpenedWithWrongFlags,
49494951 IsDir,
49504952 OutOfMemory,
49514953 NoSpaceLeft,
......@@ -4955,7 +4957,7 @@ pub const CopyFileRangeError = error{
49554957} || PReadError || PWriteError || UnexpectedError;
49564958
49574959var has_copy_file_range_syscall = init: {
4958 const kernel_has_syscall = comptime std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true;
4960 const kernel_has_syscall = std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true;
49594961 break :init std.atomic.Int(bool).init(kernel_has_syscall);
49604962};
49614963
......@@ -4998,7 +5000,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
49985000 const rc = sys.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
49995001 switch (sys.getErrno(rc)) {
50005002 0 => return @intCast(usize, rc),
5001 EBADF => return error.InvalidFileDescriptor,
5003 EBADF => return error.FilesOpenedWithWrongFlags,
50025004 EFBIG => return error.FileTooBig,
50035005 EIO => return error.InputOutput,
50045006 EISDIR => return error.IsDir,
......@@ -5019,24 +5021,13 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
50195021 }
50205022 }
50215023
5022 var buf: [2 * 4096]u8 = undefined;
5023
5024 var total_copied: usize = 0;
5025 var read_off = off_in;
5026 var write_off = off_out;
5027 while (total_copied < len) {
5028 const adjusted_count = math.min(buf.len, len - total_copied);
5029 const amt_read = try pread(fd_in, buf[0..adjusted_count], read_off);
5030 if (amt_read == 0) break;
5031 const amt_written = try pwrite(fd_out, buf[0..amt_read], write_off);
5032 // pwrite may write less than the specified amount, handle the remaining
5033 // chunk of data in the next iteration
5034 read_off += amt_written;
5035 write_off += amt_written;
5036 total_copied += amt_written;
5037 }
5038
5039 return total_copied;
5024 var buf: [8 * 4096]u8 = undefined;
5025 const adjusted_count = math.min(buf.len, len);
5026 const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in);
5027 // TODO without @as the line below fails to compile for wasm32-wasi:
5028 // error: integer value 0 cannot be coerced to type 'os.PWriteError!usize'
5029 if (amt_read == 0) return @as(usize, 0);
5030 return pwrite(fd_out, buf[0..amt_read], off_out);
50405031}
50415032
50425033pub const PollError = error{