@@ -365,7 +365,10 @@ pub struct Config {
365365 time_format_recent : String , // Time format for recent dates
366366 time_format_older : Option < String > , // Time format for older dates (optional, if not present, time_format_recent is used)
367367 context : bool ,
368+ #[ cfg( all( feature = "selinux" , target_os = "linux" ) ) ]
368369 selinux_supported : bool ,
370+ #[ cfg( all( feature = "smack" , target_os = "linux" ) ) ]
371+ smack_supported : bool ,
369372 group_directories_first : bool ,
370373 line_ending : LineEnding ,
371374 dired : bool ,
@@ -1157,16 +1160,10 @@ impl Config {
11571160 time_format_recent,
11581161 time_format_older,
11591162 context,
1160- selinux_supported : {
1161- #[ cfg( all( feature = "selinux" , target_os = "linux" ) ) ]
1162- {
1163- uucore:: selinux:: is_selinux_enabled ( )
1164- }
1165- #[ cfg( not( all( feature = "selinux" , target_os = "linux" ) ) ) ]
1166- {
1167- false
1168- }
1169- } ,
1163+ #[ cfg( all( feature = "selinux" , target_os = "linux" ) ) ]
1164+ selinux_supported : uucore:: selinux:: is_selinux_enabled ( ) ,
1165+ #[ cfg( all( feature = "smack" , target_os = "linux" ) ) ]
1166+ smack_supported : uucore:: smack:: is_smack_enabled ( ) ,
11701167 group_directories_first : options. get_flag ( options:: GROUP_DIRECTORIES_FIRST ) ,
11711168 line_ending : LineEnding :: from_zero_flag ( options. get_flag ( options:: ZERO ) ) ,
11721169 dired,
@@ -3387,37 +3384,59 @@ fn get_security_context<'a>(
33873384 }
33883385 }
33893386
3387+ #[ cfg( all( feature = "selinux" , target_os = "linux" ) ) ]
33903388 if config. selinux_supported {
3391- #[ cfg( all( feature = "selinux" , target_os = "linux" ) ) ]
3392- {
3393- match selinux:: SecurityContext :: of_path ( path, must_dereference, false ) {
3394- Err ( _r) => {
3395- // TODO: show the actual reason why it failed
3396- show_warning ! ( "failed to get security context of: {}" , path. quote( ) ) ;
3397- return Cow :: Borrowed ( SUBSTITUTE_STRING ) ;
3398- }
3399- Ok ( None ) => return Cow :: Borrowed ( SUBSTITUTE_STRING ) ,
3400- Ok ( Some ( context) ) => {
3401- let context = context. as_bytes ( ) ;
3389+ match selinux:: SecurityContext :: of_path ( path, must_dereference, false ) {
3390+ Err ( _r) => {
3391+ // TODO: show the actual reason why it failed
3392+ show_warning ! (
3393+ "{}" ,
3394+ translate!(
3395+ "ls-warning-failed-to-get-security-context" ,
3396+ "path" => path. quote( ) . to_string( )
3397+ )
3398+ ) ;
3399+ return Cow :: Borrowed ( SUBSTITUTE_STRING ) ;
3400+ }
3401+ Ok ( None ) => return Cow :: Borrowed ( SUBSTITUTE_STRING ) ,
3402+ Ok ( Some ( context) ) => {
3403+ let context = context. as_bytes ( ) ;
34023404
3403- let context = context. strip_suffix ( & [ 0 ] ) . unwrap_or ( context) ;
3405+ let context = context. strip_suffix ( & [ 0 ] ) . unwrap_or ( context) ;
34043406
3405- let res: String = String :: from_utf8 ( context. to_vec ( ) ) . unwrap_or_else ( |e| {
3406- show_warning ! (
3407- "getting security context of: {}: {}" ,
3408- path. quote( ) ,
3409- e. to_string( )
3410- ) ;
3407+ let res: String = String :: from_utf8 ( context. to_vec ( ) ) . unwrap_or_else ( |e| {
3408+ show_warning ! (
3409+ "{}" ,
3410+ translate!(
3411+ "ls-warning-getting-security-context" ,
3412+ "path" => path. quote( ) . to_string( ) ,
3413+ "error" => e. to_string( )
3414+ )
3415+ ) ;
34113416
3412- String :: from_utf8_lossy ( context) . to_string ( )
3413- } ) ;
3417+ String :: from_utf8_lossy ( context) . to_string ( )
3418+ } ) ;
34143419
3415- return Cow :: Owned ( res) ;
3416- }
3420+ return Cow :: Owned ( res) ;
34173421 }
34183422 }
34193423 }
34203424
3425+ #[ cfg( all( feature = "smack" , target_os = "linux" ) ) ]
3426+ if config. smack_supported {
3427+ // For SMACK, use the path to get the label
3428+ // If must_dereference is true, we follow the symlink
3429+ let target_path = if must_dereference {
3430+ std:: fs:: canonicalize ( path) . unwrap_or_else ( |_| path. to_path_buf ( ) )
3431+ } else {
3432+ path. to_path_buf ( )
3433+ } ;
3434+
3435+ return uucore:: smack:: get_smack_label_for_path ( & target_path)
3436+ . map ( Cow :: Owned )
3437+ . unwrap_or ( Cow :: Borrowed ( SUBSTITUTE_STRING ) ) ;
3438+ }
3439+
34213440 Cow :: Borrowed ( SUBSTITUTE_STRING )
34223441}
34233442
0 commit comments