2022-06-07 07:43:54 +00:00
|
|
|
|
use super::{BorrowedBuf, BufWriter, ErrorKind, Read, Result, Write, DEFAULT_BUF_SIZE};
|
2020-07-28 22:35:01 +00:00
|
|
|
|
use crate::mem::MaybeUninit;
|
|
|
|
|
|
|
|
|
|
/// Copies the entire contents of a reader into a writer.
|
|
|
|
|
///
|
|
|
|
|
/// This function will continuously read data from `reader` and then
|
|
|
|
|
/// write it into `writer` in a streaming fashion until `reader`
|
|
|
|
|
/// returns EOF.
|
|
|
|
|
///
|
|
|
|
|
/// On success, the total number of bytes that were copied from
|
|
|
|
|
/// `reader` to `writer` is returned.
|
|
|
|
|
///
|
|
|
|
|
/// If you’re wanting to copy the contents of one file to another and you’re
|
|
|
|
|
/// working with filesystem paths, see the [`fs::copy`] function.
|
|
|
|
|
///
|
|
|
|
|
/// [`fs::copy`]: crate::fs::copy
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
|
|
|
|
/// This function will return an error immediately if any call to [`read`] or
|
|
|
|
|
/// [`write`] returns an error. All instances of [`ErrorKind::Interrupted`] are
|
|
|
|
|
/// handled by this function and the underlying operation is retried.
|
|
|
|
|
///
|
|
|
|
|
/// [`read`]: Read::read
|
|
|
|
|
/// [`write`]: Write::write
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::io;
|
|
|
|
|
///
|
|
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
|
/// let mut reader: &[u8] = b"hello";
|
|
|
|
|
/// let mut writer: Vec<u8> = vec![];
|
|
|
|
|
///
|
|
|
|
|
/// io::copy(&mut reader, &mut writer)?;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(&b"hello"[..], &writer[..]);
|
|
|
|
|
/// Ok(())
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2022-03-25 04:44:39 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Platform-specific behavior
|
|
|
|
|
///
|
|
|
|
|
/// On Linux (including Android), this function uses `copy_file_range(2)`,
|
|
|
|
|
/// `sendfile(2)` or `splice(2)` syscalls to move data directly between file
|
|
|
|
|
/// descriptors if possible.
|
2022-03-30 02:43:40 +00:00
|
|
|
|
///
|
|
|
|
|
/// Note that platform-specific behavior [may change in the future][changes].
|
|
|
|
|
///
|
|
|
|
|
/// [changes]: crate::io#platform-specific-behavior
|
2020-07-28 22:35:01 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-11-01 18:59:24 +00:00
|
|
|
|
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
|
2020-07-28 22:35:01 +00:00
|
|
|
|
where
|
|
|
|
|
R: Read,
|
|
|
|
|
W: Write,
|
|
|
|
|
{
|
2020-10-06 23:01:12 +00:00
|
|
|
|
cfg_if::cfg_if! {
|
|
|
|
|
if #[cfg(any(target_os = "linux", target_os = "android"))] {
|
|
|
|
|
crate::sys::kernel_copy::copy_spec(reader, writer)
|
|
|
|
|
} else {
|
|
|
|
|
generic_copy(reader, writer)
|
|
|
|
|
}
|
2020-07-28 22:35:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 18:59:24 +00:00
|
|
|
|
/// The userspace read-write-loop implementation of `io::copy` that is used when
|
|
|
|
|
/// OS-specific specializations for copy offloading are not available or not applicable.
|
|
|
|
|
pub(crate) fn generic_copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
|
2020-07-28 22:35:01 +00:00
|
|
|
|
where
|
|
|
|
|
R: Read,
|
|
|
|
|
W: Write,
|
|
|
|
|
{
|
2020-11-01 18:59:24 +00:00
|
|
|
|
BufferedCopySpec::copy_to(reader, writer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Specialization of the read-write loop that either uses a stack buffer
|
|
|
|
|
/// or reuses the internal buffer of a BufWriter
|
|
|
|
|
trait BufferedCopySpec: Write {
|
|
|
|
|
fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<W: Write + ?Sized> BufferedCopySpec for W {
|
|
|
|
|
default fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
|
|
|
|
|
stack_buffer_copy(reader, writer)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<I: Write> BufferedCopySpec for BufWriter<I> {
|
|
|
|
|
fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
|
|
|
|
|
if writer.capacity() < DEFAULT_BUF_SIZE {
|
|
|
|
|
return stack_buffer_copy(reader, writer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut len = 0;
|
2021-06-29 21:50:50 +00:00
|
|
|
|
let mut init = 0;
|
2020-11-01 18:59:24 +00:00
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
let buf = writer.buffer_mut();
|
2022-06-07 07:43:54 +00:00
|
|
|
|
let mut read_buf: BorrowedBuf<'_> = buf.spare_capacity_mut().into();
|
2021-01-18 06:28:18 +00:00
|
|
|
|
|
2021-06-29 21:50:50 +00:00
|
|
|
|
unsafe {
|
2022-05-13 14:06:36 +00:00
|
|
|
|
// SAFETY: init is either 0 or the init_len from the previous iteration.
|
|
|
|
|
read_buf.set_init(init);
|
2021-06-29 21:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 06:28:18 +00:00
|
|
|
|
if read_buf.capacity() >= DEFAULT_BUF_SIZE {
|
2022-05-13 14:06:36 +00:00
|
|
|
|
let mut cursor = read_buf.unfilled();
|
2022-08-11 14:52:29 +00:00
|
|
|
|
match reader.read_buf(cursor.reborrow()) {
|
2021-01-18 06:28:18 +00:00
|
|
|
|
Ok(()) => {
|
2022-05-13 14:06:36 +00:00
|
|
|
|
let bytes_read = cursor.written();
|
2021-01-18 06:28:18 +00:00
|
|
|
|
|
|
|
|
|
if bytes_read == 0 {
|
|
|
|
|
return Ok(len);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-13 14:06:36 +00:00
|
|
|
|
init = read_buf.init_len() - bytes_read;
|
|
|
|
|
len += bytes_read as u64;
|
2021-06-29 21:50:50 +00:00
|
|
|
|
|
2022-06-07 07:43:54 +00:00
|
|
|
|
// SAFETY: BorrowedBuf guarantees all of its filled bytes are init
|
2020-11-01 18:59:24 +00:00
|
|
|
|
unsafe { buf.set_len(buf.len() + bytes_read) };
|
2022-05-13 14:06:36 +00:00
|
|
|
|
|
2020-11-01 18:59:24 +00:00
|
|
|
|
// Read again if the buffer still has enough capacity, as BufWriter itself would do
|
|
|
|
|
// This will occur if the reader returns short reads
|
|
|
|
|
}
|
2022-05-13 14:06:36 +00:00
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
|
2020-11-01 18:59:24 +00:00
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
|
}
|
2022-05-13 14:06:36 +00:00
|
|
|
|
} else {
|
|
|
|
|
writer.flush_buf()?;
|
|
|
|
|
init = 0;
|
2020-11-01 18:59:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stack_buffer_copy<R: Read + ?Sized, W: Write + ?Sized>(
|
|
|
|
|
reader: &mut R,
|
|
|
|
|
writer: &mut W,
|
|
|
|
|
) -> Result<u64> {
|
2022-05-13 14:06:36 +00:00
|
|
|
|
let buf: &mut [_] = &mut [MaybeUninit::uninit(); DEFAULT_BUF_SIZE];
|
2022-06-07 07:43:54 +00:00
|
|
|
|
let mut buf: BorrowedBuf<'_> = buf.into();
|
2021-01-18 06:28:18 +00:00
|
|
|
|
|
|
|
|
|
let mut len = 0;
|
2020-07-28 22:35:01 +00:00
|
|
|
|
|
|
|
|
|
loop {
|
2022-05-13 14:06:36 +00:00
|
|
|
|
match reader.read_buf(buf.unfilled()) {
|
2021-01-18 06:28:18 +00:00
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
|
2020-07-28 22:35:01 +00:00
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
|
};
|
2021-01-18 06:28:18 +00:00
|
|
|
|
|
|
|
|
|
if buf.filled().is_empty() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len += buf.filled().len() as u64;
|
|
|
|
|
writer.write_all(buf.filled())?;
|
|
|
|
|
buf.clear();
|
2020-07-28 22:35:01 +00:00
|
|
|
|
}
|
2021-01-18 06:28:18 +00:00
|
|
|
|
|
|
|
|
|
Ok(len)
|
2020-07-28 22:35:01 +00:00
|
|
|
|
}
|