From 84480f892dd2b853c8ba7f089bf3642ef06aef6d Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Thu, 19 May 2022 22:55:47 +0200 Subject: [PATCH] tail: add equivalent of stdin_is_pipe_or_fifo() for Windows * add support to determine if stdin is readable on Windows --- Cargo.lock | 1 + src/uu/tail/Cargo.toml | 1 + src/uu/tail/src/tail.rs | 11 +++++++---- tests/by-util/test_tail.rs | 4 +++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e57d1a61..f3892fa3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2939,6 +2939,7 @@ dependencies = [ "notify", "uucore", "winapi 0.3.9", + "winapi-util", ] [[package]] diff --git a/src/uu/tail/Cargo.toml b/src/uu/tail/Cargo.toml index d08fe0ecb..d4d9c5d5e 100644 --- a/src/uu/tail/Cargo.toml +++ b/src/uu/tail/Cargo.toml @@ -24,6 +24,7 @@ uucore = { version=">=0.0.11", package="uucore", path="../../uucore", features=[ [target.'cfg(windows)'.dependencies] winapi = { version="0.3", features=["fileapi", "handleapi", "processthreadsapi", "synchapi", "winbase"] } +winapi-util = { version= "0.1.5" } [target.'cfg(unix)'.dependencies] nix = { version = "0.24.1", default-features = false, features=["fs"] } diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index ddbfe701a..fcb5d4975 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -1435,10 +1435,13 @@ pub fn stdin_is_pipe_or_fifo() -> bool { { platform::stdin_is_pipe_or_fifo() } - // FIXME windows has GetFileType which can determine if the file is a pipe/FIFO - // so this check can also be performed - #[cfg(not(unix))] - false + #[cfg(windows)] + { + use winapi_util; + winapi_util::file::typ(winapi_util::HandleRef::stdin()) + .map(|t| t.is_disk() || t.is_pipe()) + .unwrap_or(false) + } } pub fn stdin_is_bad_fd() -> bool { diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index 695b15f99..e2ddfb146 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -11,7 +11,9 @@ extern crate tail; use crate::common::util::*; use std::char::from_digit; -use std::io::{Read, Write}; +#[cfg(unix)] +use std::io::Read; +use std::io::Write; use std::process::Stdio; #[cfg(unix)] use std::thread::sleep;