mirror of
https://github.com/an-anime-team/an-anime-game-launcher.git
synced 2024-12-17 23:41:47 +03:00
feat(core): improved files migration code
This commit is contained in:
parent
8a5693f9e0
commit
eb5aa221ef
5 changed files with 34 additions and 29 deletions
|
@ -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;
|
||||
|
|
31
src/move_files.rs
Normal file
31
src/move_files.rs
Normal 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(())
|
||||
}
|
|
@ -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(())
|
||||
}
|
|
@ -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));
|
||||
|
|
|
@ -10,7 +10,7 @@ pub fn migrate_folder(sender: ComponentSender<App>, 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");
|
||||
|
|
Loading…
Reference in a new issue