mv: validate --target arg

This commit is contained in:
Daniel Hofstetter 2023-04-29 16:16:00 +02:00
parent c5fb6eac9f
commit 7cfdbe676c

View file

@ -106,14 +106,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let backup_suffix = backup_control::determine_backup_suffix(&matches); let backup_suffix = backup_control::determine_backup_suffix(&matches);
let target_dir = matches
.get_one::<OsString>(OPT_TARGET_DIRECTORY)
.map(OsString::from);
if let Some(ref maybe_dir) = target_dir {
if !Path::new(&maybe_dir).is_dir() {
return Err(MvError::TargetNotADirectory(maybe_dir.quote().to_string()).into());
}
}
let behavior = Behavior { let behavior = Behavior {
overwrite: overwrite_mode, overwrite: overwrite_mode,
backup: backup_mode, backup: backup_mode,
suffix: backup_suffix, suffix: backup_suffix,
update: matches.get_flag(OPT_UPDATE), update: matches.get_flag(OPT_UPDATE),
target_dir: matches target_dir,
.get_one::<OsString>(OPT_TARGET_DIRECTORY)
.map(OsString::from),
no_target_dir: matches.get_flag(OPT_NO_TARGET_DIRECTORY), no_target_dir: matches.get_flag(OPT_NO_TARGET_DIRECTORY),
verbose: matches.get_flag(OPT_VERBOSE), verbose: matches.get_flag(OPT_VERBOSE),
strip_slashes: matches.get_flag(OPT_STRIP_TRAILING_SLASHES), strip_slashes: matches.get_flag(OPT_STRIP_TRAILING_SLASHES),
@ -320,12 +328,7 @@ fn exec(files: &[OsString], b: &Behavior) -> UResult<()> {
fn move_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UResult<()> { fn move_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UResult<()> {
if !target_dir.is_dir() { if !target_dir.is_dir() {
match b.target_dir { return Err(MvError::NotADirectory(target_dir.quote().to_string()).into());
Some(_) => {
return Err(MvError::TargetNotADirectory(target_dir.quote().to_string()).into())
}
None => return Err(MvError::NotADirectory(target_dir.quote().to_string()).into()),
}
} }
let canonized_target_dir = target_dir let canonized_target_dir = target_dir