Commit bc512648db

rpkak <rpkak@users.noreply.github.com>
2025-08-31 10:56:09
use copy_file_range syscall on linux
1 parent 6260d27
Changed files (3)
lib/std/fs/File.zig
@@ -1916,7 +1916,7 @@ pub const Writer = struct {
 
         const copy_file_range = switch (native_os) {
             .freebsd => std.os.freebsd.copy_file_range,
-            .linux => if (std.c.versionCheck(if (builtin.abi.isAndroid()) .{ .major = 34, .minor = 0, .patch = 0 } else .{ .major = 2, .minor = 27, .patch = 0 })) std.os.linux.wrapped.copy_file_range else {},
+            .linux => std.os.linux.wrapped.copy_file_range,
             else => {},
         };
         if (@TypeOf(copy_file_range) != void) cfr: {
lib/std/os/linux.zig
@@ -9891,7 +9891,9 @@ pub const wrapped = struct {
     };
 
     pub fn copy_file_range(fd_in: fd_t, off_in: ?*i64, fd_out: fd_t, off_out: ?*i64, len: usize, flags: u32) CopyFileRangeError!usize {
-        const rc = system.copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
+        const use_c = std.c.versionCheck(if (builtin.abi.isAndroid()) .{ .major = 34, .minor = 0, .patch = 0 } else .{ .major = 2, .minor = 27, .patch = 0 });
+        const sys = if (use_c) std.c else std.os.linux;
+        const rc = sys.copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
         switch (errno(rc)) {
             .SUCCESS => return @intCast(rc),
             .BADF => return error.BadFileFlags,
lib/std/posix.zig
@@ -6515,14 +6515,16 @@ pub const CopyFileRangeError = error{
 ///
 /// Maximum offsets on Linux and FreeBSD are `maxInt(i64)`.
 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 {
-    if (builtin.os.tag == .freebsd or
-        (comptime builtin.os.tag == .linux and std.c.versionCheck(if (builtin.abi.isAndroid()) .{ .major = 34, .minor = 0, .patch = 0 } else .{ .major = 2, .minor = 27, .patch = 0 })))
-    {
+    if (builtin.os.tag == .freebsd or builtin.os.tag == .linux) {
+        const use_c = native_os != .linux or
+            std.c.versionCheck(if (builtin.abi.isAndroid()) .{ .major = 34, .minor = 0, .patch = 0 } else .{ .major = 2, .minor = 27, .patch = 0 });
+        const sys = if (use_c) std.c else linux;
+
         var off_in_copy: i64 = @bitCast(off_in);
         var off_out_copy: i64 = @bitCast(off_out);
 
         while (true) {
-            const rc = system.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
+            const rc = sys.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
             if (native_os == .freebsd) {
                 switch (errno(rc)) {
                     .SUCCESS => return @intCast(rc),