mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-01 10:40:14 +08:00
fix(profile): recover interrupted subscription refreshes
Recover pending redo at startup, coordinate profile readers and writers, and publish Draft state only after persistence. Keep the Windows legacy path by default.
This commit is contained in:
@@ -2,7 +2,7 @@ use super::CmdResult;
|
||||
use crate::{
|
||||
cmd::StringifyErr as _,
|
||||
cmd::validate::{ValidationNoticeTarget, handle_validation_notice},
|
||||
config::{Config, IProfiles, read_profile_item_file},
|
||||
config::{Config, IProfiles, profile_storage, profiles::PROFILE_WRITE_LOCK, read_profile_item_file},
|
||||
core::auto_backup::{self, AutoBackupTrigger},
|
||||
core::{
|
||||
CoreManager, handle,
|
||||
@@ -34,6 +34,9 @@ pub async fn save_profile_file(
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let _profile_write = PROFILE_WRITE_LOCK.lock().await;
|
||||
let profile_guard = profile_storage::write().await.stringify_err()?;
|
||||
|
||||
// 在异步操作前获取必要元数据并释放锁
|
||||
let (rel_path, is_merge_file, is_script_file, affects_runtime) = {
|
||||
let profiles = Config::profiles().await;
|
||||
@@ -65,12 +68,18 @@ pub async fn save_profile_file(
|
||||
|
||||
// 保存新的配置文件
|
||||
fs::write(&file_path, &file_data).await.stringify_err()?;
|
||||
drop(profile_guard);
|
||||
|
||||
let original = OriginalProfileFile {
|
||||
content: &original_content,
|
||||
existed: original_existed,
|
||||
written: &file_data,
|
||||
};
|
||||
|
||||
let changes_applied = handle_saved_profile_file(
|
||||
&file_path_str,
|
||||
&file_path,
|
||||
&original_content,
|
||||
original_existed,
|
||||
&original,
|
||||
is_merge_file,
|
||||
is_script_file,
|
||||
affects_runtime,
|
||||
@@ -86,13 +95,19 @@ pub async fn save_profile_file(
|
||||
Ok(changes_applied)
|
||||
}
|
||||
|
||||
async fn restore_original(
|
||||
file_path: &std::path::Path,
|
||||
original_content: &str,
|
||||
original_existed: bool,
|
||||
) -> CmdResult<()> {
|
||||
if original_existed {
|
||||
fs::write(file_path, original_content).await.stringify_err()
|
||||
struct OriginalProfileFile<'a> {
|
||||
content: &'a str,
|
||||
existed: bool,
|
||||
written: &'a str,
|
||||
}
|
||||
|
||||
async fn restore_original(file_path: &std::path::Path, original: &OriginalProfileFile<'_>) -> CmdResult<()> {
|
||||
let _guard = profile_storage::write().await.stringify_err()?;
|
||||
if fs::read(file_path).await.stringify_err()? != original.written.as_bytes() {
|
||||
return Err("profile file changed during validation".into());
|
||||
}
|
||||
if original.existed {
|
||||
fs::write(file_path, original.content).await.stringify_err()
|
||||
} else {
|
||||
fs::remove_file(file_path).await.stringify_err()
|
||||
}
|
||||
@@ -122,8 +137,7 @@ fn profile_affects_runtime(profiles: &IProfiles, index: &str) -> bool {
|
||||
async fn handle_saved_profile_file(
|
||||
file_path_str: &str,
|
||||
file_path: &std::path::Path,
|
||||
original_content: &str,
|
||||
original_existed: bool,
|
||||
original: &OriginalProfileFile<'_>,
|
||||
is_merge_file: bool,
|
||||
is_script_file: bool,
|
||||
affects_runtime: bool,
|
||||
@@ -140,13 +154,13 @@ async fn handle_saved_profile_file(
|
||||
Ok(outcome) if outcome.is_valid() => {}
|
||||
Ok(outcome) => {
|
||||
logging!(warn, Type::Config, "[cmd配置save] 文件验证失败: {}", outcome);
|
||||
restore_original(file_path, original_content, original_existed).await?;
|
||||
restore_original(file_path, original).await?;
|
||||
handle_validation_notice(&outcome, target, file_type);
|
||||
return Ok(outcome);
|
||||
}
|
||||
Err(e) => {
|
||||
logging!(error, Type::Config, "[cmd配置save] 验证过程发生错误: {e:#}");
|
||||
restore_original(file_path, original_content, original_existed).await?;
|
||||
restore_original(file_path, original).await?;
|
||||
return Err(e.to_string().into());
|
||||
}
|
||||
}
|
||||
@@ -167,13 +181,13 @@ async fn handle_saved_profile_file(
|
||||
}
|
||||
Ok(outcome) => {
|
||||
logging!(warn, Type::Config, "[cmd配置save] 运行时配置应用失败: {}", outcome);
|
||||
restore_original(file_path, original_content, original_existed).await?;
|
||||
restore_original(file_path, original).await?;
|
||||
handle_validation_notice(&outcome, ValidationNoticeTarget::Runtime, "运行时配置");
|
||||
Ok(outcome)
|
||||
}
|
||||
Err(err) => {
|
||||
logging!(error, Type::Config, "[cmd配置save] 运行时配置应用错误: {err:#}");
|
||||
restore_original(file_path, original_content, original_existed).await?;
|
||||
restore_original(file_path, original).await?;
|
||||
Err(err.to_string().into())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user