From 0790a714b0cf4ff6cf6a44571d59454484e90267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Tue, 23 Jul 2019 00:51:22 -0500 Subject: [PATCH] Appropiate error handling when copying (thanks @jonathandturner) --- src/commands/cp.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/commands/cp.rs b/src/commands/cp.rs index 7e33784342..d0fc343d95 100644 --- a/src/commands/cp.rs +++ b/src/commands/cp.rs @@ -32,20 +32,18 @@ impl Command for Copycp { } pub fn cp(args: CommandArgs) -> Result { - let mut source = args.env.lock().unwrap().path().to_path_buf(); + let mut source = args.env.lock().unwrap().path().to_path_buf(); let mut destination = args.env.lock().unwrap().path().to_path_buf(); - let mut src = String::new(); let mut dst = String::new(); match args .nth(0) .ok_or_else(|| ShellError::string(&format!("No file or directory specified")))? .as_string()? - .as_str() { - - file => { - src.push_str(file); + .as_str() + { + file => { source.push(file); } } @@ -54,12 +52,12 @@ pub fn cp(args: CommandArgs) -> Result { .nth(1) .ok_or_else(|| ShellError::string(&format!("No file or directory specified")))? .as_string()? - .as_str() { - - file => { + .as_str() + { + file => { dst.push_str(file); destination.push(file); - } + } } if destination.is_dir() { @@ -68,13 +66,15 @@ pub fn cp(args: CommandArgs) -> Result { let file_name = file_name.to_str().expect(""); destination.push(Path::new(file_name)); } else if source.is_dir() { - return Err(ShellError::string( - &format!("{:?} is a directory (not copied)", src)) - ); + return Err(ShellError::string(&format!( + "{:?} is a directory (not copied)", + source.to_string_lossy() + ))); } } - std::fs::copy(source, destination).expect("can not copy file"); - - Ok(OutputStream::empty()) -} \ No newline at end of file + match std::fs::copy(source, destination) { + Err(_error) => Err(ShellError::string("can not copy file")), + Ok(_) => Ok(OutputStream::empty()), + } +}