Skip to content

Commit ca6d35c

Browse files
committed
tee: fix input with sleep
1 parent 66d4fae commit ca6d35c

1 file changed

Lines changed: 19 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

0 commit comments

Comments
 (0)