diff --git a/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchInstaller.kt b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchInstaller.kt index 0d7949daeb..94cab7a28e 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchInstaller.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchInstaller.kt @@ -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, 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() - 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) diff --git a/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchStaging.kt b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchStaging.kt new file mode 100644 index 0000000000..27426650c0 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchStaging.kt @@ -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) + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/itch/ItchStagingTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/itch/ItchStagingTest.kt new file mode 100644 index 0000000000..c6363ce663 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/itch/ItchStagingTest.kt @@ -0,0 +1,50 @@ +package com.droiddeck.launcher.stores.itch + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File + +class ItchStagingTest { + @get:Rule val tmp = TemporaryFolder() + + private fun file(root: File, path: String, text: String) = File(root, path).also { it.parentFile?.mkdirs(); it.writeText(text) } + + @Test fun newFilesReplaceOldOnesAndEverythingElseStays() { + val game = tmp.newFolder("game") + val staged = tmp.newFolder("staged") + file(game, "game.exe", "old") + file(game, "data/level.dat", "old") + file(game, "saves/slot1.sav", "mine") + file(game, ".droiddeck-store.json", "record") + file(staged, "game.exe", "new") + file(staged, "data/level.dat", "new") + file(staged, "data/extra.dat", "new") + file(staged, "saves/default.sav", "new") + ItchStaging.merge(staged, game) + assertEquals("new", File(game, "game.exe").readText()) + assertEquals("new", File(game, "data/level.dat").readText()) + assertEquals("new", File(game, "data/extra.dat").readText()) + assertEquals("mine", File(game, "saves/slot1.sav").readText()) + assertEquals("new", File(game, "saves/default.sav").readText()) + assertEquals("record", File(game, ".droiddeck-store.json").readText()) + assertTrue(staged.listFiles().orEmpty().all { it.isDirectory && it.listFiles().orEmpty().isEmpty() }) + } + + @Test fun aFileReplacingADirectoryAndTheOtherWayRound() { + val game = tmp.newFolder("game") + val staged = tmp.newFolder("staged") + file(game, "a/inner.txt", "old") + file(game, "b", "old") + file(staged, "a", "new") + file(staged, "b/inner.txt", "new") + ItchStaging.merge(staged, game) + assertTrue(File(game, "a").isFile) + assertEquals("new", File(game, "a").readText()) + assertEquals("new", File(game, "b/inner.txt").readText()) + assertFalse(File(game, "a/inner.txt").exists()) + } +} diff --git a/docs/development/itch-updates.md b/docs/development/itch-updates.md index 13f7bdca7b..f2f4e49ce1 100644 --- a/docs/development/itch-updates.md +++ b/docs/development/itch-updates.md @@ -24,6 +24,16 @@ preferences until the update is installed or the account signs out. ## Installing The Update button reruns the normal install over the game's own folder, keeping its storage location. -For a replaced build the game page asks first and names the new file. The installer marks the install -record as updating before it touches files, so a failure leaves a resumable record, and the finished -record carries the new `installVersion` and `uploadId`. +For a replaced build the game page asks first and names the new file. + +An update never extracts into the live game. The download goes to the scratch folder, as for any +install, and the archive is unpacked into `.droiddeck-update` inside the game folder. The new build is +checked there (an executable must be found) before a single game file is touched. A failed or cancelled +download or extraction removes the staging folder and leaves the game as it was. + +Only then does `ItchStaging.merge` move the staged files into place, which are renames inside one +folder: a staged file replaces the file with the same path, directories are merged, and anything the new +build does not contain (saves, settings, the install record, launcher and art) stays. The install record +is marked updating just before the move, so an interruption in that short step is resumable, and the +finished record carries the new `installVersion` and `uploadId`. Free space for one extra copy of the +build is needed while the update runs.