Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/uu/head/src/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,19 @@ impl HeadOptions {
Some(v) => v.cloned().collect(),
None => vec!["-".to_owned()],
};
//println!("{:#?}", options);

Ok(options)
}
}

#[inline]
fn wrap_in_stdout_error(err: io::Error) -> io::Error {
io::Error::new(
err.kind(),
format!("error writing 'standard output': {}", err),
)
}

fn read_n_bytes(input: impl Read, n: u64) -> std::io::Result<u64> {
// Read the first `n` bytes from the `input` reader.
let mut reader = input.take(n);
Expand All @@ -251,12 +259,12 @@ fn read_n_bytes(input: impl Read, n: u64) -> std::io::Result<u64> {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();

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

// Make sure we finish writing everything to the target before
// exiting. Otherwise, when Rust is implicitly flushing, any
// error will be silently ignored.
stdout.flush()?;
stdout.flush().map_err(wrap_in_stdout_error)?;

Ok(bytes_written)
}
Expand All @@ -268,12 +276,12 @@ fn read_n_lines(input: &mut impl std::io::BufRead, n: u64, separator: u8) -> std
// Write those bytes to `stdout`.
let mut stdout = std::io::stdout();

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

// Make sure we finish writing everything to the target before
// exiting. Otherwise, when Rust is implicitly flushing, any
// error will be silently ignored.
stdout.flush()?;
stdout.flush().map_err(wrap_in_stdout_error)?;

Ok(bytes_written)
}
Expand All @@ -298,13 +306,13 @@ fn read_but_last_n_bytes(input: impl std::io::BufRead, n: u64) -> std::io::Resul
// over the top. This gives a significant speedup (approx 4x).
let mut writer = BufWriter::with_capacity(BUF_SIZE, stdout);
for byte in take_all_but(input.bytes(), n) {
writer.write_all(&[byte?])?;
writer.write_all(&[byte?]).map_err(wrap_in_stdout_error)?;
bytes_written += 1;
}
// Make sure we finish writing everything to the target before
// exiting. Otherwise, when Rust is implicitly flushing, any
// error will be silently ignored.
writer.flush()?;
writer.flush().map_err(wrap_in_stdout_error)?;
}
Ok(bytes_written)
}
Expand All @@ -318,15 +326,17 @@ fn read_but_last_n_lines(
if let Some(n) = catch_too_large_numbers_in_backwards_bytes_or_lines(n) {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();

for bytes in take_all_but(lines(input, separator), n) {
let bytes = bytes?;
bytes_written += u64::try_from(bytes.len()).unwrap();
stdout.write_all(&bytes)?;

stdout.write_all(&bytes).map_err(wrap_in_stdout_error)?;
}
// Make sure we finish writing everything to the target before
// exiting. Otherwise, when Rust is implicitly flushing, any
// error will be silently ignored.
stdout.flush()?;
stdout.flush().map_err(wrap_in_stdout_error)?;
}
Ok(bytes_written)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/by-util/test_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ fn test_write_to_dev_full() {
.pipe_in_fixture(INPUT)
.set_stdout(dev_full)
.run()
.stderr_contains("No space left on device");
.stderr_contains("error writing 'standard output': No space left on device");
}
}
}
Loading