Skip to content

Commit 223fcae

Browse files
oech3cakebaker
authored andcommitted
yes: dedup tee()
1 parent 2ce00f9 commit 223fcae

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/uu/yes/src/yes.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,13 @@ pub fn exec(mut bytes: Vec<u8>) -> io::Result<()> {
120120
if let Ok((p_read, mut p_write)) = pipe::<true>(MAX_ROOTLESS_PIPE_SIZE)
121121
&& p_write.write_all(bytes).is_ok()
122122
{
123-
// tee() cannot control offset. Check that output is pipe by tee
124-
if safe_partial_send && tee(&p_read, &stdout, MAX_ROOTLESS_PIPE_SIZE).is_ok() {
123+
// tee() cannot control offset. But omit splice if it is possible
124+
if safe_partial_send {
125+
// fails if stdout is not a pipe
125126
while tee(&p_read, &stdout, MAX_ROOTLESS_PIPE_SIZE).is_ok() {}
126-
} else if let Ok((broker_read, broker_write)) = pipe::<true>(MAX_ROOTLESS_PIPE_SIZE) {
127-
'hybrid: while let Ok(mut remain) = tee(&p_read, &broker_write, MAX_ROOTLESS_PIPE_SIZE)
127+
}
128+
if let Ok((broker_read, broker_write)) = pipe::<true>(MAX_ROOTLESS_PIPE_SIZE) {
129+
'splice: while let Ok(mut remain) = tee(&p_read, &broker_write, MAX_ROOTLESS_PIPE_SIZE)
128130
{
129131
debug_assert!(remain == bytes.len(), "splice() should cleanup pipe");
130132
while remain > 0 {
@@ -133,7 +135,7 @@ pub fn exec(mut bytes: Vec<u8>) -> io::Result<()> {
133135
} else {
134136
// avoid output breakage with reduced remain even if it would not happen
135137
RawWriter(stdout).write_all(&bytes[bytes.len() - remain..])?;
136-
break 'hybrid;
138+
break 'splice;
137139
}
138140
}
139141
}

0 commit comments

Comments
 (0)