@@ -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,33 +3384,53 @@ 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 ! ( "failed to get security context of: {}" , path. quote( ) ) ;
3393+ return Cow :: Borrowed ( SUBSTITUTE_STRING ) ;
3394+ }
3395+ Ok ( None ) => return Cow :: Borrowed ( SUBSTITUTE_STRING ) ,
3396+ Ok ( Some ( context) ) => {
3397+ let context = context. as_bytes ( ) ;
34023398
3403- let context = context. strip_suffix ( & [ 0 ] ) . unwrap_or ( context) ;
3399+ let context = context. strip_suffix ( & [ 0 ] ) . unwrap_or ( context) ;
34043400
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- ) ;
3401+ let res: String = String :: from_utf8 ( context. to_vec ( ) ) . unwrap_or_else ( |e| {
3402+ show_warning ! (
3403+ "getting security context of: {}: {}" ,
3404+ path. quote( ) ,
3405+ e. to_string( )
3406+ ) ;
34113407
3412- String :: from_utf8_lossy ( context) . to_string ( )
3413- } ) ;
3408+ String :: from_utf8_lossy ( context) . to_string ( )
3409+ } ) ;
34143410
3415- return Cow :: Owned ( res) ;
3416- }
3411+ return Cow :: Owned ( res) ;
3412+ }
3413+ }
3414+ }
3415+
3416+ #[ cfg( all( feature = "smack" , target_os = "linux" ) ) ]
3417+ if config. smack_supported {
3418+ // For SMACK, use the path to get the label
3419+ // If must_dereference is true, we follow the symlink
3420+ let target_path = if must_dereference {
3421+ match std:: fs:: canonicalize ( path) {
3422+ Ok ( p) => p,
3423+ Err ( _) => path. to_path_buf ( ) ,
3424+ }
3425+ } else {
3426+ path. to_path_buf ( )
3427+ } ;
3428+
3429+ match uucore:: smack:: get_smack_label_for_path ( & target_path) {
3430+ Ok ( label) => return Cow :: Owned ( label) ,
3431+ Err ( _) => {
3432+ // No label or error getting label
3433+ return Cow :: Borrowed ( SUBSTITUTE_STRING ) ;
34173434 }
34183435 }
34193436 }
0 commit comments