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. --- src/error.rs | 2 -- src/lib.rs | 1 + src/minemod.rs | 12 +++--------- src/util.rs | 21 +++++++++++++++++++++ src/world.rs | 3 ++- 5 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 src/util.rs (limited to 'src') 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