Skip to content

Commit

Permalink
fcntl: change (vm)splice, tee using AsFd instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Apr 27, 2024
1 parent 213127b commit a243025
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
22 changes: 11 additions & 11 deletions src/fcntl.rs
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
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

0 comments on commit a243025

Please sign in to comment.