Skip to content
Closed
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
25 changes: 15 additions & 10 deletions src/uu/sort/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2438,8 +2438,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
});
}

if !(settings.merge || settings.check) {
check_inputs(&files)?;
// Creating the output first could make a missing merge input appear to exist.
if !settings.check {
check_inputs(&files, settings.merge)?;
}

let output = Output::new(matches.get_one::<OsString>(options::OUTPUT))?;
Expand Down Expand Up @@ -3352,17 +3353,17 @@ fn print_sorted<'a, T: Iterator<Item = &'a Line<'a>>>(
Ok(())
}

/// Check that all inputs are readable before sorting starts, like GNU sort's
/// `check_inputs`. The inputs are probed with `access(2)` rather than opened:
/// Check named inputs before creating the output, like GNU sort's
/// `check_inputs`. Where available, probe with `access(2)` rather than opening:
/// opening a FIFO here would block until a writer appears and lose data on the
/// reopen, and keeping every input open would be bounded by `RLIMIT_NOFILE`.
fn check_inputs(files: &[OsString]) -> UResult<()> {
fn check_inputs(files: &[OsString], merge: bool) -> UResult<()> {
for file in files {
if file == STDIN_FILE {
continue;
}
let path = Path::new(file);
check_readable(path).map_err(|error| SortError::ReadFailed {
check_readable(path, merge).map_err(|error| SortError::ReadFailed {
path: path.to_owned(),
error,
})?;
Expand All @@ -3371,16 +3372,20 @@ fn check_inputs(files: &[OsString]) -> UResult<()> {
}

#[cfg(all(unix, not(target_os = "redox")))]
fn check_readable(path: &Path) -> std::io::Result<()> {
fn check_readable(path: &Path, _merge: bool) -> std::io::Result<()> {
use rustix::fs::{Access, access};

access(path, Access::READ_OK).map_err(|e| std::io::Error::from_raw_os_error(e.raw_os_error()))
}

#[cfg(any(not(unix), target_os = "redox"))]
fn check_readable(path: &Path) -> std::io::Result<()> {
// No `rustix::fs::access` here, so open the file and close it right away.
File::open(path)?;
fn check_readable(path: &Path, merge: bool) -> std::io::Result<()> {
if merge {
// Check existence without opening a stream twice. The merge opens it for reading.
path.metadata()?;
} else {
File::open(path)?;
}
Ok(())
}

Expand Down
12 changes: 12 additions & 0 deletions tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,18 @@ fn test_output_is_input() {
assert_eq!(at.read("file"), input);
}

#[test]
fn test_merge_missing_input_is_output() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("today", "TX-1042\n");

ucmd.args(&["-mu", "-o", "ledger", "ledger", "today"])
.fails_with_code(2)
.no_stdout()
.stderr_contains("cannot read: ledger");
assert!(!at.file_exists("ledger"));
}

#[rstest]
#[case::newline('\n', &[])]
#[case::newline_unique('\n', &["-u"])]
Expand Down
Loading