fix: report archive storage accurately

This commit is contained in:
Kurt Himebauch committed 2026-10-28 17:37:20 -04:00
1 parent 8858285d13
commit 2229dbc0a8
4 files changed
+52 -2

No files matched your search

@@ -218,6 +218,7 @@ internal class ItchInstaller(
work = if (updating) File(folder, ItchStaging.DIR) else folder
if (updating) ItchStaging.delete(work)
work.mkdirs()
val availableBeforeExtraction = if (maintenanceTarget != null) StoreInstallRoot.availableBytes(folder) else StoreInstallRoot.SPACE_UNKNOWN
if (resuming) clearPartialInstall()
val exe = try {
when (val kind = sniff(archive, filename)) {
@@ -242,6 +243,17 @@ internal class ItchInstaller(
if (updating) ItchStaging.delete(work)
return null
}
if (maintenanceTarget != null) {
val stagedBytes = ItchStaging.regularFileBytes(work)
?: throw IllegalStateException("Could not measure the extracted itch.io archive")
val requiredBytes = ItchStaging.additionalBytesRequired(stagedBytes, backupExtraBytes = 0L)
handle.diskSize(requiredBytes)
if (StoreInstallRoot.insufficientSpace(requiredBytes, availableBeforeExtraction)) {
throw IllegalStateException(app.getString(R.string.stores_storage_insufficient,
com.droiddeck.launcher.stores.formatBytes(requiredBytes),
com.droiddeck.launcher.stores.formatBytes(availableBeforeExtraction)))
}
}
if (updating && maintenanceTarget != null) {
handle.interruptible(false)
synchronized(commit) {
@@ -87,8 +87,8 @@ internal object ItchMaintenanceAdapter : StoreMaintenanceAdapter {
else -> if (id == extra["uploadId"] && stamp == game.sidecar.installVersion) "current upload $id ($stamp)" else "current upload $id ($stamp); the installed upload is no longer available"
}
val size = target.optLong("size")
return MaintenanceProposal(game.folder, game.sidecar, label, action, size, size,
"Downloads and safely replaces files from one pinned itch.io archive upload.", target)
return MaintenanceProposal(game.folder, game.sidecar, label, action, size, MaintenanceProposal.UNKNOWN_BYTES,
"Downloads one pinned archive. Extracted size and required free space are unknown until download; DroidDeck checks the measured extracted payload before replacing files.", target)
}
override fun enqueue(context: Context, game: InstalledStoreGame, proposal: MaintenanceProposal) = enqueuePinned(context, game, proposal.target)
@@ -34,6 +34,35 @@ internal object ItchStaging {
visit(root, "")
}
fun regularFileBytes(root: File): Long? {
var total = 0L
var readable = true
fun visit(dir: File) {
if (!readable || Files.isSymbolicLink(dir.toPath())) return
val children = dir.listFiles() ?: run { readable = false; return }
for (child in children) {
if (Files.isSymbolicLink(child.toPath())) continue
if (child.isDirectory) visit(child)
else if (child.isFile) {
val length = runCatching { Files.size(child.toPath()) }.getOrNull()
if (length == null || Long.MAX_VALUE - total < length) {
readable = false
return
}
total += length
}
}
}
if (!root.isDirectory) return null
visit(root)
return total.takeIf { readable }
}
fun additionalBytesRequired(stagedBytes: Long, backupExtraBytes: Long): Long {
require(stagedBytes >= 0L && backupExtraBytes >= 0L)
return if (Long.MAX_VALUE - stagedBytes < backupExtraBytes) Long.MAX_VALUE else stagedBytes + backupExtraBytes
}
private fun protectedUserData(relative: String): Boolean = relative.split('/').any {
it.lowercase() in setOf("save", "saves", "userdata", "user data", "profiles")
} || relative.substringAfterLast('/').substringAfterLast('.', "").lowercase() in setOf("sav", "save")
@@ -237,6 +237,15 @@ class ItchStagingTest {
assertEquals("user settings", File(game, "profiles/settings.ini").readText())
}
@Test fun extractedPayloadAndBackupExtraSpaceAreAddedWithoutOverflow() {
val stage = tmp.newFolder("space-stage")
file(stage, "profiles/default.ini", "1234567890")
file(stage, "saves/default.sav", "")
assertEquals(10L, ItchStaging.regularFileBytes(stage))
assertEquals(38L, ItchStaging.additionalBytesRequired(10L, 28L))
assertEquals(Long.MAX_VALUE, ItchStaging.additionalBytesRequired(Long.MAX_VALUE - 1, 2))
}
@Test fun aDoneJournalEntryIsRehashedBeforeResumeSkipsIt() {
val game = tmp.newFolder("corrupt-resume-game")
val staged = tmp.newFolder("corrupt-resume-stage")