Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
feat(engines): lock a package while it is installed or removed
Yavuz Mete Afşar9e5cde10aee8
diff --git a/src/Filee.Engines/Infrastructure/EngineInstaller.cs b/src/Filee.Engines/Infrastructure/EngineInstaller.csindex 06c30281..4e4a7d86 100644--- a/src/Filee.Engines/Infrastructure/EngineInstaller.cs+++ b/src/Filee.Engines/Infrastructure/EngineInstaller.cs@@ -49,6 +49,12 @@ public enum EngineCopyStatus/// <param name="Outdated">Filee's download is older than the version engines.json pins.</param>public sealed record EngineCopy(EngineCopyStatus Status, string? Folder, bool FromSystem, bool Outdated);+/// <summary>Another Filee (the app, the CLI or a second copy) is installing or removing the same package.</summary>+public sealed class EngineBusyException(string packageId)+ : IOException($"{packageId} is being installed by another Filee. Try again when it has finished.")+{+ public string PackageId { get; } = packageId;+}/// <summary>Installs and removes <see cref="EnginePackage"/>s.</summary>public sealed class EngineInstaller@@ -118,6 +124,7 @@ public sealed class EngineInstallerif (package.Components.Any(c => !_components.ContainsKey(c)))throw new PlatformNotSupportedException(EngineDownloads.UnavailableReason(package)?? $"The {package.Id} package is unavailable on this platform.");+ using var packageLock = LockPackage(package);var pending = package.Components.Where(c => !string.Equals(Marker(c), _components[c].Sha256, StringComparison.OrdinalIgnoreCase)).Select(c => _components[c])@@ -153,6 +160,7 @@ public sealed class EngineInstaller/// <summary>Removes the package's folders. Fails if an engine is running (its files are locked).</summary>public void Uninstall(EnginePackage package){+ using var packageLock = LockPackage(package);foreach (var id in package.Components){var folder = FolderOf(id);@@ -161,6 +169,26 @@ public sealed class EngineInstaller}}+ /// <summary>+ /// Holds the package's lock file (.downloads/locks/<id>.lock) while it is installed or removed, so two Filee+ /// processes never write the same download or replace the same folder at once. The file stays; the open handle+ /// is the lock (deleting it on close would let a third process lock a new file while a second still holds the old).+ /// </summary>+ /// <exception cref="EngineBusyException">Another process holds the lock.</exception>+ private FileStream LockPackage(EnginePackage package)+ {+ var locks = Path.Combine(_root, ".downloads", "locks");+ Directory.CreateDirectory(locks);+ try+ {+ return new FileStream(Path.Combine(locks, package.Id + ".lock"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);+ }+ catch (IOException ex) when (ex is not (FileNotFoundException or DirectoryNotFoundException or PathTooLongException))+ {+ throw new EngineBusyException(package.Id);+ }+ }+// ───────────────────────── Download ─────────────────────────private async Task<string> DownloadAsync(EngineComponent component, Action<long> reportBytes, CancellationToken cancellationToken)diff --git a/tests/Filee.Engines.Tests/EngineInstallerTests.cs b/tests/Filee.Engines.Tests/EngineInstallerTests.csindex cce5d7f2..b56f9e5f 100644--- a/tests/Filee.Engines.Tests/EngineInstallerTests.cs+++ b/tests/Filee.Engines.Tests/EngineInstallerTests.cs@@ -133,6 +133,33 @@ public sealed class EngineInstallerTests : IDisposableAssert.False(Directory.Exists(Path.Combine(_root, "tool")));}+ [Fact]+ public async Task A_package_another_Filee_is_installing_is_neither_downloaded_nor_removed()+ {+ var archive = Zip(("tool.exe", "binary"));+ var component = Component("tool", "zip", archive);+ var server = new FakeServer(new() { [component.Url] = archive });+ var installer = Installer(server, component);+ var package = new EnginePackage("tool", ["tool"], 1000, ["tool"]);+ await installer.InstallAsync(package, null, TestContext.Current.CancellationToken);+ var update = component with { Version = "2.0", Sha256 = new string('1', 64) };+ var updating = Installer(server, update);++ var locks = Path.Combine(_root, ".downloads", "locks");+ using (new FileStream(Path.Combine(locks, "tool.lock"), FileMode.Open, FileAccess.ReadWrite, FileShare.None))+ {+ var busy = await Assert.ThrowsAsync<EngineBusyException>(() => updating.InstallAsync(package, null, TestContext.Current.CancellationToken));+ Assert.Equal("tool", busy.PackageId);+ Assert.Contains("another Filee", busy.Message);+ Assert.Throws<EngineBusyException>(() => installer.Uninstall(package));+ }++ Assert.Single(server.Requests);+ Assert.True(installer.IsUpToDate(package));+ installer.Uninstall(package);+ Assert.False(installer.IsInstalled(package));+ }+[Fact]public async Task The_copy_in_use_is_Filees_download_until_it_is_removed(){