Skip to content

Commit

Permalink
Use libc::syscall instead of libc::copy_file_range
Browse files Browse the repository at this point in the history
It seems that many Linux distros don't include copy_file_range in their
C libraries.
  • Loading branch information
asomers committed Feb 10, 2023
1 parent 0b4afa7 commit 497bd4b
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,16 +645,34 @@ pub fn copy_file_range<Fd1: AsFd, Fd2: AsFd>(
.map(|offset| offset as *mut type_of_off)
.unwrap_or(ptr::null_mut());

let ret = unsafe {
libc::copy_file_range(
fd_in.as_fd().as_raw_fd(),
off_in,
fd_out.as_fd().as_raw_fd(),
off_out,
len,
0,
)
};
cfg_if::cfg_if! {
if #[cfg(target_os = "freebsd")] {
let ret = unsafe {
libc::copy_file_range(
fd_in.as_fd().as_raw_fd(),
off_in,
fd_out.as_fd().as_raw_fd(),
off_out,
len,
0,
)
};
} else {
// May Linux distros still don't include copy_file_range in their
// libc implementations, so we need to make a direct syscall.
let ret = unsafe {
libc::syscall(
libc::SYS_copy_file_range,
fd_in,
off_in,
fd_out.as_fd().as_raw_fd(),
off_out,
len,
0,
)
};
}
}
Errno::result(ret).map(|r| r as usize)
}

Expand Down

0 comments on commit 497bd4b

Please sign in to comment.