From 5b04099c1aac53d577b143a5cd7b48f1ce4e5f9d Mon Sep 17 00:00:00 2001 From: oech3 <79379754+oech3@users.noreply.github.com> Date: Thu, 30 Apr 2026 22:01:53 +0900 Subject: [PATCH] yes: remove cfg & move ownership of buffer to fn exec --- src/uu/yes/src/yes.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/uu/yes/src/yes.rs b/src/uu/yes/src/yes.rs index 208f155e1cc..819ce3a8a18 100644 --- a/src/uu/yes/src/yes.rs +++ b/src/uu/yes/src/yes.rs @@ -27,16 +27,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?; #[allow(clippy::unwrap_used, reason = "clap provides 'y' by default")] - let mut buffer = args_into_buffer(matches.get_many::("STRING").unwrap())?; - #[cfg(any(target_os = "linux", target_os = "android"))] - let aligned_before_growing = PAGE_SIZE.is_multiple_of(buffer.len()); - - prepare_buffer(&mut buffer); - #[cfg(any(target_os = "linux", target_os = "android"))] - let res = exec(&buffer, aligned_before_growing); - #[cfg(not(any(target_os = "linux", target_os = "android")))] - let res = exec(&buffer); - match res { + let buffer = args_into_buffer(matches.get_many::("STRING").unwrap())?; + + match exec(buffer) { Ok(()) => Ok(()), // On Windows, silently handle broken pipe since there's no SIGPIPE #[cfg(windows)] @@ -98,7 +91,7 @@ fn args_into_buffer<'a>(i: impl Iterator) -> UResult) { +fn repeat_content_to_capacity(buf: &mut Vec) { let line_len = buf.len(); debug_assert!(line_len > 0, "buffer is not empty since we have newline"); let target_size = line_len * (buf.capacity() / line_len); // 0 if line_len is already large enough @@ -111,7 +104,9 @@ fn prepare_buffer(buf: &mut Vec) { } #[cfg(not(any(target_os = "linux", target_os = "android")))] -pub fn exec(bytes: &[u8]) -> io::Result<()> { +pub fn exec(mut bytes: Vec) -> io::Result<()> { + repeat_content_to_capacity(&mut bytes); + let bytes = bytes.as_slice(); let mut stdout = io::stdout().lock(); loop { stdout.write_all(bytes)?; @@ -119,8 +114,11 @@ pub fn exec(bytes: &[u8]) -> io::Result<()> { } #[cfg(any(target_os = "linux", target_os = "android"))] -pub fn exec(bytes: &[u8], aligned: bool) -> io::Result<()> { +pub fn exec(mut bytes: Vec) -> io::Result<()> { use uucore::pipes::{pipe, splice, tee}; + let aligned = PAGE_SIZE.is_multiple_of(bytes.len()); + repeat_content_to_capacity(&mut bytes); + let bytes = bytes.as_slice(); let mut stdout = io::stdout(); // no need to lock with zero-copy // don't show any error from fast-path and fallback to write for proper message if let Ok((p_read, mut p_write)) = pipe() @@ -181,7 +179,7 @@ mod tests { for (line, final_len) in tests { let mut v = Vec::with_capacity(BUF_SIZE); v.extend(std::iter::repeat_n(b'a', line)); - prepare_buffer(&mut v); + repeat_content_to_capacity(&mut v); assert_eq!(v.len(), final_len); } }