rust: fix copying of data files to not remove existing (but unrelated) files (#139)

This commit is contained in:
Tyler Wilding 2022-08-09 20:07:42 -04:00 committed by GitHub
parent ec4eb73c88
commit 3b6eb676ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 17 deletions

7
src-tauri/Cargo.lock generated
View file

@ -669,6 +669,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "fs_extra"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
[[package]] [[package]]
name = "futf" name = "futf"
version = "0.1.5" version = "0.1.5"
@ -1681,6 +1687,7 @@ dependencies = [
name = "opengoal-launcher" name = "opengoal-launcher"
version = "1.0.1" version = "1.0.1"
dependencies = [ dependencies = [
"fs_extra",
"serde", "serde",
"serde_json", "serde_json",
"tauri", "tauri",

View file

@ -15,6 +15,7 @@ rust-version = "1.61"
tauri-build = { version = "1.0.4", features = [] } tauri-build = { version = "1.0.4", features = [] }
[dependencies] [dependencies]
fs_extra = "1.2.0"
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.5", features = ["api-all", "devtools"] } tauri = { version = "1.0.5", features = ["api-all", "devtools"] }

View file

@ -1,8 +1,7 @@
use std::path::Path;
use std::process::Command; use std::process::Command;
use std::{fs, io};
use tauri::command; use tauri::command;
use tauri::Manager; use tauri::Manager;
use fs_extra::dir::copy;
#[derive(Debug, serde::Serialize)] #[derive(Debug, serde::Serialize)]
pub enum CommandError { pub enum CommandError {
@ -37,23 +36,16 @@ pub fn open_dir(dir: String) {
return open_appdir(dir); return open_appdir(dir);
} }
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> 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] #[command]
pub async fn copy_dir(dir_src: String, dir_dest: String) -> bool { 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")] #[cfg(target_os = "windows")]