From 3b6eb676ff94735c22d4a9626dae1f45444435e5 Mon Sep 17 00:00:00 2001 From: Tyler Wilding Date: Tue, 9 Aug 2022 20:07:42 -0400 Subject: [PATCH] rust: fix copying of data files to not remove existing (but unrelated) files (#139) --- src-tauri/Cargo.lock | 7 +++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/commands.rs | 26 +++++++++----------------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 0638312..1eb74a7 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -669,6 +669,12 @@ 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 = "futf" version = "0.1.5" @@ -1681,6 +1687,7 @@ dependencies = [ name = "opengoal-launcher" version = "1.0.1" dependencies = [ + "fs_extra", "serde", "serde_json", "tauri", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 4b1df5b..9ba1603 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -15,6 +15,7 @@ rust-version = "1.61" tauri-build = { version = "1.0.4", features = [] } [dependencies] +fs_extra = "1.2.0" serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } tauri = { version = "1.0.5", features = ["api-all", "devtools"] } diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 1c34f4d..87f1800 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,8 +1,7 @@ -use std::path::Path; use std::process::Command; -use std::{fs, io}; use tauri::command; use tauri::Manager; +use fs_extra::dir::copy; #[derive(Debug, serde::Serialize)] pub enum CommandError { @@ -37,23 +36,16 @@ pub fn open_dir(dir: String) { return open_appdir(dir); } -fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> io::Result<()> { - fs::create_dir_all(&dst)?; - for entry in fs::read_dir(src)? { - let entry = entry?; - let ty = entry.file_type()?; - if ty.is_dir() { - copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; - } else { - fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; - } - } - Ok(()) -} - #[command] pub async fn copy_dir(dir_src: String, dir_dest: String) -> bool { - return copy_dir_all(dir_src, dir_dest).is_ok(); + let mut options = fs_extra::dir::CopyOptions::new(); + options.copy_inside = true; + options.overwrite = true; + options.content_only = true; + if let Err(_e) = copy(dir_src, dir_dest, &options) { + return false; + } + return true; } #[cfg(target_os = "windows")]