Unpack itch updates into a staging folder before touching the game

This commit is contained in:
FX192 committed 2026-10-27 14:00:29 +03:00
1 parent 8a16bd3b3b
commit c90ad997b1
4 files changed
+122 -20

No files matched your search

@@ -33,6 +33,7 @@ internal class ItchInstaller(
private val folder: File,
) : DownloadQueue.DownloadJob {
private val cancelled = AtomicBoolean(false)
private var work: File = folder
override fun run(handle: DownloadQueue.JobHandle): String? {
val key = ItchAuth.key(app)
@@ -58,7 +59,6 @@ internal class ItchInstaller(
download(key, downloadKeyId, uploadId, upload.optString("updated_at", upload.optString("updatedAt")), archive, size, handle)
if (cancelled.get()) return null
handle.stage(DownloadStage.INSTALL)
if (StoreGameSidecar.read(folder)?.isInstalled == true) StoreInstalls.markUpdating(folder)
install(archive, filename, handle, resuming)
} catch (t: Throwable) {
if (!cancelled.get()) StoreInstalls.discard(app, folder, listOf(scratch))
@@ -160,23 +160,42 @@ internal class ItchInstaller(
}
private fun install(archive: File, filename: String, handle: DownloadQueue.JobHandle, resuming: Boolean): String? {
folder.mkdirs()
val existing = StoreGameSidecar.read(folder)
val updating = existing != null && (existing.isInstalled || existing.state == StoreGameSidecar.STATE_UPDATING)
work = if (updating) File(folder, ItchStaging.DIR) else folder
if (updating) work.deleteRecursively()
work.mkdirs()
if (resuming) clearPartialInstall()
when (val kind = sniff(archive, filename)) {
"zip" -> unzip(archive, handle)
"7z" -> sevenZip(archive, handle)
"rar" -> unrar(archive, handle)
"exe" -> copy(archive, File(folder, filename.ifBlank { "game.exe" }))
"tar" -> untar(archive, null, handle)
"gz", "bz2", "xz" -> untar(archive, kind, handle) ?: single(archive, kind, handle)
else -> copy(archive, File(folder, filename.ifBlank { "game" }))
val exe = try {
when (val kind = sniff(archive, filename)) {
"zip" -> unzip(archive, handle)
"7z" -> sevenZip(archive, handle)
"rar" -> unrar(archive, handle)
"exe" -> copy(archive, File(work, filename.ifBlank { "game.exe" }))
"tar" -> untar(archive, null, handle)
"gz", "bz2", "xz" -> untar(archive, kind, handle) ?: single(archive, kind, handle)
else -> copy(archive, File(work, filename.ifBlank { "game" }))
}
val hint = filename.substringBeforeLast('.')
StoreExe.pick(work, hint.ifBlank { null }, item.title)
} catch (t: Throwable) {
if (updating) work.deleteRecursively()
throw t
}
val hint = filename.substringBeforeLast('.')
val exe = StoreExe.pick(folder, hint.ifBlank { null }, item.title)
if (exe.isBlank()) {
handle.log("itch.io: no exe found in ${folder.name}")
if (updating) work.deleteRecursively()
return null
}
if (updating) {
if (cancelled.get()) {
work.deleteRecursively()
return null
}
StoreInstalls.markUpdating(folder)
ItchStaging.merge(work, folder)
work.deleteRecursively()
}
return exe
}
@@ -199,8 +218,8 @@ internal class ItchInstaller(
}
private fun target(name: String): File? {
val file = File(folder, name.replace('\\', '/'))
val root = folder.canonicalPath
val file = File(work, name.replace('\\', '/'))
val root = work.canonicalPath
val path = file.canonicalPath
return if (path == root || path.startsWith(root + File.separator)) file else null
}
@@ -208,8 +227,8 @@ internal class ItchInstaller(
// No JVM library reads RAR5; the runtime's bsdtar (libarchive) does.
private fun unrar(archive: File, handle: DownloadQueue.JobHandle) {
if (!LinuxRuntime.isInstalled(app)) throw IllegalStateException(app.getString(R.string.stores_itch_archive, "rar"))
val binds = if (listOf(app.filesDir, app.cacheDir).any { folder.absolutePath.startsWith(it.absolutePath + "/") }) null
else listOf(folder.absolutePath)
val binds = if (listOf(app.filesDir, app.cacheDir).any { work.absolutePath.startsWith(it.absolutePath + "/") }) null
else listOf(work.absolutePath)
fun bsdtar(args: List<String>, onLine: (String) -> Unit) =
GuestCommand.run(app, listOf("/usr/bin/bsdtar") + args, logName = "itch-unrar", extraBinds = binds, fastPath = true) { line ->
if (!line.startsWith("proot ")) onLine(line)
@@ -218,7 +237,7 @@ internal class ItchInstaller(
bsdtar(listOf("-tf", archive.absolutePath)) { entries++ }
var done = 0L
val tail = ArrayDeque<String>()
val code = bsdtar(listOf("-xvf", archive.absolutePath, "-C", folder.absolutePath)) { line ->
val code = bsdtar(listOf("-xvf", archive.absolutePath, "-C", work.absolutePath)) { line ->
if (line.startsWith("x ")) handle.stageProgress(++done, entries) else {
if (tail.size == 5) tail.removeFirst()
tail.addLast(line)
@@ -0,0 +1,23 @@
package com.droiddeck.launcher.stores.itch
import java.io.File
import java.nio.file.Files
internal object ItchStaging {
const val DIR = ".droiddeck-update"
fun merge(from: File, to: File) {
for (child in from.listFiles().orEmpty()) {
val dest = File(to, child.name)
val destPath = dest.toPath()
if (child.isDirectory && dest.isDirectory && !Files.isSymbolicLink(destPath)) {
merge(child, dest)
continue
}
if (Files.isSymbolicLink(destPath) || dest.exists()) {
if (dest.isDirectory && !Files.isSymbolicLink(destPath)) dest.deleteRecursively() else dest.delete()
}
Files.move(child.toPath(), destPath)
}
}
}