Skip to content

Commit

Permalink
tail: Fix redirected directory isn't recognized as such on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
Joining7943 committed Nov 4, 2022
1 parent 436f05e commit 1b34fd8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
22 changes: 21 additions & 1 deletion src/uu/tail/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Input {
/// FreeBSD, Macos: redirected directory `tail < dir` => Pipe
/// (canonicalizing to /dev/fd/0).
/// Linux: `tail < dir` => Fifo(path_to_dir)
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
pub fn resolve(&self) -> Resolved {
match &self.kind {
InputKind::File(path) if path != &PathBuf::from(text::DEV_STDIN) => {
Expand All @@ -80,6 +80,26 @@ impl Input {
}
}

#[cfg(target_os = "macos")]
pub fn resolve(&self) -> Resolved {
use same_file::Handle;
match &self.kind {
InputKind::File(path) if path != &PathBuf::from(text::DEV_STDIN) => {
match path.canonicalize() {
Ok(path) => Resolved::CanonicalPath(path),
Err(error) => Resolved::Error(UIoError::from(error)),
}
}
InputKind::File(_) | InputKind::Stdin => match Handle::stdin() {
Ok(mut handle) => match handle.as_file_mut().is_seekable(0) {
true => Resolved::Fifo(PathBuf::from(text::DEV_STDIN)),
false => Resolved::Pipe,
},
Err(error) => Resolved::Error(UIoError::from(error)),
},
}
}

#[cfg(windows)]
pub fn resolve(&self) -> Resolved {
match &self.kind {
Expand Down
5 changes: 4 additions & 1 deletion src/uu/tail/src/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ fn tail_stdin(

observer.register_stdin(input.display_name.as_str(), Some(Box::new(reader)), true)?;
}
_ => unreachable!(),
Resolved::CanonicalPath(_) => unreachable!(),
Resolved::Error(error) => {
dbg!(&error);
}
};

Ok(())
Expand Down
34 changes: 0 additions & 34 deletions tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ fn test_follow_redirect_stdin_name_retry() {
}

#[test]
#[cfg(not(target_os = "macos"))] // See test_stdin_redirect_dir_when_target_os_is_macos
#[cfg(all(unix, not(any(target_os = "android"))))] // FIXME: fix this test for Android
fn test_stdin_redirect_dir() {
// $ mkdir dir
Expand All @@ -589,39 +588,6 @@ fn test_stdin_redirect_dir() {
.code_is(1);
}

// On macOS path.is_dir() can be false for directories if it was a redirect,
// e.g. `$ tail < DIR. The library feature to detect the
// std::io::ErrorKind::IsADirectory isn't stable so we currently show the a wrong
// error message.
// FIXME: If `std::io::ErrorKind::IsADirectory` becomes stable or macos handles
// redirected directories like linux show the correct message like in
// `test_stdin_redirect_dir`
#[test]
#[cfg(target_vendor = "apple")]
fn test_stdin_redirect_dir_when_target_os_is_macos() {
// $ mkdir dir
// $ tail < dir, $ tail - < dir
// tail: error reading 'standard input': Is a directory

let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkdir("dir");

ts.ucmd()
.set_stdin(std::fs::File::open(at.plus("dir")).unwrap())
.fails()
.no_stdout()
.stderr_is("tail: cannot open 'standard input' for reading: No such file or directory")
.code_is(1);
ts.ucmd()
.set_stdin(std::fs::File::open(at.plus("dir")).unwrap())
.arg("-")
.fails()
.no_stdout()
.stderr_is("tail: cannot open 'standard input' for reading: No such file or directory")
.code_is(1);
}

#[test]
fn test_follow_stdin_descriptor() {
let ts = TestScenario::new(util_name!());
Expand Down

0 comments on commit 1b34fd8

Please sign in to comment.