diff --git a/src/uu/comm/src/comm.rs b/src/uu/comm/src/comm.rs index cae405865e6..36f4857ba50 100644 --- a/src/uu/comm/src/comm.rs +++ b/src/uu/comm/src/comm.rs @@ -6,7 +6,7 @@ // spell-checker:ignore (ToDO) delim mkdelim use std::cmp::Ordering; -use std::fs::File; +use std::fs::{metadata, File}; use std::io::{self, stdin, BufRead, BufReader, Stdin}; use std::path::Path; use uucore::error::{FromIo, UResult, USimpleError}; @@ -130,7 +130,11 @@ fn open_file(name: &str, line_ending: LineEnding) -> io::Result { if name == "-" { Ok(LineReader::new(Input::Stdin(stdin()), line_ending)) } else { - let f = File::open(Path::new(name))?; + let path = Path::new(name); + if metadata(path)?.is_dir() { + return Err(io::Error::new(io::ErrorKind::Other, "Is a directory")); + } + let f = File::open(path)?; Ok(LineReader::new( Input::FileIn(BufReader::new(f)), line_ending, diff --git a/tests/by-util/test_comm.rs b/tests/by-util/test_comm.rs index 2dc385ef3f2..c67f8897f93 100644 --- a/tests/by-util/test_comm.rs +++ b/tests/by-util/test_comm.rs @@ -292,3 +292,36 @@ fn test_no_such_file() { .fails() .stderr_only("comm: bogus_file_1: No such file or directory\n"); } + +#[test] +fn test_is_dir() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + scene + .ucmd() + .args(&[".", "."]) + .fails() + .stderr_only("comm: .: Is a directory\n"); + at.mkdir("dir"); + + scene + .ucmd() + .args(&["dir", "."]) + .fails() + .stderr_only("comm: dir: Is a directory\n"); + + at.touch("file"); + scene + .ucmd() + .args(&[".", "file"]) + .fails() + .stderr_only("comm: .: Is a directory\n"); + + at.touch("file"); + scene + .ucmd() + .args(&["file", "."]) + .fails() + .stderr_only("comm: .: Is a directory\n"); +}