feat(fs): atomic writes for JSON and TOML saves\n\n- Introduce atomic_write utility and use it in write_json_file\n- Add write_text_file for TOML/strings and use in Codex paths\n- Reduce risk of partial writes and ensure directory creation

This commit is contained in:
Jason
2025-09-04 16:00:19 +08:00
parent 25c112856d
commit 64f7e47b20
3 changed files with 57 additions and 12 deletions

View File

@@ -320,16 +320,16 @@ pub async fn switch_provider(
toml::from_str::<toml::Table>(cfg_str)
.map_err(|e| format!("config.toml 格式错误: {}", e))?;
}
std::fs::write(&config_path, cfg_str)
crate::config::write_text_file(&config_path, cfg_str)
.map_err(|e| format!("写入 config.toml 失败: {}", e))?;
} else {
// 非字符串时,写空
std::fs::write(&config_path, "")
crate::config::write_text_file(&config_path, "")
.map_err(|e| format!("写入空的 config.toml 失败: {}", e))?;
}
} else {
// 缺失则写空
std::fs::write(&config_path, "")
crate::config::write_text_file(&config_path, "")
.map_err(|e| format!("写入空的 config.toml 失败: {}", e))?;
}
}