|
| 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. |
| 4 | +// |
| 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. |
| 10 | + |
| 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. |
| 13 | + |
| 14 | +pub use self::imp::read2; |
| 15 | + |
| 16 | +#[cfg(not(any(unix, windows)))] |
| 17 | +mod imp { |
| 18 | + use std::io::{self, Read}; |
| 19 | + use std::process::{ChildStdout, ChildStderr}; |
| 20 | + |
| 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); |
| 27 | + buffer.clear(); |
| 28 | + err_pipe.read_to_end(&mut buffer)?; |
| 29 | + data(false, &mut buffer, true); |
| 30 | + Ok(()) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[cfg(unix)] |
| 35 | +mod imp { |
| 36 | + use std::io::prelude::*; |
| 37 | + use std::io; |
| 38 | + use std::mem; |
| 39 | + use std::os::unix::prelude::*; |
| 40 | + use std::process::{ChildStdout, ChildStderr}; |
| 41 | + use libc; |
| 42 | + |
| 43 | + pub fn read2(mut out_pipe: ChildStdout, |
| 44 | + mut err_pipe: ChildStderr, |
| 45 | + data: &mut FnMut(bool, &mut Vec<u8>, bool)) -> io::Result<()> { |
| 46 | + unsafe { |
| 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); |
| 49 | + } |
| 50 | + |
| 51 | + let mut out_done = false; |
| 52 | + let mut err_done = false; |
| 53 | + let mut out = Vec::new(); |
| 54 | + let mut err = Vec::new(); |
| 55 | + |
| 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; |
| 61 | + loop { |
| 62 | + // wait for either pipe to become readable using `select` |
| 63 | + let r = unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) }; |
| 64 | + if r == -1 { |
| 65 | + let err = io::Error::last_os_error(); |
| 66 | + if err.kind() == io::ErrorKind::Interrupted { |
| 67 | + continue |
| 68 | + } |
| 69 | + return Err(err) |
| 70 | + } |
| 71 | + |
| 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<_>| { |
| 78 | + match res { |
| 79 | + Ok(_) => Ok(true), |
| 80 | + Err(e) => { |
| 81 | + if e.kind() == io::ErrorKind::WouldBlock { |
| 82 | + Ok(false) |
| 83 | + } else { |
| 84 | + Err(e) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + }; |
| 89 | + if !out_done && fds[0].revents != 0 && handle(out_pipe.read_to_end(&mut out))? { |
| 90 | + out_done = true; |
| 91 | + } |
| 92 | + data(true, &mut out, out_done); |
| 93 | + if !err_done && fds[1].revents != 0 && handle(err_pipe.read_to_end(&mut err))? { |
| 94 | + err_done = true; |
| 95 | + } |
| 96 | + data(false, &mut err, err_done); |
| 97 | + |
| 98 | + if out_done && err_done { |
| 99 | + return Ok(()) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(windows)] |
| 106 | +mod imp { |
| 107 | + extern crate miow; |
| 108 | + extern crate winapi; |
| 109 | + |
| 110 | + use std::io; |
| 111 | + use std::os::windows::prelude::*; |
| 112 | + use std::process::{ChildStdout, ChildStderr}; |
| 113 | + use std::slice; |
| 114 | + |
| 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; |
| 119 | + |
| 120 | + struct Pipe<'a> { |
| 121 | + dst: &'a mut Vec<u8>, |
| 122 | + overlapped: Overlapped, |
| 123 | + pipe: NamedPipe, |
| 124 | + done: bool, |
| 125 | + } |
| 126 | + |
| 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(); |
| 132 | + |
| 133 | + let port = CompletionPort::new(1)?; |
| 134 | + port.add_handle(0, &out_pipe)?; |
| 135 | + port.add_handle(1, &err_pipe)?; |
| 136 | + |
| 137 | + unsafe { |
| 138 | + let mut out_pipe = Pipe::new(out_pipe, &mut out); |
| 139 | + let mut err_pipe = Pipe::new(err_pipe, &mut err); |
| 140 | + |
| 141 | + out_pipe.read()?; |
| 142 | + err_pipe.read()?; |
| 143 | + |
| 144 | + let mut status = [CompletionStatus::zero(), CompletionStatus::zero()]; |
| 145 | + |
| 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); |
| 151 | + out_pipe.read()?; |
| 152 | + } else { |
| 153 | + err_pipe.complete(status); |
| 154 | + data(false, err_pipe.dst, err_pipe.done); |
| 155 | + err_pipe.read()?; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + Ok(()) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + impl<'a> Pipe<'a> { |
| 165 | + unsafe fn new<P: IntoRawHandle>(p: P, dst: &'a mut Vec<u8>) -> Pipe<'a> { |
| 166 | + Pipe { |
| 167 | + dst: dst, |
| 168 | + pipe: NamedPipe::from_raw_handle(p.into_raw_handle()), |
| 169 | + overlapped: Overlapped::zero(), |
| 170 | + done: false, |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 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()) { |
| 177 | + Ok(_) => Ok(()), |
| 178 | + Err(e) => { |
| 179 | + if e.raw_os_error() == Some(ERROR_BROKEN_PIPE as i32) { |
| 180 | + self.done = true; |
| 181 | + Ok(()) |
| 182 | + } else { |
| 183 | + Err(e) |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 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 { |
| 193 | + self.done = true; |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] { |
| 199 | + if v.capacity() == 0 { |
| 200 | + v.reserve(16); |
| 201 | + } |
| 202 | + if v.capacity() == v.len() { |
| 203 | + v.reserve(1); |
| 204 | + } |
| 205 | + slice::from_raw_parts_mut(v.as_mut_ptr().offset(v.len() as isize), |
| 206 | + v.capacity() - v.len()) |
| 207 | + } |
| 208 | +} |
0 commit comments