@@ -12,7 +12,7 @@ use std::{borrow::Cow, io::Write};
1212
1313use chrono:: { format:: StrftimeItems , DateTime , Local } ;
1414
15- use super :: { FileType , Matcher , MatcherIO , WalkEntry , WalkError } ;
15+ use super :: { FileType , Follow , Matcher , MatcherIO , WalkEntry } ;
1616
1717#[ cfg( unix) ]
1818use std:: os:: unix:: prelude:: MetadataExt ;
@@ -359,18 +359,6 @@ fn get_starting_point(file_info: &WalkEntry) -> &Path {
359359 . unwrap ( )
360360}
361361
362- fn format_non_link_file_type ( file_type : FileType ) -> char {
363- match file_type {
364- FileType :: Regular => 'f' ,
365- FileType :: Directory => 'd' ,
366- FileType :: BlockDevice => 'b' ,
367- FileType :: CharDevice => 'c' ,
368- FileType :: Fifo => 'p' ,
369- FileType :: Socket => 's' ,
370- _ => 'U' ,
371- }
372- }
373-
374362fn format_directive < ' entry > (
375363 file_info : & ' entry WalkEntry ,
376364 directive : & FormatDirective ,
@@ -524,7 +512,7 @@ fn format_directive<'entry>(
524512 FormatDirective :: StartingPoint => get_starting_point ( file_info) . to_string_lossy ( ) ,
525513
526514 FormatDirective :: SymlinkTarget => {
527- if file_info. path_is_symlink ( ) {
515+ if file_info. file_type ( ) . is_symlink ( ) {
528516 fs:: read_link ( file_info. path ( ) ) ?
529517 . to_string_lossy ( )
530518 . into_owned ( )
@@ -534,22 +522,43 @@ fn format_directive<'entry>(
534522 }
535523 }
536524
537- FormatDirective :: Type { follow_links } => if file_info. path_is_symlink ( ) {
538- if * follow_links {
539- match file_info. path ( ) . metadata ( ) . map_err ( WalkError :: from) {
540- Ok ( meta) => format_non_link_file_type ( meta. file_type ( ) . into ( ) ) ,
541- Err ( e) if e. is_not_found ( ) => 'N' ,
542- Err ( e) if e. is_loop ( ) => 'L' ,
543- Err ( _) => '?' ,
544- }
525+ FormatDirective :: Type { follow_links } => {
526+ let follow = if * follow_links {
527+ Follow :: Force
528+ } else if file_info. follow ( ) {
529+ Follow :: Always
545530 } else {
546- 'l'
547- }
548- } else {
549- format_non_link_file_type ( file_info. file_type ( ) )
531+ Follow :: Never
532+ } ;
533+ let meta = follow. metadata ( file_info) ;
534+ let ftype = meta
535+ . as_ref ( )
536+ . map ( |m| m. file_type ( ) . into ( ) )
537+ . unwrap_or ( FileType :: Unknown ) ;
538+
539+ let ret = match ftype {
540+ FileType :: Regular => "f" ,
541+ FileType :: Directory => "d" ,
542+ FileType :: BlockDevice => "b" ,
543+ FileType :: CharDevice => "c" ,
544+ FileType :: Fifo => "p" ,
545+ FileType :: Socket => "s" ,
546+ FileType :: Symlink => "l" ,
547+ FileType :: Unknown if * follow_links => {
548+ match meta {
549+ Err ( e) if e. is_not_found ( ) => "N" ,
550+ Err ( e) if e. is_loop ( ) => "L" ,
551+ Err ( _) => {
552+ // TODO: matcher_io.set_exit_code(1);
553+ "?"
554+ }
555+ _ => "U" ,
556+ }
557+ }
558+ FileType :: Unknown => "U" ,
559+ } ;
560+ ret. into ( )
550561 }
551- . to_string ( )
552- . into ( ) ,
553562
554563 #[ cfg( not( unix) ) ]
555564 FormatDirective :: User { .. } => "0" . into ( ) ,
0 commit comments