diff --git a/src/commands/cp.rs b/src/commands/cp.rs index a6ba7ecd0..5e1ac6d26 100644 --- a/src/commands/cp.rs +++ b/src/commands/cp.rs @@ -79,15 +79,19 @@ fn cp( if entry.is_file() { let strategy = |(source_file, _depth_level)| { if destination.exists() { - let mut new_dst = dunce::canonicalize(destination.clone()).unwrap(); - new_dst.push(entry.file_name().unwrap()); - (source_file, new_dst) + let mut new_dst = dunce::canonicalize(destination.clone())?; + if let Some(name) = entry.file_name() { + new_dst.push(name); + } + Ok((source_file, new_dst)) } else { - (source_file, destination.clone()) + Ok((source_file, destination.clone())) } }; - for (ref src, ref dst) in sources.paths_applying_with(strategy) { + let sources = sources.paths_applying_with(strategy)?; + + for (ref src, ref dst) in sources { if src.is_file() { match std::fs::copy(src, dst) { Err(e) => { @@ -118,7 +122,7 @@ fn cp( let strategy = |(source_file, depth_level)| { let mut new_dst = destination.clone(); - let path = dunce::canonicalize(&source_file).unwrap(); + let path = dunce::canonicalize(&source_file)?; let mut comps: Vec<_> = path .components() @@ -133,10 +137,12 @@ fn cp( new_dst.push(fragment); } - (PathBuf::from(&source_file), PathBuf::from(new_dst)) + Ok((PathBuf::from(&source_file), PathBuf::from(new_dst))) }; - for (ref src, ref dst) in sources.paths_applying_with(strategy) { + let sources = sources.paths_applying_with(strategy)?; + + for (ref src, ref dst) in sources { if src.is_dir() { if !dst.exists() { match std::fs::create_dir_all(dst) { @@ -189,8 +195,8 @@ fn cp( }; let strategy = |(source_file, depth_level)| { - let mut new_dst = dunce::canonicalize(&destination).unwrap(); - let path = dunce::canonicalize(&source_file).unwrap(); + let mut new_dst = dunce::canonicalize(&destination)?; + let path = dunce::canonicalize(&source_file)?; let mut comps: Vec<_> = path .components() @@ -205,10 +211,12 @@ fn cp( new_dst.push(fragment); } - (PathBuf::from(&source_file), PathBuf::from(new_dst)) + Ok((PathBuf::from(&source_file), PathBuf::from(new_dst))) }; - for (ref src, ref dst) in sources.paths_applying_with(strategy) { + let sources = sources.paths_applying_with(strategy)?; + + for (ref src, ref dst) in sources { if src.is_dir() { if !dst.exists() { match std::fs::create_dir_all(dst) { diff --git a/src/commands/mv.rs b/src/commands/mv.rs index 8f78e2d26..fc00248b4 100644 --- a/src/commands/mv.rs +++ b/src/commands/mv.rs @@ -181,7 +181,7 @@ fn mv( let strategy = |(source_file, depth_level)| { let mut new_dst = destination.clone(); - let path = dunce::canonicalize(&source_file).unwrap(); + let path = dunce::canonicalize(&source_file)?; let mut comps: Vec<_> = path .components() @@ -196,10 +196,12 @@ fn mv( new_dst.push(fragment); } - (PathBuf::from(&source_file), PathBuf::from(new_dst)) + Ok((PathBuf::from(&source_file), PathBuf::from(new_dst))) }; - for (ref src, ref dst) in sources.paths_applying_with(strategy) { + let sources = sources.paths_applying_with(strategy)?; + + for (ref src, ref dst) in sources { if src.is_dir() { if !dst.exists() { match std::fs::create_dir_all(dst) { diff --git a/src/utils.rs b/src/utils.rs index 464cfb665..1e7589851 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -105,9 +105,9 @@ impl FileStructure { self.root = path.to_path_buf(); } - pub fn paths_applying_with(&mut self, to: F) -> Vec<(PathBuf, PathBuf)> + pub fn paths_applying_with(&mut self, to: F) -> Result, Box> where - F: Fn((PathBuf, usize)) -> (PathBuf, PathBuf), + F: Fn((PathBuf, usize)) -> Result<(PathBuf, PathBuf), Box>, { self.resources .iter()