Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mv: validate --target arg #4802

Merged
merged 1 commit into from
Apr 29, 2023
Merged
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
21 changes: 12 additions & 9 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

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 {
overwrite: overwrite_mode,
backup: backup_mode,
suffix: backup_suffix,
update: matches.get_flag(OPT_UPDATE),
target_dir: matches
.get_one::<OsString>(OPT_TARGET_DIRECTORY)
.map(OsString::from),
target_dir,
no_target_dir: matches.get_flag(OPT_NO_TARGET_DIRECTORY),
verbose: matches.get_flag(OPT_VERBOSE),
strip_slashes: matches.get_flag(OPT_STRIP_TRAILING_SLASHES),
Expand Down Expand Up @@ -320,12 +328,7 @@ fn exec(files: &[OsString], b: &Behavior) -> UResult<()> {

fn move_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UResult<()> {
if !target_dir.is_dir() {
match b.target_dir {
Some(_) => {
return Err(MvError::TargetNotADirectory(target_dir.quote().to_string()).into())
}
None => return Err(MvError::NotADirectory(target_dir.quote().to_string()).into()),
}
return Err(MvError::NotADirectory(target_dir.quote().to_string()).into());
}

let canonized_target_dir = target_dir
Expand Down