Skip to content

Commit

Permalink
New review round
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonBoy committed Oct 7, 2020
1 parent 1f7ec0d commit 03762da
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
2 changes: 0 additions & 2 deletions lib/std/c/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ pub const COPYFILE_XATTR = 1 << 2;
pub const COPYFILE_DATA = 1 << 3;

pub const copyfile_state_t = *@Type(.Opaque);
pub extern "c" fn copyfile_state_alloc() copyfile_state_t;
pub extern "c" fn copyfile_state_free(state: copyfile_state_t) c_int;
pub extern "c" fn fcopyfile(from: fd_t, to: fd_t, state: ?copyfile_state_t, flags: u32) c_int;

pub extern "c" fn @"realpath$DARWIN_EXTSN"(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
Expand Down
5 changes: 3 additions & 2 deletions lib/std/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2265,8 +2265,9 @@ pub fn realpathAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 {

const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError;

/// Transfer all the data between two file descriptors in the most efficient way.
/// No metadata is transferred over.
// Transfer all the data between two file descriptors in the most efficient way.
// The copy starts at offset 0, the initial offsets are preserved.
// No metadata is transferred over.
fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void {
if (comptime std.Target.current.isDarwin()) {
const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA);
Expand Down
33 changes: 12 additions & 21 deletions lib/std/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4945,7 +4945,9 @@ pub fn sendfile(
pub const CopyFileRangeError = error{
FileTooBig,
InputOutput,
InvalidFileDescriptor,
/// `fd_in` is not open for reading; or `fd_out` is not open for writing;
/// or the `O_APPEND` flag is set for `fd_out`.
FilesOpenedWithWrongFlags,
IsDir,
OutOfMemory,
NoSpaceLeft,
Expand All @@ -4955,7 +4957,7 @@ pub const CopyFileRangeError = error{
} || PReadError || PWriteError || UnexpectedError;

var has_copy_file_range_syscall = init: {
const kernel_has_syscall = comptime std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true;
const kernel_has_syscall = std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true;
break :init std.atomic.Int(bool).init(kernel_has_syscall);
};

Expand Down Expand Up @@ -4998,7 +5000,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
const rc = sys.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
switch (sys.getErrno(rc)) {
0 => return @intCast(usize, rc),
EBADF => return error.InvalidFileDescriptor,
EBADF => return error.FilesOpenedWithWrongFlags,
EFBIG => return error.FileTooBig,
EIO => return error.InputOutput,
EISDIR => return error.IsDir,
Expand All @@ -5019,24 +5021,13 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
}
}

var buf: [2 * 4096]u8 = undefined;

var total_copied: usize = 0;
var read_off = off_in;
var write_off = off_out;
while (total_copied < len) {
const adjusted_count = math.min(buf.len, len - total_copied);
const amt_read = try pread(fd_in, buf[0..adjusted_count], read_off);
if (amt_read == 0) break;
const amt_written = try pwrite(fd_out, buf[0..amt_read], write_off);
// pwrite may write less than the specified amount, handle the remaining
// chunk of data in the next iteration
read_off += amt_written;
write_off += amt_written;
total_copied += amt_written;
}

return total_copied;
var buf: [8 * 4096]u8 = undefined;
const adjusted_count = math.min(buf.len, len);
const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in);
// TODO without @as the line below fails to compile for wasm32-wasi:
// error: integer value 0 cannot be coerced to type 'os.PWriteError!usize'
if (amt_read == 0) return @as(usize, 0);
return pwrite(fd_out, buf[0..amt_read], off_out);
}

pub const PollError = error{
Expand Down

0 comments on commit 03762da

Please sign in to comment.