Skip to content

Commit 9f50c8b

Browse files
oech3sylvestre
authored andcommitted
tee: fix input with sleep
1 parent 1d3dba8 commit 9f50c8b

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

src/uu/tee/src/tee.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,33 +118,37 @@ fn copy(mut input: impl Read, mut output: impl Write) -> Result<usize> {
118118
// the standard library:
119119
// https://github.com/rust-lang/rust/blob/2feb91181882e525e698c4543063f4d0296fcf91/library/std/src/io/copy.rs#L271-L297
120120

121-
// Use small buffer size from std implementation for small input
121+
// Use buffer size from std implementation
122122
// https://github.com/rust-lang/rust/blob/2feb91181882e525e698c4543063f4d0296fcf91/library/std/src/sys/io/mod.rs#L44
123-
const FIRST_BUF_SIZE: usize = if cfg!(target_os = "espidf") {
123+
const BUF_SIZE: usize = if cfg!(target_os = "espidf") {
124124
512
125125
} else {
126126
8 * 1024
127127
};
128-
let mut buffer = [0u8; FIRST_BUF_SIZE];
128+
let mut buffer = [0u8; BUF_SIZE];
129129
let mut len = 0;
130-
match input.read(&mut buffer) {
131-
Ok(0) => return Ok(0),
132-
Ok(bytes_count) => {
133-
output.write_all(&buffer[0..bytes_count])?;
134-
len = bytes_count;
135-
if bytes_count < FIRST_BUF_SIZE {
130+
131+
loop {
132+
match input.read(&mut buffer) {
133+
Ok(0) => return Ok(len), // end of file
134+
Ok(received) => {
135+
output.write_all(&buffer[..received])?;
136136
// flush the buffer to comply with POSIX requirement that
137137
// `tee` does not buffer the input.
138138
output.flush()?;
139-
return Ok(len);
139+
len += received;
140+
if len > 2 * BUF_SIZE {
141+
// buffer is too small
142+
break;
143+
}
140144
}
145+
Err(e) if e.kind() == ErrorKind::Interrupted => {}
146+
Err(e) => return Err(e),
141147
}
142-
Err(e) if e.kind() == ErrorKind::Interrupted => (),
143-
Err(e) => return Err(e),
144148
}
145-
146-
// but optimize buffer size also for large file
147-
let mut buffer = vec![0u8; 4 * FIRST_BUF_SIZE]; //stack array makes code path for smaller file slower
149+
// optimize for large input
150+
//stack array makes code path for smaller file slower
151+
let mut buffer = vec![0u8; 4 * BUF_SIZE];
148152
loop {
149153
match input.read(&mut buffer) {
150154
Ok(0) => return Ok(len), // end of file

util/fetch-gnu.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ curl -L "${repo}/releases/download/v${ver}/coreutils-${ver}.tar.xz" | tar --stri
66
# TODO stop backporting tests from master at GNU coreutils > $ver
77
backport=(
88
misc/coreutils.sh # enable test
9+
tee/tee.sh # input containing sleep
910
misc/yes.sh # zero-copy
1011
)
1112
for f in "${backport[@]}"

0 commit comments

Comments
 (0)