From 0b0b5e5ba2f27b5d89c8d19ee0d5557a3d59b841 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:50:23 +0000 Subject: [PATCH] fix(gog): count installer parts kept from a cancelled install as free space on retry --- .../stores/gog/GogDownloadManager.java | 14 ++++- .../stores/gog/GogInstallerPartsTest.kt | 55 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java index 5ac787e98b..271b6a9720 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java @@ -271,7 +271,7 @@ public final class GogDownloadManager { boolean sized = true; for (InstallerPart part : parts) { planned += part.size; if (part.size <= 0) sized = false; } long needed = installerBytesNeeded(knownPartBytes(parts), GogLibrary.INSTANCE.sizeOf(ctx, game.gameId)); - long usable = com.droiddeck.launcher.stores.StoreInstallRoot.availableBytes(installPath); + long usable = com.droiddeck.launcher.stores.StoreInstallRoot.availableBytes(installPath) + reclaimableInstallerBytes(installPath, parts); if (com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(needed, usable)) { throw new InstallException("Not enough free space: need " + formatBytes(needed) + ", only " + formatBytes(usable) + " free"); } @@ -340,6 +340,18 @@ public final class GogDownloadManager { return known; } + static long reclaimableInstallerBytes(File installPath, List parts) { + long reclaimed = 0; + for (InstallerPart part : parts) { + try { + File existing = installerDestination(installPath, part.filename); + if (existing.isFile()) reclaimed += existing.length(); + } catch (IOException ignored) { + } + } + return reclaimed; + } + static long installerBytesNeeded(long downloadBytes, long installedBytes) { return Math.max(0, downloadBytes) + Math.max(0, installedBytes); } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogInstallerPartsTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogInstallerPartsTest.kt index 6d9e8a4f9c..67194204b4 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogInstallerPartsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogInstallerPartsTest.kt @@ -121,6 +121,61 @@ class GogInstallerPartsTest { assertEquals(0L, GogDownloadManager.knownPartBytes(emptyList())) } + private fun installDir(): File = Files.createTempDirectory("gog-parts").toFile() + + @Test fun partsKeptFromACancelledInstallCountAsFreeSpaceOnRetry() { + val dir = installDir() + try { + File(dir, "setup.exe").writeBytes(ByteArray(1000)) + File(dir, "setup-1.bin").writeBytes(ByteArray(500)) + val parts = listOf( + GogDownloadManager.InstallerPart("u0", "setup.exe", 1000L), + GogDownloadManager.InstallerPart("u1", "setup-1.bin", 1000L), + ) + val reclaimed = GogDownloadManager.reclaimableInstallerBytes(dir, parts) + assertEquals(1500L, reclaimed) + val needed = GogDownloadManager.installerBytesNeeded(GogDownloadManager.knownPartBytes(parts), 0L) + val free = 600L + assertTrue(StoreInstallRoot.insufficientSpace(needed, free)) + assertFalse(StoreInstallRoot.insufficientSpace(needed, free + reclaimed)) + } finally { + dir.deleteRecursively() + } + } + + @Test fun aMissingPartOrADirectoryReclaimsNothing() { + val dir = installDir() + try { + File(dir, "folder.bin").mkdir() + val parts = listOf( + GogDownloadManager.InstallerPart("u0", "setup.exe", 1000L), + GogDownloadManager.InstallerPart("u1", "folder.bin", 1000L), + ) + assertEquals(0L, GogDownloadManager.reclaimableInstallerBytes(dir, parts)) + assertEquals(0L, GogDownloadManager.reclaimableInstallerBytes(dir, emptyList())) + } finally { + dir.deleteRecursively() + } + } + + @Test fun aLinkedPartOrAnEscapingNameReclaimsNothing() { + val dir = installDir() + val outside = installDir() + try { + val target = File(outside, "big.bin").apply { writeBytes(ByteArray(4000)) } + Files.createSymbolicLink(File(dir, "setup.exe").toPath(), target.toPath()) + File(dir.parentFile, "escape.bin").also { it.writeBytes(ByteArray(4000)); it.deleteOnExit() } + val parts = listOf( + GogDownloadManager.InstallerPart("u0", "setup.exe", 1000L), + GogDownloadManager.InstallerPart("u1", "../escape.bin", 1000L), + ) + assertEquals(0L, GogDownloadManager.reclaimableInstallerBytes(dir, parts)) + } finally { + dir.deleteRecursively() + outside.deleteRecursively() + } + } + @Test fun anotherPlatformOrNoDownloadsAddsNothing() { assertEquals(0, GogDownloadManager.installerParts(downloads("""{"installers":[{"os":"linux","manualUrl":"https://example.invalid/x"}]}"""), "1").size) assertEquals(0, GogDownloadManager.installerParts(downloads("{}"), "1").size)