Skip to content

Commit

Permalink
Detect if file is really locked
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hawkeye116477 committed Sep 17, 2024
1 parent c10c18c commit ab605db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
3 changes: 2 additions & 1 deletion src/LegendaryDownloadManager.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ public void EnqueueMultipleJobs(List<DownloadManagerData.Download> 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();
Expand Down

0 comments on commit ab605db

Please sign in to comment.