smack: refactor to single set_smack_label_and_cleanup function

This commit is contained in:
Christopher Dryden
2026-01-06 18:39:44 +00:00
parent 6e588fed34
commit 557f1befb4
4 changed files with 15 additions and 22 deletions
+3 -1
View File
@@ -303,7 +303,9 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<(
// Apply SMACK context if requested
#[cfg(feature = "smack")]
if config.set_security_context {
uucore::smack::set_smack_label_for_new_dir(path, config.context)?;
uucore::smack::set_smack_label_and_cleanup(path, config.context, |p| {
std::fs::remove_dir(p)
})?;
}
Ok(())
}
+3 -1
View File
@@ -82,7 +82,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
let context = matches.get_one::<String>(options::CONTEXT);
if set_security_context || context.is_some() {
uucore::smack::set_smack_label_for_new_file(&f, context)?;
uucore::smack::set_smack_label_and_cleanup(&f, context, |p| {
std::fs::remove_file(p)
})?;
}
}
}
+5 -1
View File
@@ -103,7 +103,11 @@ fn mknod(file_name: &str, config: Config) -> i32 {
// Apply SMACK context if requested
#[cfg(feature = "smack")]
if config.set_security_context {
if let Err(e) = uucore::smack::set_smack_label_for_new_file(file_name, config.context) {
if let Err(e) =
uucore::smack::set_smack_label_and_cleanup(file_name, config.context, |p| {
std::fs::remove_file(p)
})
{
eprintln!("{}: {}", uucore::util_name(), e);
return 1;
}
+4 -19
View File
@@ -103,10 +103,11 @@ pub fn set_smack_label_for_path(path: &Path, label: &str) -> Result<(), SmackErr
.map_err(|e| SmackError::LabelSetFailure(label.to_string(), e))
}
/// Sets SMACK label for a file, removing it on failure.
pub fn set_smack_label_for_new_file(
/// Sets SMACK label for a new path, calling cleanup on failure.
pub fn set_smack_label_and_cleanup(
path: impl AsRef<Path>,
context: Option<&String>,
cleanup: impl FnOnce(&Path) -> io::Result<()>,
) -> Result<(), Box<dyn UError>> {
let Some(ctx) = context else { return Ok(()) };
if !is_smack_enabled() {
@@ -114,23 +115,7 @@ pub fn set_smack_label_for_new_file(
}
let path = path.as_ref();
set_smack_label_for_path(path, ctx).map_err(|e| {
let _ = fs::remove_file(path);
USimpleError::new(1, e.to_string())
})
}
/// Sets SMACK label for a directory, removing it on failure.
pub fn set_smack_label_for_new_dir(
path: impl AsRef<Path>,
context: Option<&String>,
) -> Result<(), Box<dyn UError>> {
let Some(ctx) = context else { return Ok(()) };
if !is_smack_enabled() {
return Ok(());
}
let path = path.as_ref();
set_smack_label_for_path(path, ctx).map_err(|e| {
let _ = fs::remove_dir(path);
let _ = cleanup(path);
USimpleError::new(1, e.to_string())
})
}