fix(gog): count installer parts kept from a cancelled install as free space on retry

This commit is contained in:
Kurt Himebauch committed 2026-11-16 02:50:23 +00:00
1 parent 5926606f60
commit 0b0b5e5ba2
2 files changed
+68 -1

No files matched your search

@@ -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<InstallerPart> 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);
}
@@ -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)