mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 21:35:59 +00:00
fix: serialize maintenance and uninstall
This commit is contained in:
1 parent
090b87c37c
commit
ef1221cf05
5 files changed
+150
-13
No files matched your search
@@ -282,7 +282,11 @@ object StoresState {
|
||||
fun uninstall(context: Context, game: InstalledStoreGame, onDone: () -> Unit = {}) {
|
||||
val app = context.applicationContext
|
||||
val key = "${game.sidecar.store.id}:${game.sidecar.id}"
|
||||
uninstalling[key] = 0f
|
||||
if (!com.droiddeck.launcher.stores.maintenance.StoreMaintenance.beginUninstall(game)) {
|
||||
android.widget.Toast.makeText(app, "Close, finish, or recover maintenance before uninstalling.", android.widget.Toast.LENGTH_LONG).show()
|
||||
onDone()
|
||||
return
|
||||
}
|
||||
background("stores-uninstall", onFailure = { main.post { uninstalling.remove(key); onDone() } }) {
|
||||
runCatching { backends[game.sidecar.store]?.uninstall(app, game) }
|
||||
if (game.sidecar.store == Store.BATTLENET && com.droiddeck.launcher.runtime.BattleNet.games(app).any { it.folder.absolutePath == game.folder.absolutePath }) {
|
||||
|
||||
@@ -72,6 +72,7 @@ object StoreMaintenance {
|
||||
private val revisions = ConcurrentHashMap<Store, Long>()
|
||||
private val accounts = ConcurrentHashMap<Store, String>()
|
||||
private val operationLocks = ConcurrentHashMap<String, Any>()
|
||||
private val preparing = ConcurrentHashMap.newKeySet<String>()
|
||||
val checks = mutableStateMapOf<String, MaintenanceCheck>()
|
||||
|
||||
fun register(adapter: StoreMaintenanceAdapter) {
|
||||
@@ -119,8 +120,17 @@ object StoreMaintenance {
|
||||
fun prepare(context: Context, game: InstalledStoreGame, action: MaintenanceAction): MaintenanceProposal {
|
||||
require(action in capabilities(game)) { "${game.sidecar.store.label} does not support ${action.name.lowercase()}" }
|
||||
require(action != MaintenanceAction.OPEN_CLIENT)
|
||||
return adapters[game.sidecar.store]?.prepare(context.applicationContext, game, action)
|
||||
?: error("${game.sidecar.store.label} maintenance is unavailable")
|
||||
val adapter = adapters[game.sidecar.store] ?: error("${game.sidecar.store.label} maintenance is unavailable")
|
||||
val folder = game.folder.canonicalFile
|
||||
synchronized(operationLock(folder)) {
|
||||
requireCurrentInstalledGame(game)
|
||||
check(preparing.add(folder.path)) { "${game.sidecar.store.label} maintenance is already being prepared" }
|
||||
}
|
||||
return try {
|
||||
adapter.prepare(context.applicationContext, game, action)
|
||||
} finally {
|
||||
synchronized(operationLock(folder)) { preparing.remove(folder.path) }
|
||||
}
|
||||
}
|
||||
|
||||
fun capabilities(game: InstalledStoreGame): Set<MaintenanceAction> =
|
||||
@@ -132,6 +142,8 @@ object StoreMaintenance {
|
||||
val adapter = adapters[game.sidecar.store] ?: error("${game.sidecar.store.label} maintenance is unavailable")
|
||||
val folder = game.folder.canonicalFile
|
||||
synchronized(operationLock(folder)) {
|
||||
requireCurrentInstalledGame(game)
|
||||
check(folder.path !in preparing) { "${game.sidecar.store.label} maintenance is being prepared" }
|
||||
require(!com.droiddeck.launcher.stores.download.DownloadQueue.isActive(game.key)) {
|
||||
"${game.sidecar.store.label} already has an active operation"
|
||||
}
|
||||
@@ -151,6 +163,22 @@ object StoreMaintenance {
|
||||
}
|
||||
}
|
||||
|
||||
fun beginUninstall(game: InstalledStoreGame): Boolean {
|
||||
val folder = game.folder.canonicalFile
|
||||
return synchronized(operationLock(folder)) {
|
||||
if (game.key in StoresState.uninstalling || folder.path in preparing) return@synchronized false
|
||||
val hasSharedApply = capabilities(game).any { it != MaintenanceAction.OPEN_CLIENT }
|
||||
val currentInstall = runCatching {
|
||||
if (hasSharedApply) requireCurrentInstalledGame(game)
|
||||
else require(folder.isDirectory && !Files.isSymbolicLink(folder.toPath()))
|
||||
readOperation(folder) == null
|
||||
}.getOrDefault(false)
|
||||
if (!currentInstall) return@synchronized false
|
||||
StoresState.uninstalling[game.key] = 0f
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fun resume(context: Context, game: InstalledStoreGame) {
|
||||
requireInstalledFolder(context, game.folder)
|
||||
val adapter = adapters[game.sidecar.store] ?: error("${game.sidecar.store.label} maintenance is unavailable")
|
||||
@@ -243,6 +271,18 @@ object StoreMaintenance {
|
||||
|
||||
private fun operationLock(folder: File): Any = operationLocks.computeIfAbsent(folder.path) { Any() }
|
||||
|
||||
private fun requireCurrentInstalledGame(game: InstalledStoreGame) {
|
||||
val folder = game.folder.canonicalFile
|
||||
require(game.sidecar.isInstalled && folder.isDirectory && !Files.isSymbolicLink(folder.toPath())) {
|
||||
"${game.sidecar.store.label} installation is no longer available"
|
||||
}
|
||||
val current = StoreGameSidecar.read(folder)
|
||||
require(current?.store == game.sidecar.store && current.id == game.sidecar.id && current.isInstalled) {
|
||||
"${game.sidecar.store.label} installation record is no longer available"
|
||||
}
|
||||
require(game.key !in StoresState.uninstalling) { "${game.sidecar.store.label} game is being uninstalled" }
|
||||
}
|
||||
|
||||
private fun clearNewQueuedOperation(game: InstalledStoreGame, enqueueId: String, preserveActive: Boolean) {
|
||||
val folder = game.folder.canonicalFile
|
||||
if (preserveActive && com.droiddeck.launcher.stores.download.DownloadQueue.isActive(game.key)) return
|
||||
|
||||
@@ -365,7 +365,7 @@ internal fun GameActions(
|
||||
}
|
||||
if (store == Store.ITCH && installed != null && StoreMaintenance.adapter(installed) == null) ItchUpdateButton(installed, download == null && StoresState.uninstalling[installed.key] == null)
|
||||
if (store == Store.GOG && installed != null) GogMaintenanceActions(installed)
|
||||
if (store != Store.GOG && installed != null && StoreMaintenance.adapter(installed) != null) StoreMaintenanceActions(installed)
|
||||
if (store != Store.GOG && installed != null && installed.key !in StoresState.uninstalling && StoreMaintenance.adapter(installed) != null) StoreMaintenanceActions(installed)
|
||||
if (store == null && game != null && game.source == Library.SOURCE_STEAM && game.appId > 0) SteamClientMaintenanceActions(a, s.sessionRunning)
|
||||
if (store == Store.XBOX && id != null) XboxInstalledStream(id, game?.name ?: item?.title.orEmpty(), item)
|
||||
if (store == Store.XBOX && installed != null && id != null) {
|
||||
|
||||
@@ -32,6 +32,7 @@ import com.droiddeck.launcher.stores.maintenance.MaintenanceRecovery
|
||||
import com.droiddeck.launcher.stores.maintenance.RecoveryState
|
||||
import com.droiddeck.launcher.stores.maintenance.StoreMaintenance
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@@ -81,9 +82,10 @@ internal fun StoreMaintenanceActions(game: InstalledStoreGame) {
|
||||
SecondaryButton("Resume", enabled = !busy, modifier = Modifier.testTag("store-resume")) {
|
||||
busy = true
|
||||
scope.launch {
|
||||
runCatching { withContext(Dispatchers.IO) { StoreMaintenance.resume(context, game) } }
|
||||
.onFailure { error = it.message }
|
||||
busy = false
|
||||
try {
|
||||
runCatchingCancellable { withContext(Dispatchers.IO) { StoreMaintenance.resume(context, game) } }
|
||||
.onFailure { error = it.message }
|
||||
} finally { busy = false }
|
||||
}
|
||||
}
|
||||
} else if (MaintenanceAction.UPDATE in capabilities && check?.state == MaintenanceCheckState.AVAILABLE) {
|
||||
@@ -170,9 +172,10 @@ internal fun StoreMaintenanceActions(game: InstalledStoreGame) {
|
||||
busy = true
|
||||
close()
|
||||
scope.launch {
|
||||
runCatching { withContext(Dispatchers.IO) { StoreMaintenance.enqueue(context, game, selected) } }
|
||||
.onFailure { error = it.message }
|
||||
busy = false
|
||||
try {
|
||||
runCatchingCancellable { withContext(Dispatchers.IO) { StoreMaintenance.enqueue(context, game, selected) } }
|
||||
.onFailure { error = it.message }
|
||||
} finally { busy = false }
|
||||
}
|
||||
}
|
||||
SecondaryButton("Cancel", onClick = close)
|
||||
@@ -192,12 +195,21 @@ private fun beginPrepare(
|
||||
) {
|
||||
setBusy(true)
|
||||
scope.launch {
|
||||
runCatching { withContext(Dispatchers.IO) { StoreMaintenance.prepare(context, game, action) } }
|
||||
.onSuccess(setProposal).onFailure { setError(it.message) }
|
||||
setBusy(false)
|
||||
try {
|
||||
runCatchingCancellable { withContext(Dispatchers.IO) { StoreMaintenance.prepare(context, game, action) } }
|
||||
.onSuccess(setProposal).onFailure { setError(it.message) }
|
||||
} finally { setBusy(false) }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun <T> runCatchingCancellable(block: suspend () -> T): Result<T> = try {
|
||||
Result.success(block())
|
||||
} catch (cancelled: CancellationException) {
|
||||
throw cancelled
|
||||
} catch (error: Throwable) {
|
||||
Result.failure(error)
|
||||
}
|
||||
|
||||
private fun MaintenanceAction.label(): String = when (this) {
|
||||
MaintenanceAction.UPDATE -> "Update"
|
||||
MaintenanceAction.REPAIR -> "Repair files"
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.droiddeck.launcher.stores.InstalledStoreGame
|
||||
import com.droiddeck.launcher.stores.Store
|
||||
import com.droiddeck.launcher.stores.StoreGameSidecar
|
||||
import com.droiddeck.launcher.stores.CatalogItem
|
||||
import com.droiddeck.launcher.stores.StoresState
|
||||
import com.droiddeck.launcher.stores.download.DownloadEntry
|
||||
import com.droiddeck.launcher.stores.download.DownloadQueue
|
||||
import com.droiddeck.launcher.stores.download.DownloadState
|
||||
@@ -24,7 +25,9 @@ import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import java.io.File
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33])
|
||||
@@ -198,6 +201,84 @@ class StoreMaintenanceTest {
|
||||
DownloadQueue.clear(app, key)
|
||||
}
|
||||
|
||||
@Test fun uninstallAdmissionAndMaintenanceAdmissionCannotOverlapOrRecreateADeletedInstall() {
|
||||
val app = RuntimeEnvironment.getApplication()
|
||||
val folder = temporary.newFolder().canonicalFile
|
||||
val sidecar = StoreGameSidecar(Store.EPIC, "uninstall-race", "Example", "bin/game.exe")
|
||||
sidecar.write(folder)
|
||||
val game = InstalledStoreGame(sidecar, folder)
|
||||
val proposal = MaintenanceProposal(folder, sidecar, "target", MaintenanceAction.UPDATE,
|
||||
target = JSONObject().put("schema", 1).put("build", "target"))
|
||||
StoreMaintenance.register(adapter)
|
||||
|
||||
assertTrue(StoreMaintenance.beginUninstall(game))
|
||||
assertTrue(game.key in StoresState.uninstalling)
|
||||
val prepareFailure = runCatching { StoreMaintenance.prepare(app, game, MaintenanceAction.UPDATE) }.exceptionOrNull()
|
||||
assertTrue(prepareFailure?.message.orEmpty().contains("being uninstalled"))
|
||||
assertTrue(runCatching { StoreMaintenance.enqueue(app, game, proposal) }.isFailure)
|
||||
assertNull(StoreMaintenance.readOperation(folder))
|
||||
|
||||
StoresState.uninstalling.remove(game.key)
|
||||
folder.deleteRecursively()
|
||||
assertTrue(runCatching { StoreMaintenance.enqueue(app, game, proposal) }.isFailure)
|
||||
assertFalse("stale proposals must not recreate an uninstalled game folder", folder.exists())
|
||||
}
|
||||
|
||||
@Test fun queuedMaintenanceBlocksUninstallAdmission() {
|
||||
val folder = temporary.newFolder().canonicalFile
|
||||
val sidecar = StoreGameSidecar(Store.EPIC, "maintenance-before-uninstall", "Example", "bin/game.exe")
|
||||
sidecar.write(folder)
|
||||
val game = InstalledStoreGame(sidecar, folder)
|
||||
StoreMaintenance.register(adapter)
|
||||
StoreMaintenance.enqueue(RuntimeEnvironment.getApplication(), game,
|
||||
MaintenanceProposal(folder, sidecar, "target", MaintenanceAction.UPDATE, target = JSONObject().put("schema", 1)))
|
||||
|
||||
assertFalse(StoreMaintenance.beginUninstall(game))
|
||||
assertFalse(game.key in StoresState.uninstalling)
|
||||
assertEquals("queued", StoreMaintenance.readOperation(folder)!!.getString("phase"))
|
||||
}
|
||||
|
||||
@Test fun inFlightMaintenancePreparationBlocksUninstallAdmission() {
|
||||
val app = RuntimeEnvironment.getApplication()
|
||||
val folder = temporary.newFolder().canonicalFile
|
||||
val sidecar = StoreGameSidecar(Store.EPIC, "prepare-uninstall-race", "Example", "bin/game.exe")
|
||||
sidecar.write(folder)
|
||||
val game = InstalledStoreGame(sidecar, folder)
|
||||
val proposal = MaintenanceProposal(folder, sidecar, "target", MaintenanceAction.UPDATE,
|
||||
target = JSONObject().put("schema", 1))
|
||||
val enteredPrepare = CountDownLatch(1)
|
||||
val finishPrepare = CountDownLatch(1)
|
||||
val failure = AtomicReference<Throwable?>()
|
||||
StoreMaintenance.register(object : StoreMaintenanceAdapter {
|
||||
override val store = Store.EPIC
|
||||
override fun capabilities(game: InstalledStoreGame) = setOf(MaintenanceAction.UPDATE)
|
||||
override fun check(context: Context, game: InstalledStoreGame) = MaintenanceCheck()
|
||||
override fun prepare(context: Context, game: InstalledStoreGame, action: MaintenanceAction): MaintenanceProposal {
|
||||
enteredPrepare.countDown()
|
||||
check(finishPrepare.await(5, TimeUnit.SECONDS))
|
||||
return proposal
|
||||
}
|
||||
override fun enqueue(context: Context, game: InstalledStoreGame, proposal: MaintenanceProposal) = true
|
||||
override fun resume(context: Context, game: InstalledStoreGame, operation: JSONObject) = Unit
|
||||
})
|
||||
val worker = Thread({ runCatching { StoreMaintenance.prepare(app, game, MaintenanceAction.UPDATE) }
|
||||
.onFailure(failure::set) }, "test-maintenance-prepare")
|
||||
worker.start()
|
||||
|
||||
assertTrue(enteredPrepare.await(5, TimeUnit.SECONDS))
|
||||
try {
|
||||
assertFalse(StoreMaintenance.beginUninstall(game))
|
||||
assertFalse(game.key in StoresState.uninstalling)
|
||||
} finally {
|
||||
finishPrepare.countDown()
|
||||
}
|
||||
worker.join(TimeUnit.SECONDS.toMillis(5))
|
||||
assertFalse("maintenance preparation should finish", worker.isAlive)
|
||||
assertNull(failure.get())
|
||||
assertTrue(StoreMaintenance.beginUninstall(game))
|
||||
StoresState.uninstalling.remove(game.key)
|
||||
}
|
||||
|
||||
@Test fun applyLeaseIsExclusiveAndMarksTheJournalBeforeMutation() {
|
||||
val folder = temporary.newFolder().canonicalFile
|
||||
val sidecar = StoreGameSidecar(Store.EPIC, "app", "Example", "bin/game.exe")
|
||||
|
||||
Reference in new issue
Block a user