From f316630deaa4d5322be7bcd5e2a105419ade4096 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 11:29:19 +0000 Subject: [PATCH 1/2] Write each output through a temporary file so a failed save keeps the earlier icon [patch] SaveAsPng(path) opens the destination with FileMode.Create, so a save that failed part-way left a truncated or 0-byte PNG under the output name and destroyed the output an earlier run wrote there. Encode to a .png..tmp file beside the output, move it over the destination once it is complete, and delete it if the save fails. Fixes #175 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017vfJW5GEJNk1TZFgDdUPFX --- CLAUDE.md | 6 +++ IconHelper.Test/ProcessDirectoryTests.cs | 53 ++++++++++++++++++++++++ IconHelper/IconHelper.cs | 33 ++++++++++++++- 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/CLAUDE.md b/CLAUDE.md index fd1f059..10f67b5 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -194,6 +194,12 @@ Each is covered by regression tests. Do not reintroduce them. `ProcessDirectoryTests.InputsSharingABaseNameDoNotOverwriteEachOther` and `ArgumentsTests.ValidateRejectsAnOutputThatIsTheInputDirectory`. +- **A failed save destroyed the earlier output.** `SaveAsPng(path)` truncates the destination before + encoding, so a save that failed part-way (a full disk, say) left a truncated or 0-byte PNG under + the output name, replacing the good icon from an earlier run. `WriteReplacing` now encodes to a + `.png..tmp` file beside the output and moves it into place, and deletes it on + failure. Pinned by `ProcessDirectoryTests.ASaveThatFailsPartWayLeavesTheEarlierOutputUntouched`. + ## Testing `IconHelper.Test` is an MSTest project (`MSTest.Sdk`, Microsoft Testing Platform). Both it and the diff --git a/IconHelper.Test/ProcessDirectoryTests.cs b/IconHelper.Test/ProcessDirectoryTests.cs index 9a7ddf3..3277992 100644 --- a/IconHelper.Test/ProcessDirectoryTests.cs +++ b/IconHelper.Test/ProcessDirectoryTests.cs @@ -420,4 +420,57 @@ public void SmallArtworkWithValidatedPaddingDoesNotFailTheBatch() Assert.AreEqual(1, result.Written); Assert.AreEqual(IconHelper.ExitSuccess, IconHelper.ExitCodeFor(result)); } + + [TestMethod] + public void ASaveThatFailsPartWayLeavesTheEarlierOutputUntouched() + { + using TempDirectory temp = new(); + string output = temp.Combine("icon.png"); + byte[] earlier = [1, 2, 3, 4, 5]; + File.WriteAllBytes(output, earlier); + + Assert.ThrowsExactly(() => IconHelper.WriteReplacing(output, stream => + { + stream.Write(new byte[4096]); + stream.Flush(); + throw new IOException("No space left on device"); + })); + + CollectionAssert.AreEqual(earlier, File.ReadAllBytes(output), "A failed save must not truncate the earlier output."); + CollectionAssert.AreEqual(new[] { output }, Directory.GetFiles(temp.FullPath), "A failed save must leave no partial or temporary file behind."); + } + + [TestMethod] + public void ASaveThatFailsPartWayLeavesNothingWhenThereWasNoEarlierOutput() + { + using TempDirectory temp = new(); + string output = temp.Combine("icon.png"); + + Assert.ThrowsExactly(() => IconHelper.WriteReplacing(output, stream => + { + stream.Write(new byte[4096]); + throw new IOException("No space left on device"); + })); + + Assert.IsEmpty(Directory.GetFiles(temp.FullPath), "A failed save must leave no partial file under the output name."); + } + + [TestMethod] + public void ARerunReplacesTheEarlierOutputAndLeavesNoTemporaryFiles() + { + using TempDirectory temp = new(); + string input = temp.Combine("in"); + string output = temp.Combine("out"); + Directory.CreateDirectory(input); + Directory.CreateDirectory(output); + WritePng(Path.Combine(input, "icon.png"), 64); + File.WriteAllBytes(Path.Combine(output, "icon.png"), [1, 2, 3]); + + BatchResult result = IconHelper.ProcessDirectory(ArgumentsFor(input, output), NamedColors.White); + + Assert.AreEqual(1, result.Written); + CollectionAssert.AreEqual(new[] { Path.Combine(output, "icon.png") }, Directory.GetFiles(output)); + using Image written = Image.Load(Path.Combine(output, "icon.png")); + Assert.AreEqual(32, written.Width); + } } diff --git a/IconHelper/IconHelper.cs b/IconHelper/IconHelper.cs index eceabea..46114a8 100644 --- a/IconHelper/IconHelper.cs +++ b/IconHelper/IconHelper.cs @@ -170,7 +170,7 @@ internal static BatchResult ProcessDirectory(Arguments args, Color color) FileName outputFileName = FileName.Create(outputName); AbsoluteFilePath outputFilePath = outputDirectory / outputFileName; - image.SaveAsPng(outputFilePath, Encoder); + WriteReplacing(outputFilePath, stream => image.SaveAsPng(stream, Encoder)); written.Add(outputName, file); processed++; } @@ -188,6 +188,37 @@ internal static BatchResult ProcessDirectory(Arguments args, Color color) return new BatchResult(processed, failed); } + /// + /// Writes through a temporary file beside it, then moves that + /// file over the destination. A save that fails part-way, for example on a full disk, leaves no + /// partial file under the output name and keeps the output an earlier run wrote there. + /// + /// + /// The temporary file is not a .png, and Validate rejects an output folder that is the input + /// folder, so a file left behind by a crash is never picked up as an input. + /// + internal static void WriteReplacing(string outputFilePath, Action write) + { + Ensure.NotNull(outputFilePath); + Ensure.NotNull(write); + + string temporaryPath = $"{outputFilePath}.{Path.GetRandomFileName()}.tmp"; + try + { + using (FileStream stream = new(temporaryPath, FileMode.CreateNew, FileAccess.Write, FileShare.None)) + { + write(stream); + } + + File.Move(temporaryPath, outputFilePath, overwrite: true); + } + catch + { + File.Delete(temporaryPath); + throw; + } + } + /// /// Names that Finder and Explorer drop into any folder they open: dot-files such as .DS_Store and /// ._ AppleDouble files, plus Thumbs.db, ehthumbs.db and desktop.ini. They are not icons, so they From fc9c846e729922468ee65b44be332be8cde1abc9 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 11:33:08 +0000 Subject: [PATCH 2/2] Use Path.Join in the new rerun test, as the rest of the file does Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017vfJW5GEJNk1TZFgDdUPFX --- IconHelper.Test/ProcessDirectoryTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/IconHelper.Test/ProcessDirectoryTests.cs b/IconHelper.Test/ProcessDirectoryTests.cs index 3277992..08035c8 100644 --- a/IconHelper.Test/ProcessDirectoryTests.cs +++ b/IconHelper.Test/ProcessDirectoryTests.cs @@ -463,14 +463,14 @@ public void ARerunReplacesTheEarlierOutputAndLeavesNoTemporaryFiles() string output = temp.Combine("out"); Directory.CreateDirectory(input); Directory.CreateDirectory(output); - WritePng(Path.Combine(input, "icon.png"), 64); - File.WriteAllBytes(Path.Combine(output, "icon.png"), [1, 2, 3]); + WritePng(Path.Join(input, "icon.png"), 64); + File.WriteAllBytes(Path.Join(output, "icon.png"), [1, 2, 3]); BatchResult result = IconHelper.ProcessDirectory(ArgumentsFor(input, output), NamedColors.White); Assert.AreEqual(1, result.Written); - CollectionAssert.AreEqual(new[] { Path.Combine(output, "icon.png") }, Directory.GetFiles(output)); - using Image written = Image.Load(Path.Combine(output, "icon.png")); + CollectionAssert.AreEqual(new[] { Path.Join(output, "icon.png") }, Directory.GetFiles(output)); + using Image written = Image.Load(Path.Join(output, "icon.png")); Assert.AreEqual(32, written.Width); } }