Merge pull request #2273 from ycd/mv-perm-error

mv: log proper error message when permission error occurs
This commit is contained in:
Sylvestre Ledru 2021-05-25 21:44:04 +02:00 committed by GitHub
commit cdd74afa3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View file

@ -295,7 +295,7 @@ fn exec(files: &[PathBuf], b: Behavior) -> i32 {
"cannot move {} to {}: {}",
source.display(),
target.display(),
e
e.to_string()
);
1
}
@ -358,14 +358,15 @@ fn move_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> i3
if let Err(e) = rename(sourcepath, &targetpath, b) {
show_error!(
"mv: cannot move {} to {}: {}",
"cannot move {} to {}: {}",
sourcepath.display(),
targetpath.display(),
e
e.to_string()
);
all_successful = false;
}
}
if all_successful {
0
} else {
@ -452,7 +453,13 @@ fn rename_with_fallback(from: &Path, to: &Path) -> io::Result<()> {
..DirCopyOptions::new()
};
if let Err(err) = move_dir(from, to, &options) {
return Err(io::Error::new(io::ErrorKind::Other, format!("{:?}", err)));
return match err.kind {
fs_extra::error::ErrorKind::PermissionDenied => Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"Permission denied",
)),
_ => Err(io::Error::new(io::ErrorKind::Other, format!("{:?}", err))),
};
}
} else {
fs::copy(from, to).and_then(|_| fs::remove_file(from))?;

View file

@ -587,6 +587,24 @@ fn test_mv_verbose() {
));
}
#[test]
fn test_mv_permission_error() {
let scene = TestScenario::new("mkdir");
let folder1 = "bar";
let folder2 = "foo";
let folder_to_move = "bar/foo";
scene.ucmd().arg("-m444").arg(folder1).succeeds();
scene.ucmd().arg("-m777").arg(folder2).succeeds();
scene
.cmd_keepenv(util_name!())
.arg(folder2)
.arg(folder_to_move)
.run()
.stderr_str()
.ends_with("Permission denied");
}
// Todo:
// $ at.touch a b