From 13257d8d88c0783b9a87e1796a4c6b0bad6caba9 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 7 Nov 2021 01:59:57 +0100 Subject: remove fs_extra crate The function that we needed from there is very small and easily implemented ourselves. On the other hand, fs_extra seemed to have some weird options going on and implemented the copy in a bit of a roundabout manner. --- Cargo.lock | 7 ------- Cargo.toml | 1 - src/error.rs | 2 -- src/lib.rs | 1 + src/minemod.rs | 12 +++--------- src/util.rs | 21 +++++++++++++++++++++ src/world.rs | 3 ++- 7 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 src/util.rs diff --git a/Cargo.lock b/Cargo.lock index fb5d7c7..fe6497f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -264,12 +264,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "fs_extra" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" - [[package]] name = "fuchsia-cprng" version = "0.1.1" @@ -472,7 +466,6 @@ dependencies = [ "anyhow", "clap", "dirs", - "fs_extra", "itertools", "log", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 988af09..02650e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ edition = "2018" anyhow = "1.0.45" clap = "2.33.3" dirs = "4.0.0" -fs_extra = "1.2.0" itertools = "0.10.1" log = "0.4.14" once_cell = "1.8.0" diff --git a/src/error.rs b/src/error.rs index d60fabd..08539c0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,8 +14,6 @@ pub enum Error { InvalidScrape, #[error("'{0}' is not a valid mod directory")] InvalidModDir(PathBuf), - #[error("filesystem error")] - FsExtraError(#[from] fs_extra::error::Error), #[error("ZIP error")] ZipError(#[from] zip::result::ZipError), #[error("the downloaded file was empty")] diff --git a/src/lib.rs b/src/lib.rs index f169c82..9a3598f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ pub mod error; pub mod game; pub mod kvstore; pub mod minemod; +mod util; pub mod world; pub use baas::{Baas, Snapshot}; diff --git a/src/minemod.rs b/src/minemod.rs index d90605c..9162907 100644 --- a/src/minemod.rs +++ b/src/minemod.rs @@ -5,11 +5,9 @@ use std::{ path::{Path, PathBuf}, }; -use fs_extra::dir::{self, CopyOptions}; - use super::{ error::{Error, Result}, - kvstore, scan, + kvstore, scan, util, }; /// The type of the ID that is used to identify Minetest mods. @@ -68,11 +66,9 @@ impl MineMod { /// /// Returns a new [`MineMod`] object pointing to the copy. pub fn copy_to>(&self, path: P) -> Result { - let mut options = CopyOptions::new(); - options.content_only = true; let path = path.as_ref().join(self.mod_id()?); fs::create_dir_all(&path)?; - dir::copy(&self.path, &path, &options)?; + util::copy_recursive(&self.path, &path)?; MineMod::open(&path) } } @@ -134,11 +130,9 @@ impl Modpack { /// /// Returns a new [`Modpack`] object pointing to the copy. pub fn copy_to>(&self, path: P) -> Result { - let mut options = CopyOptions::new(); - options.content_only = true; let path = path.as_ref().join(self.name()?); fs::create_dir_all(&path)?; - dir::copy(&self.path, &path, &options)?; + util::copy_recursive(&self.path, &path)?; Modpack::open(&path) } } diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..24b8701 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,21 @@ +use std::{fs, io, path::Path}; + +/// Recursively copy the *contents* of the given directory to the given path. +pub fn copy_recursive, D: AsRef>(source: S, destination: D) -> io::Result<()> { + copy_inner(source.as_ref(), destination.as_ref()) +} + +fn copy_inner(source: &Path, destination: &Path) -> io::Result<()> { + for item in fs::read_dir(source)? { + let item = item?; + let metadata = item.metadata()?; + let item_destination = destination.join(item.file_name()); + if metadata.is_file() { + fs::copy(&item.path(), &item_destination)?; + } else if metadata.is_dir() { + fs::create_dir(&item_destination)?; + copy_inner(&item.path(), &item_destination)?; + } + } + Ok(()) +} diff --git a/src/world.rs b/src/world.rs index 79ba3f9..31ddbd4 100644 --- a/src/world.rs +++ b/src/world.rs @@ -37,7 +37,8 @@ impl World { /// Returns the name of the world. pub fn world_name(&self) -> Result { - let fallback = self.path + let fallback = self + .path .file_name() .map(|s| s.to_str().expect("Non-UTF8 directory encountered")); -- cgit v1.2.3