1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // FIXME: This is a complete copy of `cargo/src/cargo/util/read2.rs`
12 // Consider unify the read2() in libstd, cargo and this to prevent further code duplication.
14 pub use self::imp::read2;
16 #[cfg(not(any(unix, windows)))]
18 use std::io::{self, Read};
19 use std::process::{ChildStderr, ChildStdout};
22 out_pipe: ChildStdout,
23 err_pipe: ChildStderr,
24 data: &mut FnMut(bool, &mut Vec<u8>, bool),
26 let mut buffer = Vec::new();
27 out_pipe.read_to_end(&mut buffer)?;
28 data(true, &mut buffer, true);
30 err_pipe.read_to_end(&mut buffer)?;
31 data(false, &mut buffer, true);
40 use std::io::prelude::*;
42 use std::os::unix::prelude::*;
43 use std::process::{ChildStderr, ChildStdout};
46 mut out_pipe: ChildStdout,
47 mut err_pipe: ChildStderr,
48 data: &mut FnMut(bool, &mut Vec<u8>, bool),
51 libc::fcntl(out_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
52 libc::fcntl(err_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
55 let mut out_done = false;
56 let mut err_done = false;
57 let mut out = Vec::new();
58 let mut err = Vec::new();
60 let mut fds: [libc::pollfd; 2] = unsafe { mem::zeroed() };
61 fds[0].fd = out_pipe.as_raw_fd();
62 fds[0].events = libc::POLLIN;
63 fds[1].fd = err_pipe.as_raw_fd();
64 fds[1].events = libc::POLLIN;
69 // wait for either pipe to become readable using `select`
70 let r = unsafe { libc::poll(fds.as_mut_ptr(), nfds, -1) };
72 let err = io::Error::last_os_error();
73 if err.kind() == io::ErrorKind::Interrupted {
79 // Read as much as we can from each pipe, ignoring EWOULDBLOCK or
80 // EAGAIN. If we hit EOF, then this will happen because the underlying
81 // reader will return Ok(0), in which case we'll see `Ok` ourselves. In
82 // this case we flip the other fd back into blocking mode and read
83 // whatever's leftover on that file descriptor.
84 let handle = |res: io::Result<_>| match res {
87 if e.kind() == io::ErrorKind::WouldBlock {
94 if !err_done && fds[errfd].revents != 0 && handle(err_pipe.read_to_end(&mut err))? {
98 data(false, &mut err, err_done);
99 if !out_done && fds[0].revents != 0 && handle(out_pipe.read_to_end(&mut out))? {
101 fds[0].fd = err_pipe.as_raw_fd();
105 data(true, &mut out, out_done);
117 use std::os::windows::prelude::*;
118 use std::process::{ChildStderr, ChildStdout};
121 use self::miow::iocp::{CompletionPort, CompletionStatus};
122 use self::miow::pipe::NamedPipe;
123 use self::miow::Overlapped;
124 use self::winapi::shared::winerror::ERROR_BROKEN_PIPE;
127 dst: &'a mut Vec<u8>,
128 overlapped: Overlapped,
134 out_pipe: ChildStdout,
135 err_pipe: ChildStderr,
136 data: &mut FnMut(bool, &mut Vec<u8>, bool),
137 ) -> io::Result<()> {
138 let mut out = Vec::new();
139 let mut err = Vec::new();
141 let port = CompletionPort::new(1)?;
142 port.add_handle(0, &out_pipe)?;
143 port.add_handle(1, &err_pipe)?;
146 let mut out_pipe = Pipe::new(out_pipe, &mut out);
147 let mut err_pipe = Pipe::new(err_pipe, &mut err);
152 let mut status = [CompletionStatus::zero(), CompletionStatus::zero()];
154 while !out_pipe.done || !err_pipe.done {
155 for status in port.get_many(&mut status, None)? {
156 if status.token() == 0 {
157 out_pipe.complete(status);
158 data(true, out_pipe.dst, out_pipe.done);
161 err_pipe.complete(status);
162 data(false, err_pipe.dst, err_pipe.done);
173 unsafe fn new<P: IntoRawHandle>(p: P, dst: &'a mut Vec<u8>) -> Pipe<'a> {
176 pipe: NamedPipe::from_raw_handle(p.into_raw_handle()),
177 overlapped: Overlapped::zero(),
182 unsafe fn read(&mut self) -> io::Result<()> {
183 let dst = slice_to_end(self.dst);
184 match self.pipe.read_overlapped(dst, self.overlapped.raw()) {
187 if e.raw_os_error() == Some(ERROR_BROKEN_PIPE as i32) {
197 unsafe fn complete(&mut self, status: &CompletionStatus) {
198 let prev = self.dst.len();
199 self.dst.set_len(prev + status.bytes_transferred() as usize);
200 if status.bytes_transferred() == 0 {
206 unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] {
207 if v.capacity() == 0 {
210 if v.capacity() == v.len() {
213 slice::from_raw_parts_mut(
214 v.as_mut_ptr().offset(v.len() as isize),
215 v.capacity() - v.len(),