mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 12:18:07 +02:00
Fix: dst error on cp command (#7895)
Fixes #7693 On `cp` commands there were two error which pass error message with invalid detail about source and destination files . there error were for Not exist file and Permission denied . Examples: Before : Copy `source_file_valid` to `destination_invalid_dir` throw this error ; `copy file "/source_file_valid" failed: No such file or directory (os error 2) ` After this PR it will throw this if destination will be invalid : `copying to destination "/destination_invalid_dir" failed: No such file or directory (os error 2) ` it was for Permission denied too . --------- Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
4db960c0a6
commit
c130ca1bc6
@ -351,17 +351,38 @@ fn copy_file(src: PathBuf, dst: PathBuf, span: Span) -> Value {
|
||||
Value::String { val: msg, span }
|
||||
}
|
||||
Err(e) => {
|
||||
let message = format!("copy file {src:?} failed: {e}");
|
||||
|
||||
let message_src = format!(
|
||||
"copying file '{src_display}' failed: {e}",
|
||||
src_display = src.display()
|
||||
);
|
||||
let message_dst = format!(
|
||||
"copying to destination '{dst_display}' failed: {e}",
|
||||
dst_display = dst.display()
|
||||
);
|
||||
use std::io::ErrorKind;
|
||||
let shell_error = match e.kind() {
|
||||
ErrorKind::NotFound => ShellError::FileNotFoundCustom(message, span),
|
||||
ErrorKind::PermissionDenied => ShellError::PermissionDeniedError(message, span),
|
||||
ErrorKind::Interrupted => ShellError::IOInterrupted(message, span),
|
||||
ErrorKind::OutOfMemory => ShellError::OutOfMemoryError(message, span),
|
||||
ErrorKind::NotFound => {
|
||||
if std::path::Path::new(&dst).exists() {
|
||||
ShellError::FileNotFoundCustom(message_src, span)
|
||||
} else {
|
||||
ShellError::FileNotFoundCustom(message_dst, span)
|
||||
}
|
||||
}
|
||||
ErrorKind::PermissionDenied => match std::fs::metadata(&dst) {
|
||||
Ok(meta) => {
|
||||
if meta.permissions().readonly() {
|
||||
ShellError::PermissionDeniedError(message_dst, span)
|
||||
} else {
|
||||
ShellError::PermissionDeniedError(message_src, span)
|
||||
}
|
||||
}
|
||||
Err(_) => ShellError::PermissionDeniedError(message_dst, span),
|
||||
},
|
||||
ErrorKind::Interrupted => ShellError::IOInterrupted(message_src, span),
|
||||
ErrorKind::OutOfMemory => ShellError::OutOfMemoryError(message_src, span),
|
||||
// TODO: handle ExecutableFileBusy etc. when io_error_more is stabilized
|
||||
// https://github.com/rust-lang/rust/issues/86442
|
||||
_ => ShellError::IOErrorSpanned(message, span),
|
||||
_ => ShellError::IOErrorSpanned(message_src, span),
|
||||
};
|
||||
|
||||
Value::Error { error: shell_error }
|
||||
|
Reference in New Issue
Block a user