Address review comments & fix compilation errors

This commit is contained in:
LemonBoy 2020-10-06 11:57:23 +02:00
parent a419a1aabc
commit 1f7ec0de70
2 changed files with 16 additions and 16 deletions

View file

@ -2270,14 +2270,14 @@ const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.Send
fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void { fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void {
if (comptime std.Target.current.isDarwin()) { if (comptime std.Target.current.isDarwin()) {
const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA); const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA);
switch (errno(rc)) { switch (os.errno(rc)) {
0 => return, 0 => return,
EINVAL => unreachable, os.EINVAL => unreachable,
ENOMEM => return error.SystemResources, os.ENOMEM => return error.SystemResources,
// The source file was not a directory, symbolic link, or regular file. // The source file is not a directory, symbolic link, or regular file.
// Try with the fallback path before giving up. // Try with the fallback path before giving up.
ENOTSUP => {}, os.ENOTSUP => {},
else => |err| return unexpectedErrno(err), else => |err| return os.unexpectedErrno(err),
} }
} }
@ -2286,9 +2286,9 @@ fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void {
// most efficient method (if available). // most efficient method (if available).
var offset: u64 = 0; var offset: u64 = 0;
cfr_loop: while (true) { cfr_loop: while (true) {
// The kernel checks `offset+count` for overflow, use a 32 bit // The kernel checks the u64 value `offset+count` for overflow, use
// value so that the syscall won't return EINVAL except for // a 32 bit value so that the syscall won't return EINVAL except for
// impossibly large files. // impossibly large files (> 2^64-1 - 2^32-1).
const amt = try os.copy_file_range(fd_in, offset, fd_out, offset, math.maxInt(u32), 0); const amt = try os.copy_file_range(fd_in, offset, fd_out, offset, math.maxInt(u32), 0);
// Terminate when no data was copied // Terminate when no data was copied
if (amt == 0) break :cfr_loop; if (amt == 0) break :cfr_loop;

View file

@ -4954,6 +4954,11 @@ pub const CopyFileRangeError = error{
FileBusy, FileBusy,
} || PReadError || PWriteError || UnexpectedError; } || 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;
break :init std.atomic.Int(bool).init(kernel_has_syscall);
};
/// Transfer data between file descriptors at specified offsets. /// Transfer data between file descriptors at specified offsets.
/// Returns the number of bytes written, which can less than requested. /// Returns the number of bytes written, which can less than requested.
/// ///
@ -4979,16 +4984,11 @@ pub const CopyFileRangeError = error{
/// Other systems fall back to calling `pread` / `pwrite`. /// Other systems fall back to calling `pread` / `pwrite`.
/// ///
/// Maximum offsets on Linux are `math.maxInt(i64)`. /// Maximum offsets on Linux are `math.maxInt(i64)`.
var has_copy_file_range_syscall = init: {
const kernel_has_syscall = comptime std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true;
break :init std.atomic.Int(u1).init(@boolToInt(kernel_has_syscall));
};
pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len: usize, flags: u32) CopyFileRangeError!usize { pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len: usize, flags: u32) CopyFileRangeError!usize {
const use_c = std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok; const use_c = std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok;
if (std.Target.current.os.tag == .linux and if (std.Target.current.os.tag == .linux and
(use_c or has_copy_file_range_syscall.get() != 0)) (use_c or has_copy_file_range_syscall.get()))
{ {
const sys = if (use_c) std.c else linux; const sys = if (use_c) std.c else linux;
@ -5013,7 +5013,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
EXDEV => {}, EXDEV => {},
// syscall added in Linux 4.5, use fallback // syscall added in Linux 4.5, use fallback
ENOSYS => { ENOSYS => {
has_copy_file_range_syscall.set(0); has_copy_file_range_syscall.set(false);
}, },
else => |err| return unexpectedErrno(err), else => |err| return unexpectedErrno(err),
} }