@@ -26,7 +26,7 @@ use args::{FilterMode, Settings, Signum, parse_args};
2626use chunks:: ReverseChunks ;
2727use follow:: Observer ;
2828use memchr:: { memchr_iter, memrchr_iter} ;
29- use paths:: { FileExtTail , HeaderPrinter , Input , InputKind } ;
29+ use paths:: { FileExtTail , HeaderPrinter , Input , InputKind , MetadataExtTail } ;
3030use std:: cmp:: Ordering ;
3131use std:: fs:: File ;
3232use std:: io:: { self , BufReader , BufWriter , ErrorKind , Read , Seek , SeekFrom , Write , stdin, stdout} ;
@@ -102,7 +102,10 @@ fn uu_tail(settings: &Settings) -> UResult<()> {
102102 the input file is not a FIFO, pipe, or regular file, it is unspecified whether or
103103 not the -f option shall be ignored.
104104 */
105- if !settings. has_only_stdin ( ) || settings. pid . is_some_and ( |pid| pid != 0 ) {
105+ if !settings. has_only_stdin ( )
106+ || settings. pid . is_some_and ( |pid| pid != 0 )
107+ || observer. stdin_is_tailable ( )
108+ {
106109 follow:: follow ( observer, settings) ?;
107110 }
108111 }
@@ -184,7 +187,8 @@ fn tail_file(
184187 reader = BufReader :: new ( file) ;
185188 unbounded_tail ( & mut reader, settings) ?;
186189 }
187- if input. is_tailable ( ) {
190+ // Use the opened file's metadata rather than resolving stdin again.
191+ if st. is_tailable ( ) {
188192 observer. add_path (
189193 path,
190194 input. display_name . as_str ( ) ,
@@ -252,22 +256,6 @@ fn tail_stdin(
252256 input : & Input ,
253257 observer : & mut Observer ,
254258) -> UResult < ( ) > {
255- // on macOS, resolve() will always return None for stdin,
256- // we need to detect if stdin is a directory ourselves.
257- // fstat-ing certain descriptors under /dev/fd fails with
258- // bad file descriptor or might not catch directory cases
259- // e.g. see the differences between running ls -l /dev/stdin /dev/fd/0
260- // on macOS and Linux.
261- #[ cfg( target_vendor = "apple" ) ]
262- if uucore:: fs:: is_stdin_directory ( & stdin ( ) ) {
263- set_exit_code ( 1 ) ;
264- show_error ! (
265- "{}" ,
266- translate!( "tail-error-cannot-open-no-such-file" , "file" => input. display_name, "error" => translate!( "tail-no-such-file-or-directory" ) )
267- ) ;
268- return Ok ( ( ) ) ;
269- }
270-
271259 // Check if stdin was closed before Rust reopened it as /dev/null
272260 if paths:: stdin_is_bad_fd ( ) {
273261 set_exit_code ( 1 ) ;
@@ -279,40 +267,80 @@ fn tail_stdin(
279267 return Ok ( ( ) ) ;
280268 }
281269
282- if let Some ( path) = input. resolve ( ) {
283- #[ cfg( not( unix) ) ]
284- let stdin_offset = 0 ;
285- // Save the current seek position/offset of a stdin redirected file.
286- // This is needed to pass "gnu/tests/tail-2/start-middle.sh"
270+ let resolved_stdin = input. resolve ( ) . or_else ( resolve_stdin_path) ;
271+
272+ if let Some ( ref path) = resolved_stdin {
287273 #[ cfg( unix) ]
288- let stdin_offset = rustix:: fs:: tell ( stdin ( ) ) . unwrap_or ( 0 ) ; // fifo
289- tail_file (
290- settings,
291- header_printer,
292- input,
293- & path,
294- observer,
295- stdin_offset,
296- ) ?;
297- } else {
298- // pipe
299- header_printer. print_input ( input) ;
300- if paths:: stdin_is_bad_fd ( ) {
301- set_exit_code ( 1 ) ;
274+ let ( stdin_offset, stdin_is_seekable_file) = (
275+ rustix:: fs:: tell ( stdin ( ) ) . unwrap_or ( 0 ) ,
276+ rustix:: fs:: fstat ( stdin ( ) ) . ok ( ) . map ( |stat| {
277+ rustix:: fs:: FileType :: from_raw_mode ( stat. st_mode )
278+ == rustix:: fs:: FileType :: RegularFile
279+ } ) ,
280+ ) ;
281+ #[ cfg( windows) ]
282+ let ( stdin_offset, stdin_is_seekable_file) = {
283+ use std:: os:: windows:: io:: AsHandle ;
284+
285+ stdin ( )
286+ . as_handle ( )
287+ . try_clone_to_owned ( )
288+ . map_or ( ( 0 , None ) , |handle| {
289+ let mut file = File :: from ( handle) ;
290+ (
291+ file. stream_position ( ) . unwrap_or ( 0 ) ,
292+ file. metadata ( ) . ok ( ) . map ( |meta| meta. is_file ( ) ) ,
293+ )
294+ } )
295+ } ;
296+ #[ cfg( not( any( unix, windows) ) ) ]
297+ let ( stdin_offset, stdin_is_seekable_file) = ( 0 , None ) ;
298+
299+ // Only fall back to the path if stdin's metadata could not be obtained.
300+ let stdin_is_seekable_file = stdin_is_seekable_file
301+ . unwrap_or_else ( || path. metadata ( ) . is_ok_and ( |meta| meta. is_file ( ) ) ) ;
302+
303+ if stdin_is_seekable_file || path. is_dir ( ) {
304+ tail_file (
305+ settings,
306+ header_printer,
307+ input,
308+ path,
309+ observer,
310+ stdin_offset,
311+ ) ?;
312+
313+ if stdin_is_seekable_file {
314+ observer. set_stdin_key ( path. clone ( ) ) ?;
315+ }
316+ return Ok ( ( ) ) ;
317+ }
318+ }
319+
320+ header_printer. print_input ( input) ;
321+ if paths:: stdin_is_bad_fd ( ) {
322+ set_exit_code ( 1 ) ;
323+ show_error ! (
324+ "{}" ,
325+ translate!(
326+ "tail-error-cannot-fstat" ,
327+ "file" => translate!( "tail-stdin-header" ) ,
328+ "error" => translate!( "tail-bad-fd" )
329+ )
330+ ) ;
331+ if settings. follow . is_some ( ) {
302332 show_error ! (
303333 "{}" ,
304- translate!( "tail-error-cannot-fstat" , "file" => translate!( "tail-stdin-header" ) , "error" => translate!( "tail-bad-fd" ) )
334+ translate!(
335+ "tail-error-reading-file" ,
336+ "file" => translate!( "tail-stdin-header" ) ,
337+ "error" => translate!( "tail-bad-fd" )
338+ )
305339 ) ;
306- if settings. follow . is_some ( ) {
307- show_error ! (
308- "{}" ,
309- translate!( "tail-error-reading-file" , "file" => translate!( "tail-stdin-header" ) , "error" => translate!( "tail-bad-fd" ) )
310- ) ;
311- }
312- } else {
313- let mut reader = BufReader :: new ( stdin ( ) ) ;
314- unbounded_tail ( & mut reader, settings) ?;
315340 }
341+ } else {
342+ let mut reader = BufReader :: new ( stdin ( ) ) ;
343+ unbounded_tail ( & mut reader, settings) ?;
316344 }
317345
318346 Ok ( ( ) )
@@ -601,6 +629,39 @@ fn print_target_section<
601629 Ok ( ( ) )
602630}
603631
632+ #[ cfg( windows) ]
633+ fn resolve_stdin_path ( ) -> Option < PathBuf > {
634+ use std:: os:: windows:: io:: AsRawHandle ;
635+ use windows_sys:: Win32 :: Foundation :: MAX_PATH ;
636+ use windows_sys:: Win32 :: Storage :: FileSystem :: { FILE_NAME_OPENED , GetFinalPathNameByHandleW } ;
637+
638+ let handle = stdin ( ) . lock ( ) . as_raw_handle ( ) ;
639+ if handle. is_null ( ) {
640+ return None ;
641+ }
642+
643+ let mut buffer = [ 0u16 ; MAX_PATH as usize ] ;
644+ let len = unsafe {
645+ GetFinalPathNameByHandleW (
646+ handle,
647+ buffer. as_mut_ptr ( ) ,
648+ buffer. len ( ) as u32 ,
649+ FILE_NAME_OPENED ,
650+ )
651+ } as usize ;
652+
653+ if len == 0 || len >= buffer. len ( ) {
654+ return None ;
655+ }
656+
657+ String :: from_utf16 ( & buffer[ ..len] ) . ok ( ) . map ( PathBuf :: from)
658+ }
659+
660+ #[ cfg( not( windows) ) ]
661+ fn resolve_stdin_path ( ) -> Option < PathBuf > {
662+ None
663+ }
664+
604665#[ cfg( test) ]
605666mod tests {
606667
0 commit comments