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::{ChildStdout, ChildStderr};
21 pub fn read2(out_pipe: ChildStdout,
22 err_pipe: ChildStderr,
23 data: &mut FnMut(bool, &mut Vec<u8>, bool)) -> io::Result<()> {
24 let mut buffer = Vec::new();
25 out_pipe.read_to_end(&mut buffer)?;
26 data(true, &mut buffer, true);
28 err_pipe.read_to_end(&mut buffer)?;
29 data(false, &mut buffer, true);
36 use std::io::prelude::*;
39 use std::os::unix::prelude::*;
40 use std::process::{ChildStdout, ChildStderr};
43 pub fn read2(mut out_pipe: ChildStdout,
44 mut err_pipe: ChildStderr,
45 data: &mut FnMut(bool, &mut Vec<u8>, bool)) -> io::Result<()> {
47 libc::fcntl(out_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
48 libc::fcntl(err_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
51 let mut out_done = false;
52 let mut err_done = false;
53 let mut out = Vec::new();
54 let mut err = Vec::new();
56 let mut fds: [libc::pollfd; 2] = unsafe { mem::zeroed() };
57 fds[0].fd = out_pipe.as_raw_fd();
58 fds[0].events = libc::POLLIN;
59 fds[1].fd = err_pipe.as_raw_fd();
60 fds[1].events = libc::POLLIN;
62 // wait for either pipe to become readable using `select`
63 let r = unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) };
65 let err = io::Error::last_os_error();
66 if err.kind() == io::ErrorKind::Interrupted {
72 // Read as much as we can from each pipe, ignoring EWOULDBLOCK or
73 // EAGAIN. If we hit EOF, then this will happen because the underlying
74 // reader will return Ok(0), in which case we'll see `Ok` ourselves. In
75 // this case we flip the other fd back into blocking mode and read
76 // whatever's leftover on that file descriptor.
77 let handle = |res: io::Result<_>| {
81 if e.kind() == io::ErrorKind::WouldBlock {
89 if !out_done && fds[0].revents != 0 && handle(out_pipe.read_to_end(&mut out))? {
92 data(true, &mut out, out_done);
93 if !err_done && fds[1].revents != 0 && handle(err_pipe.read_to_end(&mut err))? {
96 data(false, &mut err, err_done);
98 if out_done && err_done {
111 use std::os::windows::prelude::*;
112 use std::process::{ChildStdout, ChildStderr};
115 use self::miow::iocp::{CompletionPort, CompletionStatus};
116 use self::miow::pipe::NamedPipe;
117 use self::miow::Overlapped;
118 use self::winapi::ERROR_BROKEN_PIPE;
121 dst: &'a mut Vec<u8>,
122 overlapped: Overlapped,
127 pub fn read2(out_pipe: ChildStdout,
128 err_pipe: ChildStderr,
129 data: &mut FnMut(bool, &mut Vec<u8>, bool)) -> io::Result<()> {
130 let mut out = Vec::new();
131 let mut err = Vec::new();
133 let port = CompletionPort::new(1)?;
134 port.add_handle(0, &out_pipe)?;
135 port.add_handle(1, &err_pipe)?;
138 let mut out_pipe = Pipe::new(out_pipe, &mut out);
139 let mut err_pipe = Pipe::new(err_pipe, &mut err);
144 let mut status = [CompletionStatus::zero(), CompletionStatus::zero()];
146 while !out_pipe.done || !err_pipe.done {
147 for status in port.get_many(&mut status, None)? {
148 if status.token() == 0 {
149 out_pipe.complete(status);
150 data(true, out_pipe.dst, out_pipe.done);
153 err_pipe.complete(status);
154 data(false, err_pipe.dst, err_pipe.done);
165 unsafe fn new<P: IntoRawHandle>(p: P, dst: &'a mut Vec<u8>) -> Pipe<'a> {
168 pipe: NamedPipe::from_raw_handle(p.into_raw_handle()),
169 overlapped: Overlapped::zero(),
174 unsafe fn read(&mut self) -> io::Result<()> {
175 let dst = slice_to_end(self.dst);
176 match self.pipe.read_overlapped(dst, self.overlapped.raw()) {
179 if e.raw_os_error() == Some(ERROR_BROKEN_PIPE as i32) {
189 unsafe fn complete(&mut self, status: &CompletionStatus) {
190 let prev = self.dst.len();
191 self.dst.set_len(prev + status.bytes_transferred() as usize);
192 if status.bytes_transferred() == 0 {
198 unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] {
199 if v.capacity() == 0 {
202 if v.capacity() == v.len() {
205 slice::from_raw_parts_mut(v.as_mut_ptr().offset(v.len() as isize),
206 v.capacity() - v.len())