Skip to content

Commit

Permalink
changes from review.
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Nov 26, 2023
1 parent 8c32a36 commit 7f85f0d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
49 changes: 22 additions & 27 deletions src/sys/sendfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use cfg_if::cfg_if;
use std::os::unix::io::{AsFd, AsRawFd};
use std::marker::PhantomData;
use std::ptr;

use libc::{self, off_t};
Expand All @@ -21,7 +22,7 @@ use crate::Result;
/// `in_fd` must support `mmap`-like operations and therefore cannot be a socket.
///
/// For more information, see [the sendfile(2) man page.](https://man7.org/linux/man-pages/man2/sendfile.2.html) for Linux,
/// see [the sendfile(2) man page.](https://docs.oracle.com/cd/E88353_01/html/E37843/sendfile-3c.html) for Solaris.
/// see [the sendfile(2) man page.](https://illumos.org/man/3EXT/sendfile) for Solaris.
#[cfg(any(target_os = "android", target_os = "linux", solarish))]
pub fn sendfile<F1: AsFd, F2: AsFd>(
out_fd: F1,
Expand Down Expand Up @@ -121,46 +122,41 @@ cfg_if! {
}
}
} else if #[cfg(solarish)] {
use std::os::unix::io::BorrowedFd;

#[derive(Debug, Copy, Clone)]
/// Mapping of the raw C sendfilevec_t struct
/// no need to bother with `sfv_flag` since it needs to be always 0.
pub struct SendfileVec {
/// input file descriptor
pub fd: i32,
/// offset to read from
pub off: off_t,
/// size of the data to read
pub len: usize,
pub struct SendfileVec<'a> {
raw: libc::sendfilevec_t,
phantom: PhantomData<BorrowedFd<'a>>
}

const SFV_FD_SELF: i32 = -2;

impl SendfileVec {
impl<'a> SendfileVec<'a> {
/// initialises SendfileVec to send data directly from the process's address space
/// same in C with sfv_fd set to SFV_FD_SELF.
pub fn newself(
off: off_t,
len: usize
) -> SendfileVec {
SendfileVec{fd: SFV_FD_SELF, off, len}
) -> Self {
Self{raw: libc::sendfilevec_t{sfv_fd: SFV_FD_SELF, sfv_flag: 0, sfv_off: off, sfv_len: len}, phantom: PhantomData}
}

/// initialises SendfileVec to send data from `fd`.
pub fn new<F: AsFd>(
fd: F,
fd: &'a F,
off: off_t,
len: usize
) -> SendfileVec {
SendfileVec{fd: fd.as_fd().as_raw_fd(), off, len}
) -> SendfileVec<'a> {
let bfd = fd.as_fd();
Self{raw: libc::sendfilevec_t{sfv_fd: bfd.as_raw_fd(), sfv_flag: 0, sfv_off:off, sfv_len: len}, phantom: PhantomData}
}
}

fn tosendfilevec(&self) -> libc::sendfilevec_t {
libc::sendfilevec_t{
sfv_fd: self.fd,
sfv_flag: 0,
sfv_off: self.off,
sfv_len: self.len
}
impl From<SendfileVec<'_>> for libc::sendfilevec_t {
fn from<'a>(vec: SendfileVec) -> libc::sendfilevec_t {
vec.raw
}
}
}
Expand Down Expand Up @@ -340,19 +336,18 @@ cfg_if! {
/// The former allows to send data from a file descriptor through `fd`,
/// from an offset `off` and for a given amount of data `len`.
///
/// The latter allows to send data from the process' address space, from an offset `off`
/// The latter allows to send data from the process's address space, from an offset `off`
/// and for a given amount of data `len`.
///
/// For more information, see
/// [the sendfilev{3) man page.](https://docs.oracle.com/cd/E86824_01/html/E54768/sendfilev-3ext.html)
/// [the sendfilev(3) man page.](https://illumos.org/man/3EXT/sendfilev)
pub fn sendfilev<F: AsFd>(
out_sock: F,
vec: Vec<SendfileVec>
vec: &[SendfileVec]
) -> (Result<()>, usize) {
let rawvec: Vec<libc::sendfilevec_t> = vec.iter().map(|&v| v.tosendfilevec()).collect();
let mut len = 0usize;
let return_code = unsafe {
libc::sendfilev(out_sock.as_fd().as_raw_fd(), rawvec.as_ptr(), rawvec.len() as i32, &mut len)
libc::sendfilev(out_sock.as_fd().as_raw_fd(), vec.as_ptr() as *const libc::sendfilevec_t, vec.len() as i32, &mut len)
};
(Errno::result(return_code).and(Ok(())), len)
}
Expand Down
10 changes: 7 additions & 3 deletions test/test_sendfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,17 @@ fn test_sendfilev() {
.write_all(trailer_strings.concat().as_bytes())
.unwrap();
let (mut rd, wr) = UnixStream::pair().unwrap();
let vec: Vec<SendfileVec> = vec![
let vec: &[SendfileVec] = &[
SendfileVec::new(
&header_data,
0,
header_strings.iter().map(|s| s.len()).sum(),
),
SendfileVec::new(&body_data, body_offset as off_t, body.len() - 1),
SendfileVec::new(
&body_data,
body_offset as off_t,
body.len() - body_offset,
),
SendfileVec::new(
&trailer_data,
0,
Expand All @@ -252,7 +256,7 @@ fn test_sendfilev() {
+ &trailer_strings.concat();

// Verify the message that was sent
//assert_eq!(bytes_written as usize, expected_string.as_bytes().len());
assert_eq!(bytes_written, expected_string.as_bytes().len());

let mut read_string = String::new();
let bytes_read = rd.read_to_string(&mut read_string).unwrap();
Expand Down

0 comments on commit 7f85f0d

Please sign in to comment.