Skip to content

Commit 515b39a

Browse files
cerdelensylvestre
authored andcommitted
Rm no abbreviation no preserve root (uutils#10205)
--------- Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent 98336cf commit 515b39a

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

src/uu/rm/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ rm-error-dangerous-recursive-operation = it is dangerous to operate recursively
6262
rm-error-use-no-preserve-root = use --no-preserve-root to override this failsafe
6363
rm-error-refusing-to-remove-directory = refusing to remove '.' or '..' directory: skipping {$path}
6464
rm-error-cannot-remove = cannot remove {$file}
65+
rm-error-may-not-abbreviate-no-preserve-root = you may not abbreviate the --no-preserve-root option
6566
6667
# Verbose messages
6768
rm-verbose-removed = removed {$file}

src/uu/rm/locales/fr-FR.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ rm-error-dangerous-recursive-operation = il est dangereux d'opérer récursiveme
6262
rm-error-use-no-preserve-root = utilisez --no-preserve-root pour outrepasser cette protection
6363
rm-error-refusing-to-remove-directory = refus de supprimer le répertoire '.' ou '..' : ignorer {$path}
6464
rm-error-cannot-remove = impossible de supprimer {$file}
65+
rm-error-may-not-abbreviate-no-preserve-root = Vous ne pouvez pas abréger l'option --no-preserve-root
6566
6667
# Messages verbeux
6768
rm-verbose-removed = {$file} supprimé

src/uu/rm/src/rm.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ enum RmError {
4545
UseNoPreserveRoot,
4646
#[error("{}", translate!("rm-error-refusing-to-remove-directory", "path" => _0.quote()))]
4747
RefusingToRemoveDirectory(OsString),
48+
#[error("{}", translate!("rm-error-may-not-abbreviate-no-preserve-root"))]
49+
MayNotAbbreviateNoPreserveRoot,
4850
}
4951

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

201203
#[uucore::main]
202204
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
203-
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
205+
let args: Vec<OsString> = args.collect();
206+
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args.iter())?;
204207

205208
let files: Vec<_> = matches
206209
.get_many::<OsString>(ARG_FILES)
@@ -253,6 +256,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
253256
None
254257
},
255258
};
259+
260+
// manually parse all args to verify --no-preserve-root did not get abbreviated (clap does
261+
// allow this)
262+
if !options.preserve_root && !args.iter().any(|arg| arg == "--no-preserve-root") {
263+
return Err(RmError::MayNotAbbreviateNoPreserveRoot.into());
264+
}
265+
256266
if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) {
257267
let prompt_key = if options.recursive {
258268
"rm-prompt-remove-arguments-recursive"

tests/by-util/test_rm.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,24 @@ fn test_progress_no_output_on_error() {
12181218
.stderr_contains("No such file or directory");
12191219
}
12201220

1221+
#[test]
1222+
fn no_preserve_root_may_not_be_abbreviated() {
1223+
let (at, _ucmd) = at_and_ucmd!();
1224+
let file = "test_file_123";
1225+
1226+
at.touch(file);
1227+
1228+
for arg in ["--n", "--no-pre", "--no-preserve-ro"] {
1229+
new_ucmd!()
1230+
.arg(arg)
1231+
.arg(file)
1232+
.fails()
1233+
.stderr_contains("you may not abbreviate the --no-preserve-root option");
1234+
}
1235+
1236+
assert!(at.file_exists(file));
1237+
}
1238+
12211239
#[cfg(unix)]
12221240
#[test]
12231241
fn test_symlink_to_readonly_no_prompt() {

0 commit comments

Comments
 (0)