From adf305ffc0e0124e06425f9f267eba062ccedc0c Mon Sep 17 00:00:00 2001 From: Matthew Crossman Date: Mon, 3 Jan 2022 19:20:00 +1100 Subject: [PATCH] Prevent DefaultInstaller from working on a non-existent cache file. Ensures that the cache file exists before attempting an install using it. This fixes a crash that can occur if the file is not yet available, likely due to an I/O-related race condition. --- .../droidify/installer/DefaultInstaller.kt | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt b/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt index d637b3a9..503421cf 100644 --- a/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt +++ b/src/main/kotlin/com/looker/droidify/installer/DefaultInstaller.kt @@ -51,16 +51,18 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) { val session = sessionInstaller.openSession(id) - session.use { activeSession -> - activeSession.openWrite("package", 0, cacheFile.length()).use { packageStream -> - cacheFile.inputStream().use { fileStream -> - fileStream.copyTo(packageStream) + if (cacheFile.exists()) { + session.use { activeSession -> + activeSession.openWrite("package", 0, cacheFile.length()).use { packageStream -> + cacheFile.inputStream().use { fileStream -> + fileStream.copyTo(packageStream) + } } + + val pendingIntent = PendingIntent.getService(context, id, intent, flags) + + session.commit(pendingIntent.intentSender) } - - val pendingIntent = PendingIntent.getService(context, id, intent, flags) - - session.commit(pendingIntent.intentSender) } cacheFile.delete() }