1 // FIXME: This is a complete copy of `cargo/src/cargo/util/read2.rs`
2 // Consider unify the read2() in libstd, cargo and this to prevent further code duplication.
7 pub use self::imp::read2;
8 use std::io::{self, Write};
10 use std::process::{Child, Output};
12 pub fn read2_abbreviated(mut child: Child, filter_paths_from_len: &[String]) -> io::Result<Output> {
13 let mut stdout = ProcOutput::new();
14 let mut stderr = ProcOutput::new();
16 drop(child.stdin.take());
18 child.stdout.take().unwrap(),
19 child.stderr.take().unwrap(),
20 &mut |is_stdout, data, _| {
21 if is_stdout { &mut stdout } else { &mut stderr }.extend(data, filter_paths_from_len);
25 let status = child.wait()?;
27 Ok(Output { status, stdout: stdout.into_bytes(), stderr: stderr.into_bytes() })
30 const HEAD_LEN: usize = 160 * 1024;
31 const TAIL_LEN: usize = 256 * 1024;
33 // Whenever a path is filtered when counting the length of the output, we need to add some
34 // placeholder length to ensure a compiler emitting only filtered paths doesn't cause a OOM.
36 // 32 was chosen semi-arbitrarily: it was the highest power of two that still allowed the test
37 // suite to pass at the moment of implementing path filtering.
38 const FILTERED_PATHS_PLACEHOLDER_LEN: usize = 32;
41 Full { bytes: Vec<u8>, filtered_len: usize },
42 Abbreviated { head: Vec<u8>, skipped: usize, tail: Box<[u8]> },
47 ProcOutput::Full { bytes: Vec::new(), filtered_len: 0 }
50 fn extend(&mut self, data: &[u8], filter_paths_from_len: &[String]) {
51 let new_self = match *self {
52 ProcOutput::Full { ref mut bytes, ref mut filtered_len } => {
53 let old_len = bytes.len();
54 bytes.extend_from_slice(data);
55 *filtered_len += data.len();
57 // We had problems in the past with tests failing only in some environments,
58 // due to the length of the base path pushing the output size over the limit.
60 // To make those failures deterministic across all environments we ignore known
61 // paths when calculating the string length, while still including the full
62 // path in the output. This could result in some output being larger than the
63 // threshold, but it's better than having nondeterministic failures.
65 // The compiler emitting only excluded strings is addressed by adding a
66 // placeholder size for each excluded segment, which will eventually reach
67 // the configured threshold.
68 for path in filter_paths_from_len {
69 let path_bytes = path.as_bytes();
70 // We start matching `path_bytes - 1` into the previously loaded data,
71 // to account for the fact a path_bytes might be included across multiple
72 // `extend` calls. Starting from `- 1` avoids double-counting paths.
73 let matches = (&bytes[(old_len.saturating_sub(path_bytes.len() - 1))..])
74 .windows(path_bytes.len())
75 .filter(|window| window == &path_bytes)
77 *filtered_len -= matches * path_bytes.len();
79 // We can't just remove the length of the filtered path from the output lenght,
80 // otherwise a compiler emitting only filtered paths would OOM compiletest. Add
81 // a fixed placeholder length for each path to prevent that.
82 *filtered_len += matches * FILTERED_PATHS_PLACEHOLDER_LEN;
85 let new_len = bytes.len();
86 if *filtered_len <= HEAD_LEN + TAIL_LEN {
90 let mut head = replace(bytes, Vec::new());
91 let mut middle = head.split_off(HEAD_LEN);
92 let tail = middle.split_off(middle.len() - TAIL_LEN).into_boxed_slice();
93 let skipped = new_len - HEAD_LEN - TAIL_LEN;
94 ProcOutput::Abbreviated { head, skipped, tail }
96 ProcOutput::Abbreviated { ref mut skipped, ref mut tail, .. } => {
97 *skipped += data.len();
98 if data.len() <= TAIL_LEN {
99 tail[..data.len()].copy_from_slice(data);
100 tail.rotate_left(data.len());
102 tail.copy_from_slice(&data[(data.len() - TAIL_LEN)..]);
110 fn into_bytes(self) -> Vec<u8> {
112 ProcOutput::Full { bytes, .. } => bytes,
113 ProcOutput::Abbreviated { mut head, skipped, tail } => {
114 write!(&mut head, "\n\n<<<<<< SKIPPED {} BYTES >>>>>>\n\n", skipped).unwrap();
115 head.extend_from_slice(&tail);
122 #[cfg(not(any(unix, windows)))]
124 use std::io::{self, Read};
125 use std::process::{ChildStderr, ChildStdout};
128 out_pipe: ChildStdout,
129 err_pipe: ChildStderr,
130 data: &mut dyn FnMut(bool, &mut Vec<u8>, bool),
131 ) -> io::Result<()> {
132 let mut buffer = Vec::new();
133 out_pipe.read_to_end(&mut buffer)?;
134 data(true, &mut buffer, true);
136 err_pipe.read_to_end(&mut buffer)?;
137 data(false, &mut buffer, true);
145 use std::io::prelude::*;
147 use std::os::unix::prelude::*;
148 use std::process::{ChildStderr, ChildStdout};
151 mut out_pipe: ChildStdout,
152 mut err_pipe: ChildStderr,
153 data: &mut dyn FnMut(bool, &mut Vec<u8>, bool),
154 ) -> io::Result<()> {
156 libc::fcntl(out_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
157 libc::fcntl(err_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
160 let mut out_done = false;
161 let mut err_done = false;
162 let mut out = Vec::new();
163 let mut err = Vec::new();
165 let mut fds: [libc::pollfd; 2] = unsafe { mem::zeroed() };
166 fds[0].fd = out_pipe.as_raw_fd();
167 fds[0].events = libc::POLLIN;
168 fds[1].fd = err_pipe.as_raw_fd();
169 fds[1].events = libc::POLLIN;
174 // wait for either pipe to become readable using `select`
175 let r = unsafe { libc::poll(fds.as_mut_ptr(), nfds, -1) };
177 let err = io::Error::last_os_error();
178 if err.kind() == io::ErrorKind::Interrupted {
184 // Read as much as we can from each pipe, ignoring EWOULDBLOCK or
185 // EAGAIN. If we hit EOF, then this will happen because the underlying
186 // reader will return Ok(0), in which case we'll see `Ok` ourselves. In
187 // this case we flip the other fd back into blocking mode and read
188 // whatever's leftover on that file descriptor.
189 let handle = |res: io::Result<_>| match res {
192 if e.kind() == io::ErrorKind::WouldBlock {
199 if !err_done && fds[errfd].revents != 0 && handle(err_pipe.read_to_end(&mut err))? {
203 data(false, &mut err, err_done);
204 if !out_done && fds[0].revents != 0 && handle(out_pipe.read_to_end(&mut out))? {
206 fds[0].fd = err_pipe.as_raw_fd();
210 data(true, &mut out, out_done);
219 use std::os::windows::prelude::*;
220 use std::process::{ChildStderr, ChildStdout};
223 use miow::iocp::{CompletionPort, CompletionStatus};
224 use miow::pipe::NamedPipe;
225 use miow::Overlapped;
226 use winapi::shared::winerror::ERROR_BROKEN_PIPE;
229 dst: &'a mut Vec<u8>,
230 overlapped: Overlapped,
236 out_pipe: ChildStdout,
237 err_pipe: ChildStderr,
238 data: &mut dyn FnMut(bool, &mut Vec<u8>, bool),
239 ) -> io::Result<()> {
240 let mut out = Vec::new();
241 let mut err = Vec::new();
243 let port = CompletionPort::new(1)?;
244 port.add_handle(0, &out_pipe)?;
245 port.add_handle(1, &err_pipe)?;
248 let mut out_pipe = Pipe::new(out_pipe, &mut out);
249 let mut err_pipe = Pipe::new(err_pipe, &mut err);
254 let mut status = [CompletionStatus::zero(), CompletionStatus::zero()];
256 while !out_pipe.done || !err_pipe.done {
257 for status in port.get_many(&mut status, None)? {
258 if status.token() == 0 {
259 out_pipe.complete(status);
260 data(true, out_pipe.dst, out_pipe.done);
263 err_pipe.complete(status);
264 data(false, err_pipe.dst, err_pipe.done);
275 unsafe fn new<P: IntoRawHandle>(p: P, dst: &'a mut Vec<u8>) -> Pipe<'a> {
278 pipe: NamedPipe::from_raw_handle(p.into_raw_handle()),
279 overlapped: Overlapped::zero(),
284 unsafe fn read(&mut self) -> io::Result<()> {
285 let dst = slice_to_end(self.dst);
286 match self.pipe.read_overlapped(dst, self.overlapped.raw()) {
289 if e.raw_os_error() == Some(ERROR_BROKEN_PIPE as i32) {
299 unsafe fn complete(&mut self, status: &CompletionStatus) {
300 let prev = self.dst.len();
301 self.dst.set_len(prev + status.bytes_transferred() as usize);
302 if status.bytes_transferred() == 0 {
308 unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] {
309 if v.capacity() == 0 {
312 if v.capacity() == v.len() {
315 slice::from_raw_parts_mut(v.as_mut_ptr().offset(v.len() as isize), v.capacity() - v.len())