more io exception fixes

This commit is contained in:
pointfeev 2023-01-29 20:30:05 -05:00
parent 3d63457a45
commit 36570420a4
3 changed files with 31 additions and 28 deletions

View file

@ -8,7 +8,6 @@ using System.Security.Cryptography;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using CreamInstaller.Forms;
using CreamInstaller.Utility; using CreamInstaller.Utility;
namespace CreamInstaller.Resources; namespace CreamInstaller.Resources;
@ -441,28 +440,32 @@ internal static class Resources
internal static void WriteManifestResource(this string resourceIdentifier, string filePath) internal static void WriteManifestResource(this string resourceIdentifier, string filePath)
{ {
using Stream resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("CreamInstaller.Resources." + resourceIdentifier); using Stream resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("CreamInstaller.Resources." + resourceIdentifier);
try while (!Program.Canceled)
{ try
using FileStream file = new(filePath, FileMode.Create, FileAccess.Write); {
resource?.CopyTo(file); using FileStream file = new(filePath, FileMode.Create, FileAccess.Write);
} resource?.CopyTo(file);
#if DEBUG }
catch (Exception e) catch
{ {
DebugForm.Current.Log("resource write exception for '" + resourceIdentifier + "' to '" + filePath + "': " + e); if (filePath.IOWarn("Failed to write a crucial manifest resource") is not DialogResult.OK)
} break;
#else }
catch
{
//ignored
}
#endif
} }
internal static void WriteResource(this byte[] resource, string filePath) internal static void WriteResource(this byte[] resource, string filePath)
{ {
using FileStream fileStream = new(filePath, FileMode.Create, FileAccess.Write); while (!Program.Canceled)
fileStream.Write(resource); 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) internal static bool IsFilePathLocked(this string filePath)

View file

@ -45,7 +45,7 @@ internal static class ExceptionHandler
if (string.IsNullOrWhiteSpace(outputString)) if (string.IsNullOrWhiteSpace(outputString))
outputString = e?.ToString() ?? "Unknown exception"; outputString = e?.ToString() ?? "Unknown exception";
using DialogForm dialogForm = new(form ?? Form.ActiveForm); 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) internal static void HandleFatalException(this Exception e)

View file

@ -13,7 +13,7 @@ internal static class SafeIO
while (!Program.Canceled) while (!Program.Canceled)
{ {
bool exists = File.Exists(filePath); 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 exists;
} }
return false; return false;
@ -29,7 +29,7 @@ internal static class SafeIO
} }
catch 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; break;
} }
} }
@ -44,7 +44,7 @@ internal static class SafeIO
} }
catch 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; break;
} }
} }
@ -61,7 +61,7 @@ internal static class SafeIO
} }
catch 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; break;
} }
} }
@ -75,7 +75,7 @@ internal static class SafeIO
} }
catch 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; break;
} }
return null; return null;
@ -90,7 +90,7 @@ internal static class SafeIO
} }
catch 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; break;
} }
return null; return null;
@ -106,12 +106,12 @@ internal static class SafeIO
} }
catch 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; 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); using DialogForm dialogForm = new(form ?? Form.ActiveForm);
return dialogForm.Show(SystemIcons.Warning, message + ": " + filePath.BeautifyPath(), "Retry", "OK"); return dialogForm.Show(SystemIcons.Warning, message + ": " + filePath.BeautifyPath(), "Retry", "OK");