Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fcntl: change (vm)splice, tee using AsFd instead. #2387

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2387.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed tee, splice and vmsplice RawFd arguments to AsFd.
22 changes: 11 additions & 11 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,10 +1054,10 @@ pub fn copy_file_range<Fd1: AsFd, Fd2: AsFd>(
/// # See Also
/// *[`splice`](https://man7.org/linux/man-pages/man2/splice.2.html)
#[cfg(linux_android)]
pub fn splice(
fd_in: RawFd,
pub fn splice<Fd1: AsFd, Fd2: AsFd>(
fd_in: Fd1,
off_in: Option<&mut libc::loff_t>,
fd_out: RawFd,
fd_out: Fd2,
off_out: Option<&mut libc::loff_t>,
len: usize,
flags: SpliceFFlags,
Expand All @@ -1070,7 +1070,7 @@ pub fn splice(
.unwrap_or(ptr::null_mut());

let ret = unsafe {
libc::splice(fd_in, off_in, fd_out, off_out, len, flags.bits())
libc::splice(fd_in.as_fd().as_raw_fd(), off_in, fd_out.as_fd().as_raw_fd(), off_out, len, flags.bits())
};
Errno::result(ret).map(|r| r as usize)
}
Expand All @@ -1080,13 +1080,13 @@ pub fn splice(
/// # See Also
/// *[`tee`](https://man7.org/linux/man-pages/man2/tee.2.html)
#[cfg(linux_android)]
pub fn tee(
fd_in: RawFd,
fd_out: RawFd,
pub fn tee<Fd1: AsFd, Fd2: AsFd>(
fd_in: Fd1,
fd_out: Fd2,
len: usize,
flags: SpliceFFlags,
) -> Result<usize> {
let ret = unsafe { libc::tee(fd_in, fd_out, len, flags.bits()) };
let ret = unsafe { libc::tee(fd_in.as_fd().as_raw_fd(), fd_out.as_fd().as_raw_fd(), len, flags.bits()) };
Errno::result(ret).map(|r| r as usize)
}

Expand All @@ -1095,14 +1095,14 @@ pub fn tee(
/// # See Also
/// *[`vmsplice`](https://man7.org/linux/man-pages/man2/vmsplice.2.html)
#[cfg(linux_android)]
pub fn vmsplice(
fd: RawFd,
pub fn vmsplice<F: AsFd>(
fd: F,
iov: &[std::io::IoSlice<'_>],
flags: SpliceFFlags,
) -> Result<usize> {
let ret = unsafe {
libc::vmsplice(
fd,
fd.as_fd().as_raw_fd(),
iov.as_ptr().cast(),
iov.len(),
flags.bits(),
Expand Down
20 changes: 6 additions & 14 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,9 @@ mod linux_android {

let (rd, wr) = pipe().unwrap();
let mut offset: loff_t = 5;
let res = splice(
tmp.as_raw_fd(),
Some(&mut offset),
wr.as_raw_fd(),
None,
2,
SpliceFFlags::empty(),
)
.unwrap();
let res =
splice(tmp, Some(&mut offset), wr, None, 2, SpliceFFlags::empty())
.unwrap();

assert_eq!(2, res);

Expand All @@ -381,9 +375,8 @@ mod linux_android {
let (rd2, wr2) = pipe().unwrap();

write(wr1, b"abc").unwrap();
let res =
tee(rd1.as_raw_fd(), wr2.as_raw_fd(), 2, SpliceFFlags::empty())
.unwrap();
let res = tee(rd1.try_clone().unwrap(), wr2, 2, SpliceFFlags::empty())
.unwrap();

assert_eq!(2, res);

Expand All @@ -406,8 +399,7 @@ mod linux_android {
let buf2 = b"defghi";
let iovecs = [IoSlice::new(&buf1[0..3]), IoSlice::new(&buf2[0..3])];

let res = vmsplice(wr.as_raw_fd(), &iovecs[..], SpliceFFlags::empty())
.unwrap();
let res = vmsplice(wr, &iovecs[..], SpliceFFlags::empty()).unwrap();

assert_eq!(6, res);

Expand Down