feat(core): improved files migration code

This commit is contained in:
Observer KRypt0n_ 2023-08-18 09:15:06 +02:00
parent 8a5693f9e0
commit eb5aa221ef
No known key found for this signature in database
GPG key ID: 844DA47BA25FE1E2
5 changed files with 34 additions and 29 deletions

View file

@ -15,7 +15,7 @@ use anime_launcher_sdk::anime_game_core::genshin::prelude::*;
use tracing_subscriber::prelude::*; use tracing_subscriber::prelude::*;
use tracing_subscriber::filter::*; use tracing_subscriber::filter::*;
pub mod move_folder; pub mod move_files;
pub mod i18n; pub mod i18n;
pub mod background; pub mod background;
pub mod ui; pub mod ui;

31
src/move_files.rs Normal file
View file

@ -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<Path>, to: impl AsRef<Path>) -> 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(())
}

View file

@ -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(())
}

View file

@ -369,7 +369,7 @@ impl SimpleAsyncComponent for DefaultPathsApp {
))); )));
if &from != to && from.exists() { 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)); self.progress_bar.sender().send(ProgressBarMsg::UpdateProgress(i as u64 + 1, folders.len() as u64));

View file

@ -10,7 +10,7 @@ pub fn migrate_folder(sender: ComponentSender<App>, from: PathBuf, to: PathBuf,
sender.input(AppMsg::DisableButtons(true)); sender.input(AppMsg::DisableButtons(true));
std::thread::spawn(move || { 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 { if let Some(cleanup_folder) = cleanup_folder {
std::fs::remove_dir_all(cleanup_folder).expect("Failed to remove cleanup folder"); std::fs::remove_dir_all(cleanup_folder).expect("Failed to remove cleanup folder");