use fs_extra to recursively move folders (#2487)

This commit is contained in:
Aleš Katona 2020-09-03 17:44:53 -06:00 committed by GitHub
parent 1ffbb66e64
commit 4696c9069b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 7 deletions

7
Cargo.lock generated
View File

@ -1502,6 +1502,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "fs_extra"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
[[package]] [[package]]
name = "fuchsia-cprng" name = "fuchsia-cprng"
version = "0.1.1" version = "0.1.1"
@ -3032,6 +3038,7 @@ dependencies = [
"eml-parser", "eml-parser",
"encoding_rs", "encoding_rs",
"filesize", "filesize",
"fs_extra",
"futures 0.3.5", "futures 0.3.5",
"futures-util", "futures-util",
"futures_codec", "futures_codec",

View File

@ -41,6 +41,7 @@ dtparse = "1.1.0"
dunce = "1.0.1" dunce = "1.0.1"
eml-parser = "0.1.0" eml-parser = "0.1.0"
filesize = "0.2.0" filesize = "0.2.0"
fs_extra = "1.2.0"
futures = {version = "0.3", features = ["compat", "io-compat"]} futures = {version = "0.3", features = ["compat", "io-compat"]}
futures-util = "0.3.5" futures-util = "0.3.5"
futures_codec = "0.4" futures_codec = "0.4"

View File

@ -751,17 +751,31 @@ fn move_file(from: TaggedPathBuf, to: TaggedPathBuf) -> Result<(), ShellError> {
to.push(from_file_name); to.push(from_file_name);
} }
move_item(&from, from_tag, &to)
}
fn move_item(from: &Path, from_tag: &Tag, to: &Path) -> Result<(), ShellError> {
// We first try a rename, which is a quick operation. If that doesn't work, we'll try a copy // We first try a rename, which is a quick operation. If that doesn't work, we'll try a copy
// and remove the old file. This is necessary if we're moving across filesystems. // and remove the old file/folder. This is necessary if we're moving across filesystems or devices.
std::fs::rename(&from, &to) std::fs::rename(&from, &to).or_else(|_| {
.or_else(|_| std::fs::copy(&from, &to).and_then(|_| std::fs::remove_file(&from))) match if from.is_file() {
.map_err(|e| { let mut options = fs_extra::file::CopyOptions::new();
ShellError::labeled_error( options.overwrite = true;
fs_extra::file::move_file(from, to, &options)
} else {
let mut options = fs_extra::dir::CopyOptions::new();
options.overwrite = true;
options.copy_inside = true;
fs_extra::dir::move_dir(from, to, &options)
} {
Ok(_) => Ok(()),
Err(e) => Err(ShellError::labeled_error(
format!("Could not move {:?} to {:?}. {:}", from, to, e.to_string()), format!("Could not move {:?} to {:?}. {:}", from, to, e.to_string()),
"could not move", "could not move",
from_tag, from_tag,
) )),
}) }
})
} }
fn is_empty_dir(dir: impl AsRef<Path>) -> bool { fn is_empty_dir(dir: impl AsRef<Path>) -> bool {