10000 head: improve error mgmt. by sylvestre · Pull Request #7408 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

head: improve error mgmt. #7408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2025
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
0