Fix "mv allows moving a directory into itself" (#2619)

* make sort-by fail gracefully if mismatched types are compared

* Added a test to check if sorted-by with invalid types exists gracefully

* Linter changes

* removed redundant pattern matching

* Changed the error message

* Added a comma after every argument

* Changed the test to accomodate the new err messages

* Err message for sort-by invalid types now shows the mismatched types

* Lints problems

* Changed unwrap to expect

* Added the -f flag to rm command

Now when you a use rm -f there will be no error message, even if the
file doesnt actually exist

* Lint problems

* Fixed the wrong line

* Removed println

* Spelling mistake

* Fix problems when you mv a file into itself

* Lint mistakes

* Remove unecessary filtering in most cases
This commit is contained in:
Luccas Mateus
2020-09-30 22:01:05 -03:00
committed by GitHub
parent b7bc4c1f80
commit 66061192f8
2 changed files with 52 additions and 1 deletions

View File

@ -446,7 +446,7 @@ impl Shell for FilesystemShell {
let source = path.join(&src.item);
let destination = path.join(&dst.item);
let sources =
let mut sources =
glob::glob(&source.to_string_lossy()).map_or_else(|_| Vec::new(), Iterator::collect);
if sources.is_empty() {
@ -477,6 +477,29 @@ impl Shell for FilesystemShell {
));
}
let some_if_source_is_destination = sources
.iter()
.find(|f| matches!(f, Ok(f) if destination.starts_with(f)));
if destination.exists() && destination.is_dir() && sources.len() == 1 {
if let Some(Ok(filename)) = some_if_source_is_destination {
return Err(ShellError::labeled_error(
format!(
"Not possible to move {:?} to itself",
filename.file_name().expect("Invalid file name")
),
"cannot move to itself",
dst.tag,
));
}
}
if let Some(Ok(_filename)) = some_if_source_is_destination {
sources = sources
.into_iter()
.filter(|f| matches!(f, Ok(f) if !destination.starts_with(f)))
.collect();
}
for entry in sources {
if let Ok(entry) = entry {
move_file(