-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
106 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ mod readlink; | |
mod realpath; | ||
mod remove; | ||
mod rename; | ||
mod sendfile; | ||
mod stat; | ||
mod symlink; | ||
mod sync; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use rustix::fd::BorrowedFd; | ||
|
||
use errno::{set_errno, Errno}; | ||
use libc::{c_int, off64_t, off_t, size_t}; | ||
|
||
use crate::convert_res; | ||
|
||
#[cfg(any(target_os = "android", target_os = "linux"))] | ||
#[no_mangle] | ||
unsafe extern "C" fn sendfile( | ||
out_fd: c_int, | ||
in_fd: c_int, | ||
offset: *mut off_t, | ||
count: size_t, | ||
) -> isize { | ||
libc!(libc::sendfile(out_fd, in_fd, offset, count)); | ||
|
||
// Check for overflow into off64_t | ||
if !offset.is_null() { | ||
if *offset < 0 { | ||
set_errno(Errno(libc::EINVAL)); | ||
return -1; | ||
} | ||
|
||
// If count is too big then we just fail immediately | ||
if let Ok(add) = TryInto::<off_t>::try_into(count) { | ||
// Check if count + offset could overflow and return EINVAL | ||
if add.overflowing_add(*offset).1 { | ||
set_errno(Errno(libc::EINVAL)); | ||
return -1; | ||
} | ||
} else { | ||
set_errno(Errno(libc::EINVAL)); | ||
return -1; | ||
} | ||
} | ||
|
||
let mut offset_64: off64_t = 0; | ||
let res = sendfile64(out_fd, in_fd, &mut offset_64, count); | ||
|
||
if !offset.is_null() { | ||
// Safe cast as we checked above | ||
*offset = offset_64 as off_t; | ||
} | ||
return res; | ||
} | ||
|
||
#[cfg(any(target_os = "android", target_os = "linux"))] | ||
#[no_mangle] | ||
unsafe extern "C" fn sendfile64( | ||
out_fd: c_int, | ||
in_fd: c_int, | ||
offset: *mut off64_t, | ||
count: size_t, | ||
) -> isize { | ||
libc!(libc::sendfile64(out_fd, in_fd, offset, count)); | ||
|
||
let offset: *mut u64 = checked_cast!(offset); | ||
|
||
match convert_res(rustix::fs::sendfile( | ||
BorrowedFd::borrow_raw(out_fd), | ||
BorrowedFd::borrow_raw(in_fd), | ||
offset.as_mut(), | ||
count.into(), | ||
)) { | ||
Some(num) => num.try_into().unwrap(), | ||
None => -1, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use rustix::fd::BorrowedFd; | ||
use rustix::io::SpliceFlags; | ||
|
||
use libc::{c_int, c_uint, loff_t}; | ||
|
||
use crate::convert_res; | ||
|
||
#[cfg(any(target_os = "android", target_os = "linux"))] | ||
#[no_mangle] | ||
unsafe extern "C" fn splice( | ||
fd_in: c_int, | ||
off_in: *mut loff_t, | ||
fd_out: c_int, | ||
off_out: *mut loff_t, | ||
len: usize, | ||
flags: c_uint, | ||
) -> isize { | ||
libc!(libc::splice(fd_in, off_in, fd_out, off_out, len, flags)); | ||
|
||
let off_in: *mut u64 = checked_cast!(off_in); | ||
let off_out: *mut u64 = checked_cast!(off_out); | ||
|
||
match convert_res(rustix::io::splice( | ||
BorrowedFd::borrow_raw(fd_in), | ||
off_in.as_mut(), | ||
BorrowedFd::borrow_raw(fd_out), | ||
off_out.as_mut(), | ||
len, | ||
SpliceFlags::from_bits(flags as _).unwrap(), | ||
)) { | ||
Some(num) => num.try_into().unwrap(), | ||
None => -1, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters