|
7 | 7 |
|
8 | 8 | use clap::{Arg, ArgAction, Command, builder::ValueParser}; |
9 | 9 | use std::ffi::OsString; |
10 | | -use std::io::{self, Write}; |
| 10 | +use std::io::{self}; |
11 | 11 | use uucore::error::{UResult, USimpleError, strip_errno}; |
12 | 12 | #[cfg(any(target_os = "linux", target_os = "android"))] |
13 | 13 | use uucore::pipes::MAX_ROOTLESS_PIPE_SIZE; |
@@ -100,54 +100,11 @@ fn repeat_content_to_capacity(buf: &mut Vec<u8>) { |
100 | 100 | } |
101 | 101 | } |
102 | 102 |
|
103 | | -#[cfg(not(any(target_os = "linux", target_os = "android")))] |
104 | | -pub fn exec(mut bytes: Vec<u8>) -> io::Result<()> { |
105 | | - repeat_content_to_capacity(&mut bytes); |
106 | | - let bytes = bytes.as_slice(); |
107 | | - let mut stdout = io::stdout().lock(); |
108 | | - loop { |
109 | | - stdout.write_all(bytes)?; |
110 | | - } |
111 | | -} |
112 | | - |
113 | | -#[cfg(any(target_os = "linux", target_os = "android"))] |
114 | 103 | pub fn exec(mut bytes: Vec<u8>) -> io::Result<()> { |
115 | | - use uucore::pipes::{pipe, splice, tee}; |
116 | | - |
117 | | - const PAGE_SIZE: usize = 4096; |
118 | | - let aligned = PAGE_SIZE.is_multiple_of(bytes.len()); |
119 | 104 | repeat_content_to_capacity(&mut bytes); |
120 | | - let bytes = bytes.as_slice(); |
121 | | - let mut stdout = io::stdout(); // no need to lock with zero-copy |
122 | | - // don't show any error from fast-path and fallback to write for proper message |
123 | | - if let Ok((p_read, mut p_write)) = pipe() |
124 | | - // todo: zero-copy with default size when fcntl failed |
125 | | - && rustix::pipe::fcntl_setpipe_size(&stdout, MAX_ROOTLESS_PIPE_SIZE).is_ok() |
126 | | - && p_write.write_all(bytes).is_ok() |
127 | | - { |
128 | | - if aligned && tee(&p_read, &stdout, MAX_ROOTLESS_PIPE_SIZE).is_ok() { |
129 | | - while let Ok(1..) = tee(&p_read, &stdout, MAX_ROOTLESS_PIPE_SIZE) {} |
130 | | - } else if let Ok((broker_read, broker_write)) = pipe() { |
131 | | - // tee() cannot control offset and write to non-pipe |
132 | | - 'hybrid: while let Ok(mut remain) = tee(&p_read, &broker_write, MAX_ROOTLESS_PIPE_SIZE) |
133 | | - { |
134 | | - debug_assert!(remain == bytes.len(), "splice() should cleanup pipe"); |
135 | | - while remain > 0 { |
136 | | - if let Ok(s) = splice(&broker_read, &stdout, remain) { |
137 | | - remain -= s; |
138 | | - } else { |
139 | | - // avoid output breakage with reduced remain even if it would not happen |
140 | | - stdout.write_all(&bytes[bytes.len() - remain..])?; |
141 | | - break 'hybrid; |
142 | | - } |
143 | | - } |
144 | | - } |
145 | | - } |
146 | | - } |
147 | | - // fallback |
148 | | - let mut stdout = stdout.lock(); |
| 105 | + let mut stdout = uucore::stdio::stdout_raw(); |
149 | 106 | loop { |
150 | | - stdout.write_all(bytes)?; |
| 107 | + io::copy(&mut bytes.as_slice(), &mut *stdout)?; |
151 | 108 | } |
152 | 109 | } |
153 | 110 |
|
|
0 commit comments