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
7 changes: 5 additions & 2 deletions src/uu/sort/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> {
let file = open(path)?;
let (recycled_sender, recycled_receiver) = sync_channel(2);
let (loaded_sender, loaded_receiver) = sync_channel(2);
thread::spawn({
let reader_join_handle = thread::spawn({
let settings = settings.clone();
move || reader(file, &recycled_receiver, &loaded_sender, &settings)
});
Expand Down Expand Up @@ -111,7 +111,10 @@ pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> {
drop(recycled_sender);
while loaded_receiver.recv().is_ok() {}

result
// A read error ends the input early, so without this the part read so far
// would be checked and reported as sorted.
let reader_result = reader_join_handle.join().unwrap();
result.and(reader_result)
}

/// The function running on the reader thread.
Expand Down
14 changes: 6 additions & 8 deletions src/uu/sort/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ use std::{

use memchr::memchr_iter;
use self_cell::self_cell;
use uucore::error::{UResult, USimpleError, strip_errno};
use uucore::translate;
use uucore::error::UResult;

use crate::{GeneralBigDecimalParseResult, GlobalSettings, Line, numeric_str_cmp::NumInfo};
use crate::{
GeneralBigDecimalParseResult, GlobalSettings, Line, SortError, numeric_str_cmp::NumInfo,
};

const ALLOC_CHUNK_SIZE: usize = 64 * 1024;
const MAX_TOKEN_BUFFER_BYTES: usize = 4 * 1024 * 1024;
Expand Down Expand Up @@ -424,11 +425,8 @@ fn read_to_buffer<T: Read>(
Err(e) if e.kind() == ErrorKind::Interrupted => {
// retry
}
Err(e) => {
return Err(USimpleError::new(
2,
translate!("sort-read-failed", "error" => strip_errno(&e)),
));
Err(error) => {
return Err(SortError::ChunkReadFailed { error }.into());
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/uu/sort/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ pub enum SortError {
error: std::io::Error,
},

#[error("{}", translate!("sort-read-failed", "error" => strip_errno(.error)))]
ChunkReadFailed { error: std::io::Error },

#[error("{}", translate!("sort-open-tmp-file-failed", "error" => strip_errno(.error)))]
OpenTmpFileFailed { error: std::io::Error },

Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,22 @@ fn test_files0_from_non_utf8_filename() {
.stderr_contains("sort: cannot read");
}

#[rstest::rstest]
#[case(&["-c", "dir"])]
#[case(&["-C", "dir"])]
#[case(&["-m", "input", "dir"])]
#[cfg(target_os = "linux")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you run the GNU sort tests with this change?
bash util/build-gnu.sh && bash util/run-gnu-test.sh tests/sort/sort-merge.pl tests/sort/sort.pl

fn test_read_error_on_reader_thread(#[case] args: &[&str]) {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("dir");
at.write("input", "a\nb\n");
ucmd.args(args)
.fails_with_code(2)
.stderr_contains("sort: read failed: ")
.stderr_contains("Is a directory")
.stderr_does_not_contain("sort-read-failed");
}

#[test]
#[cfg(unix)]
fn test_files0_from_unreadable_source() {
Expand Down
Loading