diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 6e4cd8a23..186b7dc67 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -1106,6 +1106,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 { ); } + // Verify that we can open all input files. + // It is the correct behavior to close all files afterwards, + // and to reopen them at a later point. This is different from how the output file is handled, + // probably to prevent running out of file descriptors. + for file in &files { + open(file); + } + let output = Output::new(matches.value_of(options::OUTPUT)); settings.init_precomputed(); diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index b4b4ba41c..74241ef93 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -979,10 +979,26 @@ fn test_verifies_out_file() { } #[test] -fn test_verifies_out_file_after_keys() { +fn test_verifies_files_after_keys() { new_ucmd!() - .args(&["-o", "nonexistent_dir/nonexistent_file", "-k", "0"]) + .args(&[ + "-o", + "nonexistent_dir/nonexistent_file", + "-k", + "0", + "nonexistent_dir/input_file", + ]) .fails() .status_code(2) .stderr_contains("failed to parse key"); } + +#[test] +#[cfg(unix)] +fn test_verifies_input_files() { + new_ucmd!() + .args(&["/dev/random", "nonexistent_file"]) + .fails() + .status_code(2) + .stderr_is("sort: cannot read: nonexistent_file: No such file or directory"); +}