Skip to content

Commit 8931d0b

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

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-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: 44 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,56 @@ 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 false) or
6292+
((comptime builtin.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse false 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+
if (builtin.os.tag == .freebsd) {
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+
.NOSPC => return error.NoSpaceLeft,
6309+
.INVAL => break, // these may not be regular files, try fallback
6310+
.INTEGRITY => return error.CorruptedData,
6311+
.INTR => continue,
6312+
else => |err| return unexpectedErrno(err),
6313+
}
6314+
} else { // assume linux
6315+
switch (system.getErrno(rc)) {
6316+
.SUCCESS => return @intCast(usize, rc),
6317+
.BADF => return error.FilesOpenedWithWrongFlags,
6318+
.FBIG => return error.FileTooBig,
6319+
.IO => return error.InputOutput,
6320+
.ISDIR => return error.IsDir,
6321+
.NOSPC => return error.NoSpaceLeft,
6322+
.INVAL => break, // these may not be regular files, try fallback
6323+
.NOMEM => return error.OutOfMemory,
6324+
.OVERFLOW => return error.Unseekable,
6325+
.PERM => return error.PermissionDenied,
6326+
.TXTBSY => return error.SwapFile,
6327+
.XDEV => break, // support for cross-filesystem copy added in Linux 5.3, use fallback
6328+
.NOSYS => { // syscall added in Linux 4.5, use fallback
6329+
has_copy_file_range_syscall.store(false, .Monotonic);
6330+
break;
6331+
},
6332+
else => |err| return unexpectedErrno(err),
6333+
}
6334+
}
63226335
}
63236336
}
63246337

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)