]> git.proxmox.com Git - rustc.git/blob - library/std/src/io/copy.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / library / std / src / io / copy.rs
1 use super::{BorrowedBuf, BufWriter, ErrorKind, Read, Result, Write, DEFAULT_BUF_SIZE};
2 use crate::mem::MaybeUninit;
3
4 /// Copies the entire contents of a reader into a writer.
5 ///
6 /// This function will continuously read data from `reader` and then
7 /// write it into `writer` in a streaming fashion until `reader`
8 /// returns EOF.
9 ///
10 /// On success, the total number of bytes that were copied from
11 /// `reader` to `writer` is returned.
12 ///
13 /// If you’re wanting to copy the contents of one file to another and you’re
14 /// working with filesystem paths, see the [`fs::copy`] function.
15 ///
16 /// [`fs::copy`]: crate::fs::copy
17 ///
18 /// # Errors
19 ///
20 /// This function will return an error immediately if any call to [`read`] or
21 /// [`write`] returns an error. All instances of [`ErrorKind::Interrupted`] are
22 /// handled by this function and the underlying operation is retried.
23 ///
24 /// [`read`]: Read::read
25 /// [`write`]: Write::write
26 ///
27 /// # Examples
28 ///
29 /// ```
30 /// use std::io;
31 ///
32 /// fn main() -> io::Result<()> {
33 /// let mut reader: &[u8] = b"hello";
34 /// let mut writer: Vec<u8> = vec![];
35 ///
36 /// io::copy(&mut reader, &mut writer)?;
37 ///
38 /// assert_eq!(&b"hello"[..], &writer[..]);
39 /// Ok(())
40 /// }
41 /// ```
42 ///
43 /// # Platform-specific behavior
44 ///
45 /// On Linux (including Android), this function uses `copy_file_range(2)`,
46 /// `sendfile(2)` or `splice(2)` syscalls to move data directly between file
47 /// descriptors if possible.
48 ///
49 /// Note that platform-specific behavior [may change in the future][changes].
50 ///
51 /// [changes]: crate::io#platform-specific-behavior
52 #[stable(feature = "rust1", since = "1.0.0")]
53 pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
54 where
55 R: Read,
56 W: Write,
57 {
58 cfg_if::cfg_if! {
59 if #[cfg(any(target_os = "linux", target_os = "android"))] {
60 crate::sys::kernel_copy::copy_spec(reader, writer)
61 } else {
62 generic_copy(reader, writer)
63 }
64 }
65 }
66
67 /// The userspace read-write-loop implementation of `io::copy` that is used when
68 /// OS-specific specializations for copy offloading are not available or not applicable.
69 pub(crate) fn generic_copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
70 where
71 R: Read,
72 W: Write,
73 {
74 BufferedCopySpec::copy_to(reader, writer)
75 }
76
77 /// Specialization of the read-write loop that either uses a stack buffer
78 /// or reuses the internal buffer of a BufWriter
79 trait BufferedCopySpec: Write {
80 fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64>;
81 }
82
83 impl<W: Write + ?Sized> BufferedCopySpec for W {
84 default fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
85 stack_buffer_copy(reader, writer)
86 }
87 }
88
89 impl<I: Write> BufferedCopySpec for BufWriter<I> {
90 fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
91 if writer.capacity() < DEFAULT_BUF_SIZE {
92 return stack_buffer_copy(reader, writer);
93 }
94
95 let mut len = 0;
96 let mut init = 0;
97
98 loop {
99 let buf = writer.buffer_mut();
100 let mut read_buf: BorrowedBuf<'_> = buf.spare_capacity_mut().into();
101
102 unsafe {
103 // SAFETY: init is either 0 or the init_len from the previous iteration.
104 read_buf.set_init(init);
105 }
106
107 if read_buf.capacity() >= DEFAULT_BUF_SIZE {
108 let mut cursor = read_buf.unfilled();
109 match reader.read_buf(cursor.reborrow()) {
110 Ok(()) => {
111 let bytes_read = cursor.written();
112
113 if bytes_read == 0 {
114 return Ok(len);
115 }
116
117 init = read_buf.init_len() - bytes_read;
118 len += bytes_read as u64;
119
120 // SAFETY: BorrowedBuf guarantees all of its filled bytes are init
121 unsafe { buf.set_len(buf.len() + bytes_read) };
122
123 // Read again if the buffer still has enough capacity, as BufWriter itself would do
124 // This will occur if the reader returns short reads
125 }
126 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
127 Err(e) => return Err(e),
128 }
129 } else {
130 writer.flush_buf()?;
131 init = 0;
132 }
133 }
134 }
135 }
136
137 fn stack_buffer_copy<R: Read + ?Sized, W: Write + ?Sized>(
138 reader: &mut R,
139 writer: &mut W,
140 ) -> Result<u64> {
141 let buf: &mut [_] = &mut [MaybeUninit::uninit(); DEFAULT_BUF_SIZE];
142 let mut buf: BorrowedBuf<'_> = buf.into();
143
144 let mut len = 0;
145
146 loop {
147 match reader.read_buf(buf.unfilled()) {
148 Ok(()) => {}
149 Err(e) if e.kind() == ErrorKind::Interrupted => continue,
150 Err(e) => return Err(e),
151 };
152
153 if buf.filled().is_empty() {
154 break;
155 }
156
157 len += buf.filled().len() as u64;
158 writer.write_all(buf.filled())?;
159 buf.clear();
160 }
161
162 Ok(len)
163 }