1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

Merge pull request #5255 from granquet/split_filter_broken_pipe

split: catch broken pipe error for round robin strategy
This commit is contained in:
Daniel Hofstetter 2023-09-13 14:08:45 +02:00 committed by GitHub
commit 39a0e92a91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View file

@ -1575,7 +1575,7 @@ fn split_into_n_chunks_by_line_round_robin<R>(
settings: &Settings,
reader: &mut R,
num_chunks: u64,
) -> UResult<()>
) -> std::io::Result<()>
where
R: BufRead,
{
@ -1586,7 +1586,8 @@ where
settings.suffix_length,
settings.suffix_type,
settings.suffix_start,
)?;
)
.map_err(|e| io::Error::new(ErrorKind::Other, format!("{e}")))?;
// Create one writer for each chunk. This will create each
// of the underlying files (if not in `--filter` mode).
@ -1594,7 +1595,7 @@ where
for _ in 0..num_chunks {
let filename = filename_iterator
.next()
.ok_or_else(|| USimpleError::new(1, "output file suffixes exhausted"))?;
.ok_or_else(|| io::Error::new(ErrorKind::Other, "output file suffixes exhausted"))?;
let writer = settings.instantiate_current_writer(filename.as_str())?;
writers.push(writer);
}
@ -1686,7 +1687,13 @@ fn split(settings: &Settings) -> UResult<()> {
kth_chunk_by_line(settings, &mut reader, chunk_number, num_chunks)
}
Strategy::Number(NumberType::RoundRobin(num_chunks)) => {
split_into_n_chunks_by_line_round_robin(settings, &mut reader, num_chunks)
match split_into_n_chunks_by_line_round_robin(settings, &mut reader, num_chunks) {
Ok(_) => Ok(()),
Err(e) => match e.kind() {
ErrorKind::BrokenPipe => Ok(()),
_ => Err(USimpleError::new(1, format!("{e}"))),
},
}
}
Strategy::Number(NumberType::KthRoundRobin(chunk_number, num_chunks)) => {
// The chunk number is given as a 1-indexed number, but it

View file

@ -327,6 +327,17 @@ fn test_filter_command_fails() {
.fails();
}
#[test]
#[cfg(unix)]
fn test_filter_broken_pipe() {
let (at, mut ucmd) = at_and_ucmd!();
let name = "filter-big-input";
RandomFile::new(&at, name).add_lines(1024 * 10);
ucmd.args(&["--filter=head -c1 > /dev/null", "-n", "r/1", name])
.succeeds();
}
#[test]
fn test_split_lines_number() {
// Test if stdout/stderr for '--lines' option is correct