Skip to content

Commit

Permalink
fix: config migration issue
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Feb 14, 2024
1 parent 5bf4775 commit 5772101
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 27 deletions.
8 changes: 4 additions & 4 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions backend/tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ fn main() -> std::io::Result<()> {

crate::log_err!(init::init_config());

let verge = { Config::verge().latest().language.clone().unwrap() };
rust_i18n::set_locale(verge.as_str());

// Panic Hook to show a panic dialog and save logs
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
Expand All @@ -36,6 +33,9 @@ fn main() -> std::io::Result<()> {
default_panic(info);
}));

let verge = { Config::verge().latest().language.clone().unwrap() };
rust_i18n::set_locale(verge.as_str());

#[allow(unused_mut)]
let mut builder = tauri::Builder::default()
.system_tray(SystemTray::new())
Expand Down
18 changes: 16 additions & 2 deletions backend/tauri/src/utils/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use rust_i18n::t;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Barrier};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc, Barrier,
};
use tauri::api::dialog::{MessageDialogBuilder, MessageDialogButtons, MessageDialogKind};

pub fn panic_dialog(msg: &str) {
Expand Down Expand Up @@ -32,3 +34,15 @@ pub fn migrate_dialog() -> bool {
barrier.wait();
migrate.load(Ordering::Relaxed)
}

pub fn error_dialog(msg: String) {
let barrier = Arc::new(Barrier::new(2));
let barrier_ref = barrier.clone();
MessageDialogBuilder::new("Error", msg)
.kind(MessageDialogKind::Error)
.buttons(MessageDialogButtons::Ok)
.show(move |_| {
barrier_ref.wait();
});
barrier.wait();
}
15 changes: 15 additions & 0 deletions backend/tauri/src/utils/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ macro_rules! log_err {
};
}

#[macro_export]
macro_rules! dialog_err {
($result: expr) => {
if let Err(err) = $result {
crate::utils::dialog::error_dialog(format!("{:?}", err));
}
};

($result: expr, $err_str: expr) => {
if let Err(_) = $result {
crate::utils::dialog::error_dialog($err_str.into());
}
};
}

#[macro_export]
macro_rules! trace_err {
($result: expr, $err_str: expr) => {
Expand Down
58 changes: 40 additions & 18 deletions backend/tauri/src/utils/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::utils::dialog::migrate_dialog;
use crate::{
config::*,
utils::{dirs, help},
utils::{dialog::migrate_dialog, dirs, help},
};
use anyhow::Result;
use chrono::Local;
Expand All @@ -11,8 +10,8 @@ use log4rs::{
config::{Appender, Logger, Root},
encode::pattern::PatternEncoder,
};
use std::fs;
use std::path::PathBuf;
use runas::Command as RunasCommand;
use std::{fs, io::ErrorKind, path::PathBuf};

/// initialize this instance's log file
fn init_log() -> Result<()> {
Expand Down Expand Up @@ -79,27 +78,30 @@ pub fn init_config() -> Result<()> {
#[cfg(target_os = "windows")]
let _ = dirs::init_portable_flag();

// Check if old config dir exist
// Check if old config dir exist and new config dir is not exist
let mut old_app_dir: Option<PathBuf> = None;
crate::log_err!(dirs::old_app_home_dir().map(|_old_app_dir| {
if _old_app_dir.exists() && migrate_dialog() {
old_app_dir = Some(_old_app_dir);
}
let mut app_dir: Option<PathBuf> = None;
crate::dialog_err!(dirs::old_app_home_dir().map(|_old_app_dir| {
old_app_dir = Some(_old_app_dir);
}));

let _ = init_log();
crate::dialog_err!(dirs::app_home_dir().map(|_app_dir| {
app_dir = Some(_app_dir);
}));

crate::log_err!(dirs::app_home_dir().map(|app_dir| {
// Do migrate
if let Some(_old_app_dir) = old_app_dir {
let _ = fs::remove_dir_all(&app_dir);
let _ = fs::rename(_old_app_dir, &app_dir);
if let (Some(app_dir), Some(old_app_dir)) = (old_app_dir, app_dir) {
if !app_dir.exists() && old_app_dir.exists() && migrate_dialog() {
if let Err(e) = do_config_migration(&old_app_dir, &app_dir) {
super::dialog::error_dialog(format!("failed to do migration: {:?}", e))
}
}

if !app_dir.exists() {
let _ = fs::create_dir_all(&app_dir);
let _ = fs::create_dir_all(app_dir);
}
}));
}

// init log
let _ = init_log();

crate::log_err!(dirs::app_profiles_dir().map(|profiles_dir| {
if !profiles_dir.exists() {
Expand Down Expand Up @@ -254,3 +256,23 @@ pub fn init_service() -> Result<()> {

Ok(())
}

fn do_config_migration(old_app_dir: &PathBuf, app_dir: &PathBuf) -> anyhow::Result<()> {
if let Err(e) = fs::rename(old_app_dir, app_dir) {
match e.kind() {
#[cfg(windows)]
ErrorKind::PermissionDenied => {
// It seems that clash-verge-service is running, so kill it.
let status = RunasCommand::new("cmd")
.args(&["/C", "taskkill", "/IM", "clash-verge-service.exe", "/F"])
.status()?;
if !status.success() {
anyhow::bail!("failed to kill clash-verge-service.exe")
}
fs::rename(old_app_dir, app_dir)?;
}
_ => return Err(e.into()),
};
}
Ok(())
}

0 comments on commit 5772101

Please sign in to comment.