diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/sys/mod.rs | 1 | ||||
-rw-r--r-- | src/sys/sendfile.rs | 46 |
2 files changed, 46 insertions, 1 deletions
diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 654a4d87..0bd0bc9f 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -110,6 +110,7 @@ feature! { } #[cfg(any(target_os = "android", + target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "linux", diff --git a/src/sys/sendfile.rs b/src/sys/sendfile.rs index 1a87a680..5ec0b526 100644 --- a/src/sys/sendfile.rs +++ b/src/sys/sendfile.rs @@ -64,7 +64,8 @@ pub fn sendfile64( } cfg_if! { - if #[cfg(any(target_os = "freebsd", + if #[cfg(any(target_os = "dragonfly", + target_os = "freebsd", target_os = "ios", target_os = "macos"))] { use crate::sys::uio::IoVec; @@ -184,6 +185,49 @@ cfg_if! { }; (Errno::result(return_code).and(Ok(())), bytes_sent) } + } else if #[cfg(target_os = "dragonfly")] { + /// Read up to `count` bytes from `in_fd` starting at `offset` and write to `out_sock`. + /// + /// Returns a `Result` and a count of bytes written. Bytes written may be non-zero even if + /// an error occurs. + /// + /// `in_fd` must describe a regular file. `out_sock` must describe a stream socket. + /// + /// If `offset` falls past the end of the file, the function returns success and zero bytes + /// written. + /// + /// If `count` is `None` or 0, bytes will be read from `in_fd` until reaching the end of + /// file (EOF). + /// + /// `headers` and `trailers` specify optional slices of byte slices to be sent before and + /// after the data read from `in_fd`, respectively. The length of headers and trailers sent + /// is included in the returned count of bytes written. The values of `offset` and `count` + /// do not apply to headers or trailers. + /// + /// For more information, see + /// [the sendfile(2) man page.](https://leaf.dragonflybsd.org/cgi/web-man?command=sendfile§ion=2) + pub fn sendfile( + in_fd: RawFd, + out_sock: RawFd, + offset: off_t, + count: Option<usize>, + headers: Option<&[&[u8]]>, + trailers: Option<&[&[u8]]>, + ) -> (Result<()>, off_t) { + let mut bytes_sent: off_t = 0; + let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers)); + let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr); + let return_code = unsafe { + libc::sendfile(in_fd, + out_sock, + offset, + count.unwrap_or(0), + hdtr_ptr as *mut libc::sf_hdtr, + &mut bytes_sent as *mut off_t, + 0) + }; + (Errno::result(return_code).and(Ok(())), bytes_sent) + } } else if #[cfg(any(target_os = "ios", target_os = "macos"))] { /// Read bytes from `in_fd` starting at `offset` and write up to `count` bytes to /// `out_sock`. |