From ab605dbbb4fa0478f895b2d6b71a6d58e962328a Mon Sep 17 00:00:00 2001 From: hawkeye116477 Date: Tue, 17 Sep 2024 13:45:40 +0200 Subject: [PATCH] Detect if file is really locked Sometimes lock file could remain despite no process is locking it. That's the reason we need to detect if it's really locked. #84 --- src/Helpers.cs | 14 ++++++++++++++ src/LegendaryDownloadManager.xaml.cs | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Helpers.cs b/src/Helpers.cs index 1d98288..2d57fef 100644 --- a/src/Helpers.cs +++ b/src/Helpers.cs @@ -135,5 +135,19 @@ public static double GetDouble(string value) return result; } + + public static bool IsFileLocked(string filePath) + { + try + { + using FileStream inputStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None); + inputStream.Close(); + } + catch (Exception) + { + return true; + } + return false; + } } } diff --git a/src/LegendaryDownloadManager.xaml.cs b/src/LegendaryDownloadManager.xaml.cs index 1bc68a1..26810d6 100644 --- a/src/LegendaryDownloadManager.xaml.cs +++ b/src/LegendaryDownloadManager.xaml.cs @@ -216,7 +216,8 @@ public void EnqueueMultipleJobs(List downloadManag public static async Task WaitUntilLegendaryCloses() { - if (File.Exists(Path.Combine(LegendaryLauncher.ConfigPath, "installed.json.lock"))) + var installedLockPath = Path.Combine(LegendaryLauncher.ConfigPath, "installed.json.lock"); + if (File.Exists(installedLockPath) && Helpers.IsFileLocked(installedLockPath)) { await Task.Delay(1000); await WaitUntilLegendaryCloses();