Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/uu/rm/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ rm-error-dangerous-recursive-operation = it is dangerous to operate recursively
rm-error-use-no-preserve-root = use --no-preserve-root to override this failsafe
rm-error-refusing-to-remove-directory = refusing to remove '.' or '..' directory: skipping {$path}
rm-error-cannot-remove = cannot remove {$file}
rm-error-may-not-abbreviate-no-preserve-root = you may not abbreviate the --no-preserve-root option

# Verbose messages
rm-verbose-removed = removed {$file}
Expand Down
1 change: 1 addition & 0 deletions src/uu/rm/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ rm-error-dangerous-recursive-operation = il est dangereux d'opérer récursiveme
rm-error-use-no-preserve-root = utilisez --no-preserve-root pour outrepasser cette protection
rm-error-refusing-to-remove-directory = refus de supprimer le répertoire '.' ou '..' : ignorer {$path}
rm-error-cannot-remove = impossible de supprimer {$file}
rm-error-may-not-abbreviate-no-preserve-root = Vous ne pouvez pas abréger l'option --no-preserve-root

# Messages verbeux
rm-verbose-removed = {$file} supprimé
Expand Down
12 changes: 11 additions & 1 deletion src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ enum RmError {
UseNoPreserveRoot,
#[error("{}", translate!("rm-error-refusing-to-remove-directory", "path" => _0.quote()))]
RefusingToRemoveDirectory(OsString),
#[error("{}", translate!("rm-error-may-not-abbreviate-no-preserve-root"))]
MayNotAbbreviateNoPreserveRoot,
}

impl UError for RmError {}
Expand Down Expand Up @@ -200,7 +202,8 @@ static ARG_FILES: &str = "files";

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
let args = args.collect_ignore();
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args.iter())?;

let files: Vec<_> = matches
.get_many::<OsString>(ARG_FILES)
Expand Down Expand Up @@ -253,6 +256,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
None
},
};

// manually parse all args to verify --no-preserve-root did not get abbreviated (clap does
// allow this)
if !options.preserve_root && !args.iter().any(|arg| arg == "--no-preserve-root") {
return Err(RmError::MayNotAbbreviateNoPreserveRoot.into());
}

if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) {
let msg: String = format!(
"remove {} {}{}",
Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,3 +1217,31 @@ fn test_progress_no_output_on_error() {
.stderr_contains("cannot remove")
.stderr_contains("No such file or directory");
}

#[test]
fn no_preserve_root_may_not_be_abbreviated() {
let (at, _ucmd) = at_and_ucmd!();
let file = "test_file_123";

at.touch(file);

new_ucmd!()
.arg("--n")
.arg(file)
.fails()
.stderr_contains("you may not abbreviate the --no-preserve-root option");

new_ucmd!()
.arg("--no-pre")
.arg(file)
.fails()
.stderr_contains("you may not abbreviate the --no-preserve-root option");

new_ucmd!()
.arg("--no-preserve-ro")
.arg(file)
.fails()
.stderr_contains("you may not abbreviate the --no-preserve-root option");

assert!(at.file_exists(file));
}
Loading