From eb5aa221efb8de9eba4b43b2c79b305130b5f193 Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Fri, 18 Aug 2023 09:15:06 +0200 Subject: [PATCH] feat(core): improved files migration code --- src/main.rs | 2 +- src/move_files.rs | 31 +++++++++++++++++++++++++++++++ src/move_folder.rs | 26 -------------------------- src/ui/first_run/default_paths.rs | 2 +- src/ui/main/migrate_folder.rs | 2 +- 5 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 src/move_files.rs delete mode 100644 src/move_folder.rs diff --git a/src/main.rs b/src/main.rs index a95ea7b..539c859 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use anime_launcher_sdk::anime_game_core::genshin::prelude::*; use tracing_subscriber::prelude::*; use tracing_subscriber::filter::*; -pub mod move_folder; +pub mod move_files; pub mod i18n; pub mod background; pub mod ui; diff --git a/src/move_files.rs b/src/move_files.rs new file mode 100644 index 0000000..6ca3cd1 --- /dev/null +++ b/src/move_files.rs @@ -0,0 +1,31 @@ +use std::path::Path; +use std::io::Result; + +/// Move files from one folder to another +pub fn move_files(from: impl AsRef, to: impl AsRef) -> Result<()> { + for entry in from.as_ref().read_dir()?.flatten() { + let source = entry.path(); + let target = to.as_ref().join(entry.file_name()); + + if std::fs::rename(&source, &target).is_err() { + if source.is_dir() { + std::fs::create_dir_all(&target) + .and_then(|_| move_files(&source, &target)) + .and_then(|_| std::fs::remove_dir_all(&source))?; + } + + else if source.is_symlink() { + std::fs::read_link(&source) + .and_then(|link_target| std::os::unix::fs::symlink(link_target, &target)) + .and_then(|_| std::fs::remove_file(&source))?; + } + + else { + std::fs::copy(&source, &target) + .and_then(|_| std::fs::remove_file(&source))?; + } + } + } + + Ok(()) +} diff --git a/src/move_folder.rs b/src/move_folder.rs deleted file mode 100644 index c975607..0000000 --- a/src/move_folder.rs +++ /dev/null @@ -1,26 +0,0 @@ -use std::path::Path; - -pub fn move_folder(from: &Path, to: &Path) -> std::io::Result<()> { - if !to.exists() { - std::fs::create_dir_all(to)?; - } - - for entry in from.read_dir()?.flatten() { - let to_path = to.join(entry.file_name()); - - if entry.metadata()?.is_dir() { - move_folder(&entry.path(), &to_path)?; - } - - else if entry.metadata()?.is_file() { - std::fs::copy(entry.path(), to_path)?; - std::fs::remove_file(entry.path())?; - } - - // TODO: symlinks? - } - - std::fs::remove_dir_all(from)?; - - Ok(()) -} diff --git a/src/ui/first_run/default_paths.rs b/src/ui/first_run/default_paths.rs index 8c8fd03..6e2a96c 100644 --- a/src/ui/first_run/default_paths.rs +++ b/src/ui/first_run/default_paths.rs @@ -369,7 +369,7 @@ impl SimpleAsyncComponent for DefaultPathsApp { ))); if &from != to && from.exists() { - move_folder::move_folder(from, to).expect(&format!("Failed to move folder: {:?} -> {:?}", from, to)); + move_files::move_files(from, to).expect(&format!("Failed to move folder: {:?} -> {:?}", from, to)); } self.progress_bar.sender().send(ProgressBarMsg::UpdateProgress(i as u64 + 1, folders.len() as u64)); diff --git a/src/ui/main/migrate_folder.rs b/src/ui/main/migrate_folder.rs index dbeb0c4..db8ea43 100644 --- a/src/ui/main/migrate_folder.rs +++ b/src/ui/main/migrate_folder.rs @@ -10,7 +10,7 @@ pub fn migrate_folder(sender: ComponentSender, from: PathBuf, to: PathBuf, sender.input(AppMsg::DisableButtons(true)); std::thread::spawn(move || { - move_folder::move_folder(&from, &to).expect("Failed to perform migration"); + move_files::move_files(&from, &to).expect("Failed to perform migration"); if let Some(cleanup_folder) = cleanup_folder { std::fs::remove_dir_all(cleanup_folder).expect("Failed to remove cleanup folder");