|
7 | 7 |
|
8 | 8 | use std::ffi::OsString; |
9 | 9 | use std::fs::OpenOptions; |
10 | | -use std::io::{Error, ErrorKind, Read, Result, Write, stderr, stdin, stdout}; |
| 10 | +use std::io::{Error, ErrorKind, Read, Result, Write, stderr, stdin}; |
11 | 11 | use std::path::PathBuf; |
12 | 12 | use uucore::display::Quotable; |
13 | 13 | use uucore::error::{UResult, strip_errno}; |
@@ -80,7 +80,10 @@ fn tee(options: &Options) -> Result<()> { |
80 | 80 | 0, |
81 | 81 | NamedWriter { |
82 | 82 | name: translate!("tee-standard-output").into(), |
83 | | - inner: Writer::Stdout(stdout()), |
| 83 | + #[cfg(any(unix, target_os = "wasi"))] |
| 84 | + inner: Writer::Stdout(uucore::io::RawWriter(rustix::stdio::stdout())), |
| 85 | + #[cfg(not(any(unix, target_os = "wasi")))] |
| 86 | + inner: Writer::Stdout(std::io::stdout()), |
84 | 87 | }, |
85 | 88 | ); |
86 | 89 |
|
@@ -197,21 +200,16 @@ impl MultiWriter { |
197 | 200 |
|
198 | 201 | fn write_flush(&mut self, buf: &[u8]) -> Result<()> { |
199 | 202 | let mode = self.output_error_mode; |
200 | | - self.writers.retain_mut(|writer| { |
201 | | - let res = (|| { |
202 | | - writer.inner.write_all(buf)?; |
203 | | - writer.inner.flush() |
204 | | - })(); |
205 | | - match res { |
| 203 | + self.writers |
| 204 | + .retain_mut(|writer| match writer.inner.write_all(buf) { |
206 | 205 | Ok(()) => true, |
207 | 206 | Err(e) => { |
208 | 207 | if let Err(e) = process_error(mode, e, writer, &mut self.ignored_errors) { |
209 | 208 | self.aborted.get_or_insert(e); |
210 | 209 | } |
211 | 210 | false |
212 | 211 | } |
213 | | - } |
214 | | - }); |
| 212 | + }); |
215 | 213 | self.aborted.take().map_or( |
216 | 214 | if self.writers.is_empty() { |
217 | 215 | // This error kind will never be raised by the standard |
@@ -251,21 +249,26 @@ fn process_error( |
251 | 249 |
|
252 | 250 | enum Writer { |
253 | 251 | File(std::fs::File), |
| 252 | + // remove buffering for posix requirement and improve throughput |
| 253 | + #[cfg(any(unix, target_os = "wasi"))] |
| 254 | + Stdout(uucore::io::RawWriter<rustix::fd::BorrowedFd<'static>>), |
| 255 | + #[cfg(not(any(unix, target_os = "wasi")))] |
254 | 256 | Stdout(std::io::Stdout), |
255 | 257 | } |
256 | 258 |
|
257 | | -impl Write for Writer { |
258 | | - fn write(&mut self, buf: &[u8]) -> Result<usize> { |
259 | | - match self { |
260 | | - Self::File(f) => f.write(buf), |
261 | | - Self::Stdout(s) => s.write(buf), |
262 | | - } |
263 | | - } |
264 | | - |
265 | | - fn flush(&mut self) -> Result<()> { |
| 259 | +impl Writer { |
| 260 | + pub fn write_all(&mut self, buf: &[u8]) -> Result<()> { |
266 | 261 | match self { |
267 | | - Self::File(f) => f.flush(), |
268 | | - Self::Stdout(s) => s.flush(), |
| 262 | + // File does not have line buffering |
| 263 | + Self::File(f) => f.write_all(buf), |
| 264 | + #[cfg(any(unix, target_os = "wasi"))] |
| 265 | + Self::Stdout(s) => s.write_all(buf), |
| 266 | + #[cfg(not(any(unix, target_os = "wasi")))] |
| 267 | + Self::Stdout(s) => { |
| 268 | + s.write_all(buf)?; |
| 269 | + // needs unsafe to remove buffering... flush after write_all to keep overhead minimal |
| 270 | + s.flush() |
| 271 | + } |
269 | 272 | } |
270 | 273 | } |
271 | 274 | } |
|
0 commit comments