@@ -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/// ```
9979pub 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
167148pub 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