Skip to content

Commit 670be42

Browse files
committed
std.os: take advantage of the freebsd's copy_file_range
1 parent 14c173b commit 670be42

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
@@ -1440,6 +1440,7 @@ pub const E = enum(u16) {
14401440
CAPMODE = 94, // Not permitted in capability mode
14411441
NOTRECOVERABLE = 95, // State not recoverable
14421442
OWNERDEAD = 96, // Previous owner died
1443+
INTEGRITY = 97, // Integrity check failed
14431444
_,
14441445
};
14451446

@@ -1875,3 +1876,4 @@ pub const MFD = struct {
18751876
};
18761877

18771878
pub extern "c" fn memfd_create(name: [*:0]const u8, flags: c_uint) c_int;
1879+
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
@@ -6262,6 +6262,7 @@ pub const CopyFileRangeError = error{
62626262
Unseekable,
62636263
PermissionDenied,
62646264
SwapFile,
6265+
CorruptedData,
62656266
} || PReadError || PWriteError || UnexpectedError;
62666267

62676268
var has_copy_file_range_syscall = std.atomic.Atomic(bool).init(true);
@@ -6287,44 +6288,56 @@ var has_copy_file_range_syscall = std.atomic.Atomic(bool).init(true);
62876288
///
62886289
/// These systems support in-kernel data copying:
62896290
/// * Linux 4.5 (cross-filesystem 5.3)
6291+
/// * FreeBSD 13.0
62906292
///
62916293
/// Other systems fall back to calling `pread` / `pwrite`.
62926294
///
6293-
/// Maximum offsets on Linux are `math.maxInt(i64)`.
6295+
/// Maximum offsets on Linux and FreeBSD are `math.maxInt(i64)`.
62946296
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 {
6295-
const call_cfr = comptime if (builtin.os.tag == .wasi)
6296-
// WASI-libc doesn't have copy_file_range.
6297-
false
6298-
else if (builtin.link_libc)
6299-
std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok
6300-
else
6301-
builtin.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true;
6302-
6303-
if (call_cfr and has_copy_file_range_syscall.load(.Monotonic)) {
6297+
if ((comptime builtin.os.isAtLeast(.freebsd, .{ .major = 13, .minor = 0 }) orelse false) or
6298+
((comptime builtin.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse false and
6299+
std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok) and
6300+
has_copy_file_range_syscall.load(.Monotonic)))
6301+
{
63046302
var off_in_copy = @bitCast(i64, off_in);
63056303
var off_out_copy = @bitCast(i64, off_out);
63066304

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

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)