From aadfaf7150640120910748d158785ffeef1841a2 Mon Sep 17 00:00:00 2001
From: GyDi <zzzgydi@gmail.com>
Date: Sat, 22 Jul 2023 09:18:54 +0800
Subject: [PATCH] feat: overwrite resource file according to file modified

---
 src-tauri/src/utils/init.rs | 48 ++++++++++++++++++++++++++++++++-----
 1 file changed, 42 insertions(+), 6 deletions(-)

diff --git a/src-tauri/src/utils/init.rs b/src-tauri/src/utils/init.rs
index 291e7aa..4135441 100644
--- a/src-tauri/src/utils/init.rs
+++ b/src-tauri/src/utils/init.rs
@@ -66,7 +66,8 @@ fn init_log() -> Result<()> {
     Ok(())
 }
 
-/// Initialize all the files from resources
+/// Initialize all the config files
+/// before tauri setup
 pub fn init_config() -> Result<()> {
     #[cfg(target_os = "windows")]
     unsafe {
@@ -111,7 +112,8 @@ pub fn init_config() -> Result<()> {
     Ok(())
 }
 
-/// initialize app
+/// initialize app resources
+/// after tauri setup
 pub fn init_resources(package_info: &PackageInfo) -> Result<()> {
     let app_dir = dirs::app_home_dir()?;
     let res_dir = dirs::app_resources_dir(package_info)?;
@@ -123,13 +125,47 @@ pub fn init_resources(package_info: &PackageInfo) -> Result<()> {
         let _ = fs::create_dir_all(&res_dir);
     }
 
+    #[cfg(target_os = "windows")]
+    let file_list = ["Country.mmdb", "geoip.dat", "geosite.dat", "wintun.dll"];
+    #[cfg(not(target_os = "windows"))]
+    let file_list = ["Country.mmdb", "geoip.dat", "geosite.dat"];
+
     // copy the resource file
-    for file in ["Country.mmdb", "geoip.dat", "geosite.dat", "wintun.dll"].iter() {
+    // if the source file is newer than the destination file, copy it over
+    for file in file_list.iter() {
         let src_path = res_dir.join(file);
-        let target_path = app_dir.join(file);
-        if src_path.exists() {
-            let _ = fs::copy(src_path, target_path);
+        let dest_path = app_dir.join(file);
+
+        let handle_copy = || {
+            match fs::copy(&src_path, &dest_path) {
+                Ok(_) => log::debug!(target: "app", "resources copied '{file}'"),
+                Err(err) => {
+                    log::error!(target: "app", "failed to copy resources '{file}', {err}")
+                }
+            };
+        };
+
+        if src_path.exists() && !dest_path.exists() {
+            handle_copy();
+            continue;
         }
+
+        let src_modified = fs::metadata(&src_path).and_then(|m| m.modified());
+        let dest_modified = fs::metadata(&dest_path).and_then(|m| m.modified());
+
+        match (src_modified, dest_modified) {
+            (Ok(src_modified), Ok(dest_modified)) => {
+                if src_modified > dest_modified {
+                    handle_copy();
+                } else {
+                    log::debug!(target: "app", "skipping resource copy '{file}'");
+                }
+            }
+            _ => {
+                log::debug!(target: "app", "failed to get modified '{file}'");
+                handle_copy();
+            }
+        };
     }
 
     Ok(())
-- 
GitLab