From 36570420a4cb4ce9e34f110c734ba2c203bd01e4 Mon Sep 17 00:00:00 2001 From: pointfeev Date: Sun, 29 Jan 2023 20:30:05 -0500 Subject: [PATCH] more io exception fixes --- CreamInstaller/Resources/Resources.cs | 41 ++++++++++++---------- CreamInstaller/Utility/ExceptionHandler.cs | 2 +- CreamInstaller/Utility/SafeIO.cs | 16 ++++----- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/CreamInstaller/Resources/Resources.cs b/CreamInstaller/Resources/Resources.cs index a0ef27e..b03ec93 100644 --- a/CreamInstaller/Resources/Resources.cs +++ b/CreamInstaller/Resources/Resources.cs @@ -8,7 +8,6 @@ using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; -using CreamInstaller.Forms; using CreamInstaller.Utility; namespace CreamInstaller.Resources; @@ -441,28 +440,32 @@ internal static class Resources internal static void WriteManifestResource(this string resourceIdentifier, string filePath) { using Stream resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("CreamInstaller.Resources." + resourceIdentifier); - try - { - using FileStream file = new(filePath, FileMode.Create, FileAccess.Write); - resource?.CopyTo(file); - } -#if DEBUG - catch (Exception e) - { - DebugForm.Current.Log("resource write exception for '" + resourceIdentifier + "' to '" + filePath + "': " + e); - } -#else - catch - { - //ignored - } -#endif + while (!Program.Canceled) + try + { + using FileStream file = new(filePath, FileMode.Create, FileAccess.Write); + resource?.CopyTo(file); + } + catch + { + if (filePath.IOWarn("Failed to write a crucial manifest resource") is not DialogResult.OK) + break; + } } internal static void WriteResource(this byte[] resource, string filePath) { - using FileStream fileStream = new(filePath, FileMode.Create, FileAccess.Write); - fileStream.Write(resource); + while (!Program.Canceled) + try + { + using FileStream fileStream = new(filePath, FileMode.Create, FileAccess.Write); + fileStream.Write(resource); + } + catch + { + if (filePath.IOWarn("Failed to write a crucial resource") is not DialogResult.OK) + break; + } } internal static bool IsFilePathLocked(this string filePath) diff --git a/CreamInstaller/Utility/ExceptionHandler.cs b/CreamInstaller/Utility/ExceptionHandler.cs index 1bc4c12..6e7db74 100644 --- a/CreamInstaller/Utility/ExceptionHandler.cs +++ b/CreamInstaller/Utility/ExceptionHandler.cs @@ -45,7 +45,7 @@ internal static class ExceptionHandler if (string.IsNullOrWhiteSpace(outputString)) outputString = e?.ToString() ?? "Unknown exception"; using DialogForm dialogForm = new(form ?? Form.ActiveForm); - return dialogForm.Show(SystemIcons.Error, outputString, acceptButtonText, cancelButtonText, caption) == DialogResult.OK; + return dialogForm.Show(SystemIcons.Error, outputString, acceptButtonText, cancelButtonText, caption) is DialogResult.OK; } internal static void HandleFatalException(this Exception e) diff --git a/CreamInstaller/Utility/SafeIO.cs b/CreamInstaller/Utility/SafeIO.cs index 27b9353..60a2b82 100644 --- a/CreamInstaller/Utility/SafeIO.cs +++ b/CreamInstaller/Utility/SafeIO.cs @@ -13,7 +13,7 @@ internal static class SafeIO while (!Program.Canceled) { bool exists = File.Exists(filePath); - if (exists || !crucial || filePath.Warn("Failed to find a crucial file", form) is not DialogResult.OK) + if (exists || !crucial || filePath.IOWarn("Failed to find a crucial file", form) is not DialogResult.OK) return exists; } return false; @@ -29,7 +29,7 @@ internal static class SafeIO } catch { - if (!crucial || filePath.Warn("Failed to create a crucial file", form) is not DialogResult.OK) + if (!crucial || filePath.IOWarn("Failed to create a crucial file", form) is not DialogResult.OK) break; } } @@ -44,7 +44,7 @@ internal static class SafeIO } catch { - if (!crucial || !filePath.Exists(true) || filePath.Warn("Failed to move a crucial file", form) is not DialogResult.OK) + if (!crucial || !filePath.Exists(true) || filePath.IOWarn("Failed to move a crucial file", form) is not DialogResult.OK) break; } } @@ -61,7 +61,7 @@ internal static class SafeIO } catch { - if (!crucial || filePath.Warn("Failed to delete a crucial file", form) is not DialogResult.OK) + if (!crucial || filePath.IOWarn("Failed to delete a crucial file", form) is not DialogResult.OK) break; } } @@ -75,7 +75,7 @@ internal static class SafeIO } catch { - if (!crucial || !filePath.Exists(true) || filePath.Warn("Failed to read a crucial file", form) is not DialogResult.OK) + if (!crucial || !filePath.Exists(true) || filePath.IOWarn("Failed to read a crucial file", form) is not DialogResult.OK) break; } return null; @@ -90,7 +90,7 @@ internal static class SafeIO } catch { - if (!crucial || !filePath.Exists(true) || filePath.Warn("Failed to read a crucial file", form) is not DialogResult.OK) + if (!crucial || !filePath.Exists(true) || filePath.IOWarn("Failed to read a crucial file", form) is not DialogResult.OK) break; } return null; @@ -106,12 +106,12 @@ internal static class SafeIO } catch { - if (!crucial || filePath.Warn("Failed to write a crucial file", form) is not DialogResult.OK) + if (!crucial || filePath.IOWarn("Failed to write a crucial file", form) is not DialogResult.OK) break; } } - private static DialogResult Warn(this string filePath, string message, Form form = null) + internal static DialogResult IOWarn(this string filePath, string message, Form form = null) { using DialogForm dialogForm = new(form ?? Form.ActiveForm); return dialogForm.Show(SystemIcons.Warning, message + ": " + filePath.BeautifyPath(), "Retry", "OK");