Skip to content

Commit

Permalink
Merge #1439
Browse files Browse the repository at this point in the history
1439: Add sendfile64 r=asomers a=tdryer

`sendfile64` is a Linux-specific call with a wider type for the `offset` argument than `sendfile`.

This is largely a copy of the existing `sendfile` function and associated test.

Co-authored-by: Tom Dryer <tomdryer.com@gmail.com>
  • Loading branch information
bors[bot] and tdryer committed May 30, 2021
2 parents 33df3e7 + fb5d942 commit df9ed93
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Added
- Added TIMESTAMPNS support for linux
(#[1402](https://github.com/nix-rust/nix/pull/1402))
- Added `sendfile64` (#[1439](https://github.com/nix-rust/nix/pull/1439))

### Changed
- Made `forkpty` unsafe, like `fork`
Expand Down
26 changes: 26 additions & 0 deletions src/sys/sendfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ pub fn sendfile(
Errno::result(ret).map(|r| r as usize)
}

/// Copy up to `count` bytes to `out_fd` from `in_fd` starting at `offset`.
///
/// Returns a `Result` with the number of bytes written.
///
/// If `offset` is `None`, `sendfile` will begin reading at the current offset of `in_fd`and will
/// update the offset of `in_fd`. If `offset` is `Some`, `sendfile` will begin at the specified
/// offset and will not update the offset of `in_fd`. Instead, it will mutate `offset` to point to
/// the byte after the last byte copied.
///
/// `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)
#[cfg(target_os = "linux")]
pub fn sendfile64(
out_fd: RawFd,
in_fd: RawFd,
offset: Option<&mut libc::off64_t>,
count: usize,
) -> Result<usize> {
let offset = offset
.map(|offset| offset as *mut _)
.unwrap_or(ptr::null_mut());
let ret = unsafe { libc::sendfile64(out_fd, in_fd, offset, count) };
Errno::result(ret).map(|r| r as usize)
}

cfg_if! {
if #[cfg(any(target_os = "freebsd",
target_os = "ios",
Expand Down
22 changes: 22 additions & 0 deletions test/test_sendfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ fn test_sendfile_linux() {
close(wr).unwrap();
}

#[cfg(target_os = "linux")]
#[test]
fn test_sendfile64_linux() {
const CONTENTS: &[u8] = b"abcdef123456";
let mut tmp = tempfile().unwrap();
tmp.write_all(CONTENTS).unwrap();

let (rd, wr) = pipe().unwrap();
let mut offset: libc::off64_t = 5;
let res = sendfile64(wr, tmp.as_raw_fd(), Some(&mut offset), 2).unwrap();

assert_eq!(2, res);

let mut buf = [0u8; 1024];
assert_eq!(2, read(rd, &mut buf).unwrap());
assert_eq!(b"f1", &buf[0..2]);
assert_eq!(7, offset);

close(rd).unwrap();
close(wr).unwrap();
}

#[cfg(target_os = "freebsd")]
#[test]
fn test_sendfile_freebsd() {
Expand Down

0 comments on commit df9ed93

Please sign in to comment.