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;...@@ -24,8 +24,6 @@ pub const COPYFILE_XATTR = 1 << 2;
24pub const COPYFILE_DATA = 1 << 3;24pub const COPYFILE_DATA = 1 << 3;
2525
26pub const copyfile_state_t = *@Type(.Opaque);26pub 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;
29pub extern "c" fn fcopyfile(from: fd_t, to: fd_t, state: ?copyfile_state_t, flags: u32) c_int;27pub extern "c" fn fcopyfile(from: fd_t, to: fd_t, state: ?copyfile_state_t, flags: u32) c_int;
3028
31pub extern "c" fn @"realpath$DARWIN_EXTSN"(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;29pub 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 {...@@ -2265,8 +2265,9 @@ pub fn realpathAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 {
22652265
2266const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError;2266const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError;
22672267
2268/// Transfer all the data between two file descriptors in the most efficient way.2268// Transfer all the data between two file descriptors in the most efficient way.
2269/// No metadata is transferred over.2269// The copy starts at offset 0, the initial offsets are preserved.
2270// No metadata is transferred over.
2270fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void {2271fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void {
2271 if (comptime std.Target.current.isDarwin()) {2272 if (comptime std.Target.current.isDarwin()) {
2272 const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA);2273 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(...@@ -4945,7 +4945,9 @@ pub fn sendfile(
4945pub const CopyFileRangeError = error{4945pub const CopyFileRangeError = error{
4946 FileTooBig,4946 FileTooBig,
4947 InputOutput,4947 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,
4949 IsDir,4951 IsDir,
4950 OutOfMemory,4952 OutOfMemory,
4951 NoSpaceLeft,4953 NoSpaceLeft,
...@@ -4955,7 +4957,7 @@ pub const CopyFileRangeError = error{...@@ -4955,7 +4957,7 @@ pub const CopyFileRangeError = error{
4955} || PReadError || PWriteError || UnexpectedError;4957} || PReadError || PWriteError || UnexpectedError;
49564958
4957var has_copy_file_range_syscall = init: {4959var 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;
4959 break :init std.atomic.Int(bool).init(kernel_has_syscall);4961 break :init std.atomic.Int(bool).init(kernel_has_syscall);
4960};4962};
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...@@ -4998,7 +5000,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
4998 const rc = sys.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);5000 const rc = sys.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
4999 switch (sys.getErrno(rc)) {5001 switch (sys.getErrno(rc)) {
5000 0 => return @intCast(usize, rc),5002 0 => return @intCast(usize, rc),
5001 EBADF => return error.InvalidFileDescriptor,5003 EBADF => return error.FilesOpenedWithWrongFlags,
5002 EFBIG => return error.FileTooBig,5004 EFBIG => return error.FileTooBig,
5003 EIO => return error.InputOutput,5005 EIO => return error.InputOutput,
5004 EISDIR => return error.IsDir,5006 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...@@ -5019,24 +5021,13 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
5019 }5021 }
5020 }5022 }
50215023
5022 var buf: [2 * 4096]u8 = undefined;5024 var buf: [8 * 4096]u8 = undefined;
50235025 const adjusted_count = math.min(buf.len, len);
5024 var total_copied: usize = 0;5026 const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in);
5025 var read_off = off_in;5027 // TODO without @as the line below fails to compile for wasm32-wasi:
5026 var write_off = off_out;5028 // error: integer value 0 cannot be coerced to type 'os.PWriteError!usize'
5027 while (total_copied < len) {5029 if (amt_read == 0) return @as(usize, 0);
5028 const adjusted_count = math.min(buf.len, len - total_copied);5030 return pwrite(fd_out, buf[0..amt_read], off_out);
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;
5040}5031}
50415032
5042pub const PollError = error{5033pub const PollError = error{