From cb5687c7cd0bb79777bf4cfbdb46743cc2dab52e Mon Sep 17 00:00:00 2001 From: Anna Date: Sun, 8 Sep 2024 09:58:18 -0400 Subject: [PATCH] fix: delete hard link test files even on errors --- DownloadTask.cs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/DownloadTask.cs b/DownloadTask.cs index 129e0d9..f5c08fc 100644 --- a/DownloadTask.cs +++ b/DownloadTask.cs @@ -322,22 +322,34 @@ private void CreateDirectories() { } private async Task TestHardLinks() { + string? a = null; + string? b = null; + try { - var a = Path.Join(this.PenumbraModPath, Path.GetRandomFileName()); + a = Path.Join(this.PenumbraModPath, Path.GetRandomFileName()); await FileHelper.Create(a, true).DisposeAsync(); - var b = Path.Join(this.PenumbraModPath, Path.GetRandomFileName()); + b = Path.Join(this.PenumbraModPath, Path.GetRandomFileName()); FileHelper.CreateHardLink(a, b); this.SupportsHardLinks = true; - - try { - File.Delete(a); - File.Delete(b); - } catch (Exception ex) { - Plugin.Log.Warning(ex, "Could not delete temp files"); - } } catch (InvalidOperationException) { this.SupportsHardLinks = false; + } finally { + if (a != null) { + try { + File.Delete(a); + } catch (Exception ex) { + Plugin.Log.Warning(ex, "Could not delete temp files"); + } + } + + if (b != null) { + try { + File.Delete(b); + } catch (Exception ex) { + Plugin.Log.Warning(ex, "Could not delete temp files"); + } + } } }