Skip to content

Commit 2736ee9

Browse files
committed
yes: use std::io::copy
1 parent c7e2efc commit 2736ee9

1 file changed

Lines changed: 3 additions & 46 deletions

File tree

src/uu/yes/src/yes.rs

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use clap::{Arg, ArgAction, Command, builder::ValueParser};
99
use std::ffi::OsString;
10-
use std::io::{self, Write};
10+
use std::io::self;
1111
use uucore::error::{UResult, USimpleError, strip_errno};
1212
#[cfg(any(target_os = "linux", target_os = "android"))]
1313
use uucore::pipes::MAX_ROOTLESS_PIPE_SIZE;
@@ -100,54 +100,11 @@ fn repeat_content_to_capacity(buf: &mut Vec<u8>) {
100100
}
101101
}
102102

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"))]
114103
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());
119104
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();
149106
loop {
150-
stdout.write_all(bytes)?;
107+
io::copy(&mut bytes.as_slice(), &mut *stdout)?;
151108
}
152109
}
153110

0 commit comments

Comments
 (0)