Skip to content

Commit 390c462

Browse files
committed
std.os: take advantage of the freebsd's copy_file_range
1 parent cae76d8 commit 390c462

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

lib/std/c/freebsd.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ pub const E = enum(u16) {
14491449
CAPMODE = 94, // Not permitted in capability mode
14501450
NOTRECOVERABLE = 95, // State not recoverable
14511451
OWNERDEAD = 96, // Previous owner died
1452+
INTEGRITY = 97, // Integrity check failed
14521453
_,
14531454
};
14541455

@@ -1884,3 +1885,4 @@ pub const MFD = struct {
18841885
};
18851886

18861887
pub extern "c" fn memfd_create(name: [*:0]const u8, flags: c_uint) c_int;
1888+
pub extern "c" fn copy_file_range(fd_in: fd_t, off_in: ?*off_t, fd_out: fd_t, off_out: ?*off_t, len: usize, flags: u32) usize;

lib/std/os.zig

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6256,6 +6256,7 @@ pub const CopyFileRangeError = error{
62566256
Unseekable,
62576257
PermissionDenied,
62586258
SwapFile,
6259+
CorruptedData,
62596260
} || PReadError || PWriteError || UnexpectedError;
62606261

62616262
var has_copy_file_range_syscall = std.atomic.Atomic(bool).init(true);
@@ -6281,44 +6282,53 @@ var has_copy_file_range_syscall = std.atomic.Atomic(bool).init(true);
62816282
///
62826283
/// These systems support in-kernel data copying:
62836284
/// * Linux 4.5 (cross-filesystem 5.3)
6285+
/// * FreeBSD 13.0
62846286
///
62856287
/// Other systems fall back to calling `pread` / `pwrite`.
62866288
///
6287-
/// Maximum offsets on Linux are `math.maxInt(i64)`.
6289+
/// Maximum offsets on Linux and FreeBSD are `math.maxInt(i64)`.
62886290
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 {
6289-
const call_cfr = comptime if (builtin.os.tag == .wasi)
6290-
// WASI-libc doesn't have copy_file_range.
6291-
false
6292-
else if (builtin.link_libc)
6293-
std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok
6294-
else
6295-
builtin.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true;
6296-
6297-
if (call_cfr and has_copy_file_range_syscall.load(.Monotonic)) {
6291+
if ((comptime builtin.os.isAtLeast(.freebsd, .{ .major = 13, .minor = 0 }) orelse true) or
6292+
((comptime builtin.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true and
6293+
std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok) and
6294+
has_copy_file_range_syscall.load(.Monotonic)))
6295+
{
62986296
var off_in_copy = @bitCast(i64, off_in);
62996297
var off_out_copy = @bitCast(i64, off_out);
63006298

6301-
const rc = system.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
6302-
switch (system.getErrno(rc)) {
6303-
.SUCCESS => return @intCast(usize, rc),
6304-
.BADF => return error.FilesOpenedWithWrongFlags,
6305-
.FBIG => return error.FileTooBig,
6306-
.IO => return error.InputOutput,
6307-
.ISDIR => return error.IsDir,
6308-
.NOMEM => return error.OutOfMemory,
6309-
.NOSPC => return error.NoSpaceLeft,
6310-
.OVERFLOW => return error.Unseekable,
6311-
.PERM => return error.PermissionDenied,
6312-
.TXTBSY => return error.SwapFile,
6313-
// these may not be regular files, try fallback
6314-
.INVAL => {},
6315-
// support for cross-filesystem copy added in Linux 5.3, use fallback
6316-
.XDEV => {},
6317-
// syscall added in Linux 4.5, use fallback
6318-
.NOSYS => {
6319-
has_copy_file_range_syscall.store(false, .Monotonic);
6320-
},
6321-
else => |err| return unexpectedErrno(err),
6299+
while (true) {
6300+
const rc = system.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
6301+
switch (system.getErrno(rc)) {
6302+
.SUCCESS => return @intCast(usize, rc),
6303+
.BADF => return error.FilesOpenedWithWrongFlags,
6304+
.FBIG => return error.FileTooBig,
6305+
.IO => return error.InputOutput,
6306+
.ISDIR => return error.IsDir,
6307+
.NOSPC => return error.NoSpaceLeft,
6308+
.INVAL => break, // these may not be regular files, try fallback
6309+
else => |err| switch (builtin.os.tag) {
6310+
// Linux-specific errors
6311+
.linux => switch (err) {
6312+
.NOMEM => return error.OutOfMemory,
6313+
.OVERFLOW => return error.Unseekable,
6314+
.PERM => return error.PermissionDenied,
6315+
.TXTBSY => return error.SwapFile,
6316+
.XDEV => break, // support for cross-filesystem copy added in Linux 5.3, use fallback
6317+
.NOSYS => { // syscall added in Linux 4.5, use fallback
6318+
has_copy_file_range_syscall.store(false, .Monotonic);
6319+
break;
6320+
},
6321+
else => return unexpectedErrno(err),
6322+
},
6323+
// FreeBSD-specific errors
6324+
.freebsd => switch (err) {
6325+
.INTEGRITY => return error.CorruptedData,
6326+
.INTR => continue,
6327+
else => return unexpectedErrno(err),
6328+
},
6329+
else => unreachable,
6330+
},
6331+
}
63226332
}
63236333
}
63246334

src/link.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ pub const File = struct {
433433
Unseekable,
434434
PermissionDenied,
435435
SwapFile,
436+
CorruptedData,
436437
SystemResources,
437438
OperationAborted,
438439
BrokenPipe,

0 commit comments

Comments
 (0)