fix: make profile deletion rollback-safe

This commit is contained in:
Tunglies
2026-08-03 20:00:01 +08:00
parent 903410c12e
commit c5b641e421
14 changed files with 264 additions and 113 deletions
+4 -4
View File
@@ -3,7 +3,7 @@ use crate::feat;
use crate::utils::{dirs, yaml_emitter};
use crate::{
cmd::StringifyErr as _,
config::{ClashInfo, Config},
config::{ClashInfo, Config, profiles::profiles_save_file_safe},
constants,
core::{
CoreManager, handle,
@@ -64,7 +64,7 @@ pub async fn change_clash_core(clash_core: String) -> CmdResult<Option<String>>
match CoreManager::global().change_core(&clash_core).await {
Ok(_) => {
logging_error!(Type::Core, Config::profiles().await.data_arc().save_file().await);
logging_error!(Type::Core, profiles_save_file_safe().await);
// 切换内核后重启内核
match CoreManager::global().restart_core().await {
@@ -107,7 +107,7 @@ pub async fn start_core() -> CmdResult {
/// 关闭核心
#[tauri::command]
pub async fn stop_core() -> CmdResult {
logging_error!(Type::Core, Config::profiles().await.data_arc().save_file().await);
logging_error!(Type::Core, profiles_save_file_safe().await);
let result = CoreManager::global()
.stop_core()
.await
@@ -121,7 +121,7 @@ pub async fn stop_core() -> CmdResult {
/// 重启核心
#[tauri::command]
pub async fn restart_core() -> CmdResult {
logging_error!(Type::Core, Config::profiles().await.data_arc().save_file().await);
logging_error!(Type::Core, profiles_save_file_safe().await);
let result = CoreManager::global()
.restart_core()
.await
+43 -26
View File
@@ -6,7 +6,7 @@ use crate::{
config::{
Config, IProfiles, PrfItem, PrfOption,
profiles::{
profiles_append_item_with_filedata_safe, profiles_delete_item_safe, profiles_patch_item_safe,
PROFILE_WRITE_LOCK, profiles_append_item_with_filedata_safe, profiles_patch_item_safe,
profiles_reorder_safe, profiles_save_file_safe,
},
profiles_append_item_safe,
@@ -153,13 +153,44 @@ pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResu
/// 删除配置文件
#[tauri::command]
pub async fn delete_profile(index: String) -> CmdResult {
// 使用Send-safe helper函数
let should_update = profiles_delete_item_safe(&index)
.await
.with_error_code("PROFILE_DELETE_FAILED")?;
profiles_save_file_safe()
let profile_write_guard = PROFILE_WRITE_LOCK.lock().await;
let profiles = Config::profiles().await;
let result = profiles
.with_data_modify(|mut candidate| async move {
let original = candidate.clone();
let (should_update, plan) = candidate.plan_delete_item(&index)?;
let guard = if should_update {
match CoreManager::global()
.update_config_forced_with_profiles(&candidate, &original)
.await?
{
Ok(guard) => Some(guard),
Err(outcome) => return Ok((original, Err(outcome))),
}
} else {
candidate.save_file().await?;
None
};
let current = candidate.current.clone();
plan.cleanup().await;
Ok((candidate, Ok((should_update, current, guard))))
})
.await
.with_error_code("PROFILE_DELETE_FAILED")?;
let (should_update, current, config_update_guard) = match result {
Ok(result) => result,
Err(outcome) => {
handle_validation_notice(&outcome, ValidationNoticeTarget::Runtime, "运行时配置");
return Err(coded_error("PROFILE_DELETE_FAILED", outcome));
}
};
if should_update {
logging_error!(Type::Config, profiles::activate_selected_nodes());
}
drop(config_update_guard);
drop(profile_write_guard);
if let Err(e) = Tray::global().update_tooltip().await {
logging!(warn, Type::Cmd, "Warning: 异步更新托盘提示失败: {e}");
}
@@ -168,28 +199,13 @@ pub async fn delete_profile(index: String) -> CmdResult {
logging!(warn, Type::Cmd, "Warning: 异步更新托盘菜单失败: {e}");
}
if should_update {
match CoreManager::global().update_config_forced().await {
Ok(outcome) if outcome.is_valid() => {
handle::Handle::refresh_clash();
// 发送配置变更通知
logging!(info, Type::Cmd, "[删除订阅] 发送配置变更通知: {}", index);
handle::Handle::notify_profile_changed(&index);
}
Ok(outcome) => {
logging!(warn, Type::Cmd, "删除订阅后更新配置失败: {}", outcome);
handle_validation_notice(&outcome, ValidationNoticeTarget::Runtime, "运行时配置");
return Err(coded_error("PROFILE_DELETE_FAILED", outcome));
}
Err(e) => {
logging!(error, Type::Cmd, "{}", e);
return Err(coded_error("PROFILE_DELETE_FAILED", e));
}
handle::Handle::refresh_clash();
if let Some(current) = current.as_ref() {
logging!(info, Type::Cmd, "[删除订阅] 发送配置变更通知: {}", current);
handle::Handle::notify_profile_changed(current);
}
}
Timer::global()
.refresh()
.await
.with_error_code("PROFILE_DELETE_FAILED")?;
logging_error!(Type::Timer, Timer::global().refresh().await);
Ok(())
}
@@ -316,6 +332,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<ValidationO
logging!(info, Type::Cmd, "当前正在切换配置,放弃请求");
return Ok(ValidationOutcome::Busy);
}
let _profile_write_guard = PROFILE_WRITE_LOCK.lock().await;
let target_profile = profiles.current.as_ref();