From 6b715002fc824154b6a3539fb0f4feca1a6f92e5 Mon Sep 17 00:00:00 2001 From: wanglei Date: Mon, 30 Nov 2026 11:50:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=9F=E5=AD=90=E4=BF=9D=E5=AD=98=E9=85=8D?= =?UTF-8?q?=E6=96=B9=20YAML=20=E5=B9=B6=E4=BF=9D=E7=95=99=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E5=89=8D=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sop/SopConfigLoader.cs | 20 ++++++++++--- tests/RecipePackage.Smoke/Program.cs | 29 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/SopConfigLoader.cs b/src/SopHelper.CrossPlatform.Engine/Sop/SopConfigLoader.cs index 0bd7519..a653dc7 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/SopConfigLoader.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/SopConfigLoader.cs @@ -89,13 +89,25 @@ public static partial class SopConfigLoader /// /// 写回文件,并顺手校验一遍(校验提醒随返回值给出,编辑器要显示)。 /// - /// 按配方序列化约定写回 YAML,供运行和编辑器重新加载。 + /// 同目录暂存并重新加载校验,通过后原子替换正式文件。写入或校验失败时保留旧文件。 public static SopConfig Save(SopConfig config, string yamlPath) { - File.WriteAllText(yamlPath, ToYaml(config), new UTF8Encoding(false)); + var bytes = Encoding.UTF8.GetBytes(ToYaml(config)); + var temporary = yamlPath + "." + Guid.NewGuid().ToString("N") + ".tmp"; + try + { + using (var stream = new FileStream(temporary, FileMode.CreateNew, FileAccess.Write, FileShare.None)) + { + stream.Write(bytes); + stream.Flush(flushToDisk: true); + } - // 落盘后再读一遍:校验的是文件里真正的内容,不是内存里那份 - return Load(yamlPath); + // 校验实际写入的内容,避免在解析失败后才发现旧配方已被覆盖。 + var saved = Load(temporary); + File.Move(temporary, yamlPath, overwrite: true); + return saved; + } + finally { if (File.Exists(temporary)) File.Delete(temporary); } } /// diff --git a/tests/RecipePackage.Smoke/Program.cs b/tests/RecipePackage.Smoke/Program.cs index f62b77e..ef29034 100644 --- a/tests/RecipePackage.Smoke/Program.cs +++ b/tests/RecipePackage.Smoke/Program.cs @@ -34,8 +34,37 @@ internal static class Program } } + /// 验证配方覆盖保存及替换失败后的临时文件清理。 + private static void AtomicYamlSave() + { + var folder = Directory.CreateDirectory(Path.Combine(Root, "atomic-save")).FullName; + var path = Path.Combine(folder, "atomic.yaml"); + var config = new SopConfig(); + config.Sop.Name = "原配方"; + SopConfigLoader.Save(config, path); + config.Sop.Name = "新配方"; + if (OperatingSystem.IsWindows()) + { + // 独占打开正式配方,确保替换失败时旧内容仍可读。 + using (File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None)) + { + try + { + SopConfigLoader.Save(config, path); + throw new Exception("文件被占用时应拒绝替换"); + } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + } + Check(SopConfigLoader.Load(path).Sop.Name == "原配方", "原子替换失败时保留原配方"); + } + Check(SopConfigLoader.Save(config, path).Sop.Name == "新配方" + && SopConfigLoader.Load(path).Sop.Name == "新配方", "原子保存可覆盖原配方并返回重载结果"); + Check(Directory.GetFiles(folder, "atomic.yaml.*.tmp").Length == 0, "原子保存不残留临时文件"); + } + private static void Run() { + AtomicYamlSave(); LegacyWorkspace(); RoundTrip(); OverwriteAndRename();