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

Merge pull request #7771 from cakebaker/selinux_fix_check_selinux_enabled

selinux: rename `check_selinux_enabled()`
This commit is contained in:
Sylvestre Ledru 2025-04-17 10:58:55 +02:00 committed by GitHub
commit 8487e12057
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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
#[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);

View file

@ -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),

View file

@ -29,28 +29,8 @@ impl From<Error> 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<String, Error> {
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");
}
}
}