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 94cab7a28e..daa0a8f757 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 @@ -163,7 +163,7 @@ internal class ItchInstaller( 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() + if (updating) ItchStaging.delete(work) work.mkdirs() if (resuming) clearPartialInstall() val exe = try { @@ -179,22 +179,22 @@ internal class ItchInstaller( val hint = filename.substringBeforeLast('.') StoreExe.pick(work, hint.ifBlank { null }, item.title) } catch (t: Throwable) { - if (updating) work.deleteRecursively() + if (updating) ItchStaging.delete(work) throw t } if (exe.isBlank()) { handle.log("itch.io: no exe found in ${folder.name}") - if (updating) work.deleteRecursively() + if (updating) ItchStaging.delete(work) return null } if (updating) { if (cancelled.get()) { - work.deleteRecursively() + ItchStaging.delete(work) return null } StoreInstalls.markUpdating(folder) ItchStaging.merge(work, folder) - work.deleteRecursively() + ItchStaging.delete(work) } return exe } 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 index b44201d8d2..df7258cb74 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchStaging.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchStaging.kt @@ -15,9 +15,14 @@ internal object ItchStaging { continue } if (Files.isSymbolicLink(destPath) || dest.exists()) { - if (dest.isDirectory && !Files.isSymbolicLink(destPath)) dest.deleteRecursively() else dest.delete() + delete(dest) } Files.move(child.toPath(), destPath) } } + + fun delete(file: File) { + if (!Files.isSymbolicLink(file.toPath()) && file.isDirectory) file.listFiles().orEmpty().forEach { delete(it) } + file.delete() + } } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchUpdates.kt b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchUpdates.kt index e6c3a7eeab..d255c790e4 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchUpdates.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/itch/ItchUpdates.kt @@ -34,6 +34,7 @@ internal object ItchUpdates { private val running = AtomicBoolean(false) private val restored = AtomicBoolean(false) + @Volatile private var generation = 0 internal fun evaluate(uploads: JSONArray?, uploadId: String, version: String): ItchUpdate? { if (uploads == null || version.isBlank()) return null @@ -66,8 +67,12 @@ internal object ItchUpdates { private fun restore(app: Context) { if (!restored.compareAndSet(false, true)) return + val started = generation val saved = decode(ItchPrefs.get(app).getString(KEY_FOUND, null)) - StoresState.post { for ((k, v) in saved) if (k !in available) available[k] = v } + StoresState.post { + if (started != generation) return@post + for ((k, v) in saved) if (k !in available) available[k] = v + } } private fun save(app: Context) { @@ -77,6 +82,7 @@ internal object ItchUpdates { fun check(context: Context, force: Boolean) { val app = context.applicationContext if (!running.compareAndSet(false, true)) return + val started = generation Thread({ try { restore(app) @@ -103,14 +109,16 @@ internal object ItchUpdates { } else failed = true StoresState.post { done = i + 1 } } + if (started != generation) return@Thread StoresState.post { + if (started != generation) return@post for (k in answered) { val u = found[k] if (u != null) available[k] = u else available.remove(k) } save(app) } - if (!failed) prefs.edit().putLong(KEY_AT, System.currentTimeMillis()).apply() + if (!failed && started == generation) prefs.edit().putLong(KEY_AT, System.currentTimeMillis()).apply() StoresState.logLine("itch.io: checked ${games.size} installed games, ${found.size} with updates") if (force) StoresState.post { val text = if (found.isNotEmpty()) app.getString(R.string.stores_itch_updates_found, found.size) else app.getString(R.string.upd_current_status) @@ -144,6 +152,7 @@ internal object ItchUpdates { fun clear(context: Context) { val app = context.applicationContext + generation++ ItchPrefs.get(app).edit().remove(KEY_AT).remove(KEY_FOUND).apply() StoresState.post { available.clear() } } 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 index c6363ce663..918c62d8f7 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/itch/ItchStagingTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/itch/ItchStagingTest.kt @@ -47,4 +47,28 @@ class ItchStagingTest { assertEquals("new", File(game, "b/inner.txt").readText()) assertFalse(File(game, "a/inner.txt").exists()) } + + @Test fun deletingFollowsNoLinkOutOfTheTree() { + val outside = tmp.newFolder("outside") + file(outside, "keep.txt", "keep") + val staged = tmp.newFolder("staged") + file(staged, "own.txt", "x") + java.nio.file.Files.createSymbolicLink(File(staged, "link").toPath(), outside.toPath()) + ItchStaging.delete(staged) + assertFalse(staged.exists()) + assertEquals("keep", File(outside, "keep.txt").readText()) + } + + @Test fun aLinkInTheGameIsReplacedNotFollowed() { + val outside = tmp.newFolder("outside") + file(outside, "keep.txt", "keep") + val game = tmp.newFolder("game") + java.nio.file.Files.createSymbolicLink(File(game, "data").toPath(), outside.toPath()) + val staged = tmp.newFolder("staged") + file(staged, "data/level.dat", "new") + ItchStaging.merge(staged, game) + assertEquals("keep", File(outside, "keep.txt").readText()) + assertFalse(File(outside, "level.dat").exists()) + assertEquals("new", File(game, "data/level.dat").readText()) + } }