Skip to content

Commit 557f1be

Browse files
committed
smack: refactor to single set_smack_label_and_cleanup function
1 parent 6e588fe commit 557f1be

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

src/uu/mkdir/src/mkdir.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<(
303303
// Apply SMACK context if requested
304304
#[cfg(feature = "smack")]
305305
if config.set_security_context {
306-
uucore::smack::set_smack_label_for_new_dir(path, config.context)?;
306+
uucore::smack::set_smack_label_and_cleanup(path, config.context, |p| {
307+
std::fs::remove_dir(p)
308+
})?;
307309
}
308310
Ok(())
309311
}

src/uu/mkfifo/src/mkfifo.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8282
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
8383
let context = matches.get_one::<String>(options::CONTEXT);
8484
if set_security_context || context.is_some() {
85-
uucore::smack::set_smack_label_for_new_file(&f, context)?;
85+
uucore::smack::set_smack_label_and_cleanup(&f, context, |p| {
86+
std::fs::remove_file(p)
87+
})?;
8688
}
8789
}
8890
}

src/uu/mknod/src/mknod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ fn mknod(file_name: &str, config: Config) -> i32 {
103103
// Apply SMACK context if requested
104104
#[cfg(feature = "smack")]
105105
if config.set_security_context {
106-
if let Err(e) = uucore::smack::set_smack_label_for_new_file(file_name, config.context) {
106+
if let Err(e) =
107+
uucore::smack::set_smack_label_and_cleanup(file_name, config.context, |p| {
108+
std::fs::remove_file(p)
109+
})
110+
{
107111
eprintln!("{}: {}", uucore::util_name(), e);
108112
return 1;
109113
}

src/uucore/src/lib/features/smack.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,34 +103,19 @@ pub fn set_smack_label_for_path(path: &Path, label: &str) -> Result<(), SmackErr
103103
.map_err(|e| SmackError::LabelSetFailure(label.to_string(), e))
104104
}
105105

106-
/// Sets SMACK label for a file, removing it on failure.
107-
pub fn set_smack_label_for_new_file(
106+
/// Sets SMACK label for a new path, calling cleanup on failure.
107+
pub fn set_smack_label_and_cleanup(
108108
path: impl AsRef<Path>,
109109
context: Option<&String>,
110+
cleanup: impl FnOnce(&Path) -> io::Result<()>,
110111
) -> Result<(), Box<dyn UError>> {
111112
let Some(ctx) = context else { return Ok(()) };
112113
if !is_smack_enabled() {
113114
return Ok(());
114115
}
115116
let path = path.as_ref();
116117
set_smack_label_for_path(path, ctx).map_err(|e| {
117-
let _ = fs::remove_file(path);
118-
USimpleError::new(1, e.to_string())
119-
})
120-
}
121-
122-
/// Sets SMACK label for a directory, removing it on failure.
123-
pub fn set_smack_label_for_new_dir(
124-
path: impl AsRef<Path>,
125-
context: Option<&String>,
126-
) -> Result<(), Box<dyn UError>> {
127-
let Some(ctx) = context else { return Ok(()) };
128-
if !is_smack_enabled() {
129-
return Ok(());
130-
}
131-
let path = path.as_ref();
132-
set_smack_label_for_path(path, ctx).map_err(|e| {
133-
let _ = fs::remove_dir(path);
118+
let _ = cleanup(path);
134119
USimpleError::new(1, e.to_string())
135120
})
136121
}

0 commit comments

Comments
 (0)