Make staging cleanup symlink-safe and drop update checks that outlive a sign-out

This commit is contained in:
FX192 committed 2026-10-27 14:43:13 +03:00
1 parent e34db5adcb
commit efaf5bf9b7
4 files changed
+46 -8

No files matched your search

@@ -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
}
@@ -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()
}
}
@@ -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() }
}