feat: overwrite resource file according to file modified

This commit is contained in:
GyDi 2023-07-22 09:18:54 +08:00
parent b307b9a66b
commit aadfaf7150
No known key found for this signature in database
GPG Key ID: 9C3AD40F1F99880A

View File

@ -66,7 +66,8 @@ fn init_log() -> Result<()> {
Ok(()) Ok(())
} }
/// Initialize all the files from resources /// Initialize all the config files
/// before tauri setup
pub fn init_config() -> Result<()> { pub fn init_config() -> Result<()> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
unsafe { unsafe {
@ -111,7 +112,8 @@ pub fn init_config() -> Result<()> {
Ok(()) Ok(())
} }
/// initialize app /// initialize app resources
/// after tauri setup
pub fn init_resources(package_info: &PackageInfo) -> Result<()> { pub fn init_resources(package_info: &PackageInfo) -> Result<()> {
let app_dir = dirs::app_home_dir()?; let app_dir = dirs::app_home_dir()?;
let res_dir = dirs::app_resources_dir(package_info)?; 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); 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 // 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 src_path = res_dir.join(file);
let target_path = app_dir.join(file); let dest_path = app_dir.join(file);
if src_path.exists() {
let _ = fs::copy(src_path, target_path); 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(()) Ok(())