Skip to content

Commit 8487e12

Browse files
authored
Merge pull request #7771 from cakebaker/selinux_fix_check_selinux_enabled
selinux: rename `check_selinux_enabled()`
2 parents 349e568 + 42782d6 commit 8487e12

File tree

3 files changed

+12
-31
lines changed

3 files changed

+12
-31
lines changed

src/uu/mkdir/src/mkdir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ fn create_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<()> {
294294

295295
// Apply SELinux context if requested
296296
#[cfg(feature = "selinux")]
297-
if config.set_selinux_context && uucore::selinux::check_selinux_enabled().is_ok() {
297+
if config.set_selinux_context && uucore::selinux::is_selinux_enabled() {
298298
if let Err(e) = uucore::selinux::set_selinux_security_context(path, config.context)
299299
{
300300
let _ = std::fs::remove_dir(path);

src/uu/stat/src/stat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ impl Stater {
906906
'C' => {
907907
#[cfg(feature = "selinux")]
908908
{
909-
if uucore::selinux::check_selinux_enabled().is_ok() {
909+
if uucore::selinux::is_selinux_enabled() {
910910
match uucore::selinux::get_selinux_security_context(Path::new(file))
911911
{
912912
Ok(ctx) => OutputType::Str(ctx),

src/uucore/src/lib/features/selinux.rs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,8 @@ impl From<Error> for i32 {
2929
/// Checks if SELinux is enabled on the system.
3030
///
3131
/// This function verifies whether the kernel has SELinux support enabled.
32-
///
33-
/// # Returns
34-
///
35-
/// * `Ok(())` - If SELinux is enabled on the system.
36-
/// * `Err(Error::SELinuxNotEnabled)` - If SELinux is not enabled.
37-
///
38-
/// # Examples
39-
///
40-
/// ```
41-
/// use uucore::selinux::check_selinux_enabled;
42-
///
43-
/// match check_selinux_enabled() {
44-
/// Ok(_) => println!("SELinux is enabled"),
45-
/// Err(_) => println!("SELinux is not enabled"),
46-
/// }
47-
/// ```
48-
pub fn check_selinux_enabled() -> Result<(), Error> {
49-
if selinux::kernel_support() == selinux::KernelSupport::Unsupported {
50-
Err(Error::SELinuxNotEnabled)
51-
} else {
52-
Ok(())
53-
}
32+
pub fn is_selinux_enabled() -> bool {
33+
selinux::kernel_support() != selinux::KernelSupport::Unsupported
5434
}
5535

5636
/// Sets the SELinux security context for the given filesystem path.
@@ -97,8 +77,9 @@ pub fn check_selinux_enabled() -> Result<(), Error> {
9777
/// }
9878
/// ```
9979
pub fn set_selinux_security_context(path: &Path, context: Option<&String>) -> Result<(), String> {
100-
// Check if SELinux is enabled on the system
101-
check_selinux_enabled().map_err(|e| format!("{:?}", e))?;
80+
if !is_selinux_enabled() {
81+
return Err("SELinux is not enabled on this system".to_string());
82+
}
10283

10384
if let Some(ctx_str) = context {
10485
// Create a CString from the provided context string
@@ -165,7 +146,7 @@ pub fn set_selinux_security_context(path: &Path, context: Option<&String>) -> Re
165146
/// ```
166147
167148
pub fn get_selinux_security_context(path: &Path) -> Result<String, Error> {
168-
if selinux::kernel_support() == selinux::KernelSupport::Unsupported {
149+
if !is_selinux_enabled() {
169150
return Err(Error::SELinuxNotEnabled);
170151
}
171152

@@ -240,15 +221,15 @@ mod tests {
240221
}
241222

242223
#[test]
243-
fn test_check_selinux_enabled_runtime_behavior() {
244-
let result = check_selinux_enabled();
224+
fn test_is_selinux_enabled_runtime_behavior() {
225+
let result = is_selinux_enabled();
245226

246227
match selinux::kernel_support() {
247228
selinux::KernelSupport::Unsupported => {
248-
assert!(matches!(result, Err(Error::SELinuxNotEnabled)));
229+
assert!(!result, "Expected false when SELinux is not supported");
249230
}
250231
_ => {
251-
assert!(result.is_ok(), "Expected Ok(()) when SELinux is supported");
232+
assert!(result, "Expected true when SELinux is supported");
252233
}
253234
}
254235
}

0 commit comments

Comments
 (0)