Skip to content

Commit dfac24d

Browse files
committed
head: catch I/O error with -c 1
1 parent 5127cb8 commit dfac24d

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/uu/head/src/head.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ fn wrap_in_stdout_error(err: io::Error) -> io::Error {
169169
// zero-copy fast-path
170170
#[cfg(any(target_os = "linux", target_os = "android"))]
171171
fn print_n_bytes(input: impl Read + AsFd, n: u64) -> io::Result<u64> {
172-
let out = io::stdout();
173-
uucore::pipes::send_n_bytes(input, out, n).map_err(wrap_in_stdout_error)
172+
let mut out = io::stdout();
173+
let res = uucore::pipes::send_n_bytes(input, &out, n).map_err(wrap_in_stdout_error);
174+
// flush prevents ignoring I/O error
175+
out.flush().map_err(wrap_in_stdout_error)?;
176+
res
174177
}
175178

176179
#[cfg(not(any(target_os = "linux", target_os = "android")))]
@@ -184,9 +187,7 @@ fn print_n_bytes(input: impl Read, n: u64) -> io::Result<u64> {
184187

185188
let bytes_written = io::copy(&mut reader, &mut stdout).map_err(wrap_in_stdout_error)?;
186189

187-
// Make sure we finish writing everything to the target before
188-
// exiting. Otherwise, when Rust is implicitly flushing, any
189-
// error will be silently ignored.
190+
// flush prevents ignoring I/O error
190191
stdout.flush().map_err(wrap_in_stdout_error)?;
191192

192193
Ok(bytes_written)

0 commit comments

Comments
 (0)