zio_sendfile/
lib.rs

1extern crate libc;
2use std::io;
3use std::os::unix::io::{AsRawFd, RawFd};
4
5/// Convenience function for quickly copying a file from the source to the dest.
6pub fn copy<S: AsRawFd, D: AsRawFd>(count: usize, source: &mut S, dest: &mut D) -> io::Result<()> {
7    let mut sendfile = SendFile::new(source, count)?;
8
9    loop {
10        match sendfile.send(dest)? {
11            0 => return Ok(()),
12            _ => (),
13        }
14    }
15}
16
17/// Convenience function for quickly copying a file from the source to the dest.
18pub fn copy_callback<CB: FnMut(&mut SendFile, u64), S: AsRawFd, D: AsRawFd>(
19    count: usize,
20    source: &mut S,
21    dest: &mut D,
22    mut cb: CB,
23) -> io::Result<()> {
24    let mut sendfile = SendFile::new(source, count)?;
25
26    loop {
27        match sendfile.send(dest)? {
28            0 => return Ok(()),
29            wrote => cb(&mut sendfile, wrote),
30        }
31    }
32}
33
34/// Abstraction for interacting with Linux's zero-copy I/O sendfile syscall.
35///
36/// ```rust,no_run
37/// # extern crate zio_sendfile;
38/// # use std::os::unix::io::{AsRawFd, RawFd};
39/// # use std::io;
40/// # use zio_sendfile::SendFile;
41/// #
42/// # pub fn copy<S: AsRawFd, D: AsRawFd>(count: usize, source: &mut S, dest: &mut D) -> io::Result<()> {
43/// let mut sendfile = SendFile::new(source, count)?;
44///
45/// loop {
46///     match sendfile.send(dest)? {
47///         0 => return Ok(()),
48///         _ => ()
49///     }
50/// }
51/// # }
52/// ```
53pub struct SendFile {
54    pub file: RawFd,
55    pub count: usize,
56    pub offset: libc::off_t,
57}
58
59impl SendFile {
60    /// Set a file to be used as the sendfile source.
61    pub fn new<F: AsRawFd>(file: &mut F, count: usize) -> io::Result<Self> {
62        Ok(Self {
63            file: file.as_raw_fd(),
64            count,
65            offset: 0,
66        })
67    }
68
69    /// Set the number of bytes to write per call.
70    pub fn count(mut self, count: usize) -> Self {
71        self.count = if count < 1024 { 1024 } else { count };
72        self
73    }
74
75    /// Set the offset to begin copying bytes from.
76    pub fn offset(mut self, offset: libc::off_t) -> Self {
77        self.offset = offset;
78        self
79    }
80
81    /// Begin copying from the the sendfile to the destination file.
82    ///
83    /// - This will write up to `self.count` amount of bytes at a time.
84    /// - The `self.offset` value is updated by the syscall on return.
85    pub fn send<F: AsRawFd>(&mut self, to: &mut F) -> io::Result<u64> {
86        let result = unsafe {
87            libc::sendfile(
88                to.as_raw_fd(),
89                self.file,
90                &mut self.offset as *mut libc::off_t,
91                self.count,
92            )
93        };
94
95        if result == -1 {
96            Err(io::Error::last_os_error())
97        } else {
98            Ok(result as u64)
99        }
100    }
101}