From 42782d6053c25c4337a20b7573bac6cacfe78f17 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Thu, 17 Apr 2025 10:18:31 +0200 Subject: [PATCH] selinux: rename check_selinux_enabled() to is_selinux_enabled() and change return type to bool --- src/uu/mkdir/src/mkdir.rs | 2 +- src/uu/stat/src/stat.rs | 2 +- src/uucore/src/lib/features/selinux.rs | 39 +++++++------------------- 3 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/uu/mkdir/src/mkdir.rs b/src/uu/mkdir/src/mkdir.rs index 14c7701db..958d3b6f8 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -294,7 +294,7 @@ fn create_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<()> { // Apply SELinux context if requested #[cfg(feature = "selinux")] - if config.set_selinux_context && uucore::selinux::check_selinux_enabled().is_ok() { + if config.set_selinux_context && uucore::selinux::is_selinux_enabled() { if let Err(e) = uucore::selinux::set_selinux_security_context(path, config.context) { let _ = std::fs::remove_dir(path); diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 6a4a343e4..16a96c380 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -906,7 +906,7 @@ impl Stater { 'C' => { #[cfg(feature = "selinux")] { - if uucore::selinux::check_selinux_enabled().is_ok() { + if uucore::selinux::is_selinux_enabled() { match uucore::selinux::get_selinux_security_context(Path::new(file)) { Ok(ctx) => OutputType::Str(ctx), diff --git a/src/uucore/src/lib/features/selinux.rs b/src/uucore/src/lib/features/selinux.rs index 2f2d2b6a1..6686b98d4 100644 --- a/src/uucore/src/lib/features/selinux.rs +++ b/src/uucore/src/lib/features/selinux.rs @@ -29,28 +29,8 @@ impl From for i32 { /// Checks if SELinux is enabled on the system. /// /// This function verifies whether the kernel has SELinux support enabled. -/// -/// # Returns -/// -/// * `Ok(())` - If SELinux is enabled on the system. -/// * `Err(Error::SELinuxNotEnabled)` - If SELinux is not enabled. -/// -/// # Examples -/// -/// ``` -/// use uucore::selinux::check_selinux_enabled; -/// -/// match check_selinux_enabled() { -/// Ok(_) => println!("SELinux is enabled"), -/// Err(_) => println!("SELinux is not enabled"), -/// } -/// ``` -pub fn check_selinux_enabled() -> Result<(), Error> { - if selinux::kernel_support() == selinux::KernelSupport::Unsupported { - Err(Error::SELinuxNotEnabled) - } else { - Ok(()) - } +pub fn is_selinux_enabled() -> bool { + selinux::kernel_support() != selinux::KernelSupport::Unsupported } /// Sets the SELinux security context for the given filesystem path. @@ -97,8 +77,9 @@ pub fn check_selinux_enabled() -> Result<(), Error> { /// } /// ``` pub fn set_selinux_security_context(path: &Path, context: Option<&String>) -> Result<(), String> { - // Check if SELinux is enabled on the system - check_selinux_enabled().map_err(|e| format!("{:?}", e))?; + if !is_selinux_enabled() { + return Err("SELinux is not enabled on this system".to_string()); + } if let Some(ctx_str) = context { // Create a CString from the provided context string @@ -165,7 +146,7 @@ pub fn set_selinux_security_context(path: &Path, context: Option<&String>) -> Re /// ``` pub fn get_selinux_security_context(path: &Path) -> Result { - if selinux::kernel_support() == selinux::KernelSupport::Unsupported { + if !is_selinux_enabled() { return Err(Error::SELinuxNotEnabled); } @@ -240,15 +221,15 @@ mod tests { } #[test] - fn test_check_selinux_enabled_runtime_behavior() { - let result = check_selinux_enabled(); + fn test_is_selinux_enabled_runtime_behavior() { + let result = is_selinux_enabled(); match selinux::kernel_support() { selinux::KernelSupport::Unsupported => { - assert!(matches!(result, Err(Error::SELinuxNotEnabled))); + assert!(!result, "Expected false when SELinux is not supported"); } _ => { - assert!(result.is_ok(), "Expected Ok(()) when SELinux is supported"); + assert!(result, "Expected true when SELinux is supported"); } } }