1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

selinux: rename check_selinux_enabled()

to is_selinux_enabled() and change return type to bool
This commit is contained in:
Daniel Hofstetter 2025-04-17 10:18:31 +02:00
parent 349e56897c
commit 42782d6053
3 changed files with 12 additions and 31 deletions

View file

@ -294,7 +294,7 @@ fn create_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<()> {
// Apply SELinux context if requested // Apply SELinux context if requested
#[cfg(feature = "selinux")] #[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) if let Err(e) = uucore::selinux::set_selinux_security_context(path, config.context)
{ {
let _ = std::fs::remove_dir(path); let _ = std::fs::remove_dir(path);

View file

@ -906,7 +906,7 @@ impl Stater {
'C' => { 'C' => {
#[cfg(feature = "selinux")] #[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)) match uucore::selinux::get_selinux_security_context(Path::new(file))
{ {
Ok(ctx) => OutputType::Str(ctx), Ok(ctx) => OutputType::Str(ctx),

View file

@ -29,28 +29,8 @@ impl From<Error> for i32 {
/// Checks if SELinux is enabled on the system. /// Checks if SELinux is enabled on the system.
/// ///
/// This function verifies whether the kernel has SELinux support enabled. /// This function verifies whether the kernel has SELinux support enabled.
/// pub fn is_selinux_enabled() -> bool {
/// # Returns selinux::kernel_support() != selinux::KernelSupport::Unsupported
///
/// * `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(())
}
} }
/// Sets the SELinux security context for the given filesystem path. /// 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> { pub fn set_selinux_security_context(path: &Path, context: Option<&String>) -> Result<(), String> {
// Check if SELinux is enabled on the system if !is_selinux_enabled() {
check_selinux_enabled().map_err(|e| format!("{:?}", e))?; return Err("SELinux is not enabled on this system".to_string());
}
if let Some(ctx_str) = context { if let Some(ctx_str) = context {
// Create a CString from the provided context string // 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<String, Error> { pub fn get_selinux_security_context(path: &Path) -> Result<String, Error> {
if selinux::kernel_support() == selinux::KernelSupport::Unsupported { if !is_selinux_enabled() {
return Err(Error::SELinuxNotEnabled); return Err(Error::SELinuxNotEnabled);
} }
@ -240,15 +221,15 @@ mod tests {
} }
#[test] #[test]
fn test_check_selinux_enabled_runtime_behavior() { fn test_is_selinux_enabled_runtime_behavior() {
let result = check_selinux_enabled(); let result = is_selinux_enabled();
match selinux::kernel_support() { match selinux::kernel_support() {
selinux::KernelSupport::Unsupported => { 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");
} }
} }
} }