fix: a Linux runtime update keeps imported apps and catalog AppImages (#1062)

* fix: a Linux runtime update keeps imported apps and catalog AppImages

The update carried only /root into the new tree and then discarded the
old one, so every imported AppImage, script app, Flatpak override and
catalog emulator AppImage was deleted. User-owned paths are now moved
across as a list, moved back if the carry or swap fails partway, and
recovered after an interrupted update.

* fix(runtime): retain user data when carry and recovery fail

* fix(runtime): mark staging carry start so interrupted extraction is discardable

* test: mark interrupted runtime data for recovery

* fix: make runtime data carry recoverable

* fix: the runtime update with carried user data builds again

carryUserData passed the de-duplicated Set of user paths where a List is expected after the two carry changes met; copy it into a List.

* fix: a runtime update stops instead of dropping app markers it cannot inspect, and keeps app names with line breaks

userPaths now uses the throwing exists() helper for the AppImage, menu entry and marker checks. The carry marker stores each path URL-encoded on its own line, so a name containing a newline cannot split into two paths. The symlink test now links a parent directory, which is what safePath rejects.

* fix: retain conflicting runtime recovery data on every retry

Stop on unreadable carry markers and keep rollback tests independent of add-on downloads.

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-10-25 12:57:04 -04:00
1 parent 0ad4b7cc14
commit 0de672f94e
5 files changed
+603 -14

No files matched your search

@@ -0,0 +1,140 @@
package com.droiddeck.launcher.runtime
import android.app.Application
import android.content.Context
import android.content.ContextWrapper
import com.droiddeck.launcher.BuildConfig
import com.droiddeck.launcher.core.AddonPack
import java.io.ByteArrayOutputStream
import java.io.File
import java.net.ServerSocket
import java.security.MessageDigest
import java.util.concurrent.atomic.AtomicBoolean
import java.util.function.Predicate
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class)
class RuntimeInstallRollbackTest {
@get:Rule val tmp = TemporaryFolder()
@After fun resetFaults() { RuntimeFileTree.refuseForTest = null }
private fun put(root: File, rel: String, data: String) = File(root, rel).apply {
checkNotNull(parentFile).mkdirs()
writeText(data)
}
private data class Attempt(val root: File, val staging: File, val old: File, val result: Boolean)
private fun attempt(failRollback: Boolean): Attempt {
val files = tmp.newFolder("files")
val cache = tmp.newFolder("cache")
val context = object : ContextWrapper(RuntimeEnvironment.getApplication()) {
override fun getFilesDir(): File = files
override fun getCacheDir(): File = cache
override fun getApplicationContext(): Context = this
}
for ((pack, pin, required) in listOf(
Triple(AddonPack.PROOT, BuildConfig.PACK_PROOT_SHA256, listOf("libproot.so", "libproot-loader.so")),
Triple(AddonPack.DIRECTAUDIO, BuildConfig.PACK_DIRECTAUDIO_SHA256,
listOf("linux-wine11/version.txt", "linux-wine11-systhread/version.txt")),
)) {
val version = pin.ifEmpty { "0".repeat(64) }
val directory = File(files, "addons/${pack.name}/$version")
required.forEach { put(directory, it, "test pack") }
put(directory, AddonPack.COMPLETE, version)
}
val root = LinuxRuntime.rootDir(context)
val staging = File(files, LinuxRuntime.DIR + ".new")
val old = File(files, LinuxRuntime.DIR + ".old")
put(root, "root/save.dat", "original user data")
put(root, "opt/appimages/user/game/app.AppImage", "original app")
put(root, "usr/bin/usable", "previous runtime")
val payload = ByteArrayOutputStream().also { bytes ->
ZstdCompressorOutputStream(bytes).use { zstd ->
TarArchiveOutputStream(zstd).use { tar ->
for ((name, value) in listOf("root/.bashrc" to "shipped home", "usr/bin/new" to "new runtime",
"opt/appimages/user/block" to "shipped app")) {
val data = value.toByteArray()
tar.putArchiveEntry(TarArchiveEntry(name).apply { size = data.size.toLong() })
tar.write(data)
tar.closeArchiveEntry()
}
}
}
}.toByteArray()
val digest = MessageDigest.getInstance("SHA-256").digest(payload).joinToString("") { "%02x".format(it) }
val faultSeen = AtomicBoolean()
RuntimeFileTree.refuseForTest = Predicate { path ->
if (path == File(staging, "opt/appimages/user/block").toPath() && faultSeen.compareAndSet(false, true)) {
if (failRollback) put(old, "root/rollback-block", "concurrent data")
true
} else failRollback && path == File(old, "root/rollback-block").toPath()
}
val server = ServerSocket(0)
val thread = Thread {
runCatching {
server.accept().use { socket ->
socket.soTimeout = 5000
val input = socket.getInputStream().bufferedReader(Charsets.ISO_8859_1)
while (true) {
val line = input.readLine() ?: break
if (line.isEmpty()) break
}
val out = socket.getOutputStream()
out.write(("HTTP/1.1 200 OK\r\nContent-Length: ${payload.size}\r\nConnection: close\r\n\r\n").toByteArray())
out.write(payload)
out.flush()
}
}
}.apply { isDaemon = true; start() }
try {
val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", digest, payload.size.toLong())
val result = LinuxRuntimeInstaller.install(context, release, null)
assertTrue("the intended carry fault was not reached", faultSeen.get())
return Attempt(root, staging, old, result)
} finally {
server.close()
thread.join(1000)
RuntimeFileTree.refuseForTest = null
}
}
@Test fun aFailedCarryAndFailedRollbackKeepTheOnlyStagedCopy() {
val attempt = attempt(failRollback = true)
assertFalse(attempt.result)
assertTrue("the only staged copy was deleted", File(attempt.staging, "root/save.dat").isFile)
assertEquals("original user data", File(attempt.staging, "root/save.dat").readText())
assertEquals("original app", File(attempt.root, "opt/appimages/user/game/app.AppImage").readText())
assertEquals("previous runtime", File(attempt.root, "usr/bin/usable").readText())
repeat(2) {
assertThrows(java.io.IOException::class.java) {
LinuxRuntimeInstaller.recoverInterruptedSwap(attempt.root, attempt.staging, attempt.old)
}
assertEquals("original user data", File(attempt.staging, "root/save.dat").readText())
}
}
@Test fun aCompleteRollbackStillRestoresAndCleansTheFailedCandidate() {
val attempt = attempt(failRollback = false)
assertFalse(attempt.result)
assertEquals("original user data", File(attempt.root, "root/save.dat").readText())
assertEquals("original app", File(attempt.root, "opt/appimages/user/game/app.AppImage").readText())
assertFalse(attempt.staging.exists())
}
}
@@ -69,6 +69,7 @@ class RuntimeRemovalTest {
val old = File(dir, "linuxfs.old").apply { File(this, "root").mkdirs() }
val staging = File(dir, "linuxfs.new").apply { File(this, "root").mkdirs() }
File(staging, "root/save").writeText("user data")
File(staging, LinuxRuntimeInstaller.CARRY_MARKER).writeText("1")
for (tree in listOf(old, staging))
Files.setPosixFilePermissions(tree.toPath(), PosixFilePermissions.fromString("r-xr-xr-x"))
LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old)
@@ -0,0 +1,243 @@
package com.droiddeck.launcher.runtime
import java.io.File
import java.io.IOException
import java.nio.file.Files
import org.junit.After
import org.junit.Assume.assumeTrue
import org.junit.Assert.assertThrows
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class RuntimeUserDataCarryTest {
@get:Rule val tmp = TemporaryFolder()
@After fun resetFaults() { RuntimeFileTree.refuseForTest = null }
private fun put(root: File, rel: String, text: String = rel) = File(root, rel).apply { parentFile.mkdirs(); writeText(text) }
private fun userTree(): File = tmp.newFolder("old").also { old ->
put(old, "root/save.dat", "mine")
put(old, "opt/appimages/user/app1/app.AppImage")
put(old, "opt/droiddeck-apps/game/run.sh")
put(old, "usr/local/share/applications/droiddeck-app-game.desktop")
put(old, "usr/local/share/applications/droiddeck-appimage-app1.desktop")
put(old, "usr/local/share/applications/other.desktop")
put(old, "opt/appimages/dolphin.AppImage")
put(old, "usr/share/applications/droiddeck-dolphin.desktop")
put(old, ".droiddeck-pkg-dolphin", "1.0")
put(old, "usr/bin/tool", "old system file")
}
@Test fun anUpdateKeepsImportedAppsCatalogAppImagesAndHome() {
val old = userTree()
val staging = tmp.newFolder("new").also { put(it, "root/.bashrc", "shipped"); put(it, "usr/bin/tool", "new system file") }
val moved = RuntimeFileTree.carryUserData(old, staging, false)
assertEquals("mine", File(staging, "root/save.dat").readText())
assertFalse("the shipped home is replaced, not merged", File(staging, "root/.bashrc").exists())
for (rel in listOf("opt/appimages/user/app1/app.AppImage", "opt/droiddeck-apps/game/run.sh",
"usr/local/share/applications/droiddeck-app-game.desktop", "usr/local/share/applications/droiddeck-appimage-app1.desktop",
"opt/appimages/dolphin.AppImage", "usr/share/applications/droiddeck-dolphin.desktop", ".droiddeck-pkg-dolphin")) {
assertTrue(rel, File(staging, rel).exists())
assertTrue(rel, rel in moved || moved.any { rel.startsWith("$it/") })
}
assertEquals("system files come from the new runtime", "new system file", File(staging, "usr/bin/tool").readText())
assertFalse("only DroidDeck's own menu entries are user data", File(staging, "usr/local/share/applications/other.desktop").exists())
}
@Test fun aFailureHalfwayPutsEverythingBack() {
val old = userTree()
val staging = tmp.newFolder("new")
put(staging, "usr/local", "a file where a folder is needed")
try {
RuntimeFileTree.carryUserData(old, staging, false)
fail("expected the carry to fail")
} catch (expected: IOException) {
}
assertEquals("mine", File(old, "root/save.dat").readText())
assertTrue(File(old, "opt/appimages/user/app1/app.AppImage").exists())
assertTrue(File(old, "opt/droiddeck-apps/game/run.sh").exists())
assertFalse(File(staging, "root").exists())
}
@Test fun recoveryOnlyFillsWhatIsMissing() {
val target = tmp.newFolder("target").also { put(it, "opt/droiddeck-apps/game/run.sh", "kept") }
val staging = tmp.newFolder("staging").also {
put(it, "opt/droiddeck-apps/game/run.sh", "stale")
put(it, "opt/appimages/user/app1/app.AppImage")
}
val moved = RuntimeFileTree.carryUserData(staging, target, true)
assertEquals(listOf("opt/appimages/user"), moved)
assertEquals("kept", File(target, "opt/droiddeck-apps/game/run.sh").readText())
assertTrue(File(target, "opt/appimages/user/app1/app.AppImage").exists())
}
@Test fun recoveryFindsCompanionsWhoseAppImageWasAlreadyPutBack() {
val root = tmp.newFolder("runtime")
val staging = tmp.newFolder("runtime.new")
val old = File(tmp.root, "runtime.old")
put(root, "opt/appimages/dolphin.AppImage", "user app")
put(staging, LinuxRuntimeInstaller.CARRY_MARKER, "1")
put(staging, "usr/share/applications/droiddeck-dolphin.desktop", "menu")
put(staging, ".droiddeck-pkg-dolphin", "version")
put(staging, ".droiddeck-pkg-desktop-kde", "system package marker")
LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old)
assertEquals("menu", File(root, "usr/share/applications/droiddeck-dolphin.desktop").readText())
assertEquals("version", File(root, ".droiddeck-pkg-dolphin").readText())
assertFalse(File(root, ".droiddeck-pkg-desktop-kde").exists())
assertEquals("user app", File(root, "opt/appimages/dolphin.AppImage").readText())
}
@Test fun unmarkedExtractedStagingDoesNotBlockRecovery() {
val root = tmp.newFolder("runtime")
val staging = tmp.newFolder("runtime.new")
val old = File(tmp.root, "runtime.old")
put(root, "root/save.dat", "current user data")
put(staging, "root/.bashrc", "shipped home from interrupted extract")
LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old)
assertEquals("current user data", File(root, "root/save.dat").readText())
assertEquals("shipped home from interrupted extract", File(staging, "root/.bashrc").readText())
}
@Test fun anUnreadableAppImageDirectoryAbortsBeforeMovingHome() {
assumeTrue(Files.getFileStore(tmp.root.toPath()).supportsFileAttributeView("posix"))
val old = userTree()
val images = File(old, "opt/appimages")
val staging = tmp.newFolder("staging")
assertTrue(images.setReadable(false, false))
assertTrue(images.setExecutable(false, false))
try {
assertThrows(IOException::class.java) { RuntimeFileTree.carryUserData(old, staging, false) }
assertEquals("mine", File(old, "root/save.dat").readText())
assertFalse(File(staging, "root/save.dat").exists())
} finally {
images.setReadable(true, true)
images.setExecutable(true, true)
}
}
@Test fun recoveryRestoresTheRuntimeBeforeTryingAFallibleHomeMove() {
val root = File(tmp.root, "runtime")
val old = tmp.newFolder("runtime.old")
val staging = tmp.newFolder("runtime.new")
put(old, "usr/bin/usable", "previous runtime")
File(old, "root").mkdirs()
put(staging, LinuxRuntimeInstaller.CARRY_MARKER, "1")
put(staging, "root/save.dat", "staged user data")
RuntimeFileTree.refuseForTest = java.util.function.Predicate { it.fileName.toString() == "root" }
assertThrows(IOException::class.java) { LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old) }
assertTrue("the previous runtime was not restored", root.isDirectory)
assertEquals("previous runtime", File(root, "usr/bin/usable").readText())
assertEquals("staged user data", File(staging, "root/save.dat").readText())
}
@Test fun conflictingRecoveryDataIsRetainedOnEveryRetry() {
val root = tmp.newFolder("runtime")
val staging = tmp.newFolder("runtime.new")
val old = File(tmp.root, "runtime.old")
put(root, "root/current.dat", "current user data")
put(staging, LinuxRuntimeInstaller.CARRY_MARKER, "1")
put(staging, "root/older.dat", "unrecovered user data")
repeat(2) {
assertThrows(IOException::class.java) { LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old) }
assertEquals("current user data", File(root, "root/current.dat").readText())
assertEquals("unrecovered user data", File(staging, "root/older.dat").readText())
}
}
@Test fun stagedDataWithoutASurvivingRuntimeIsKeptForRecovery() {
val root = File(tmp.root, "runtime")
val old = File(tmp.root, "runtime.old")
val staging = tmp.newFolder("runtime.new")
put(staging, LinuxRuntimeInstaller.CARRY_MARKER, "1")
put(staging, "root/save.dat", "only surviving user data")
assertThrows(IOException::class.java) { LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old) }
assertEquals("only surviving user data", File(staging, "root/save.dat").readText())
}
@Test fun conflictingIntendedHomesAreRetainedWithoutProofOfTheirContents() {
val root = File(tmp.root, "runtime")
val old = tmp.newFolder("runtime.old")
val staging = tmp.newFolder("runtime.new")
put(old, "root/save.dat", "user data")
put(staging, LinuxRuntimeInstaller.CARRY_MARKER, "root")
put(staging, "root/.bashrc", "shipped default")
assertThrows(IOException::class.java) { LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old) }
assertEquals("user data", File(root, "root/save.dat").readText())
assertEquals("shipped default", File(staging, "root/.bashrc").readText())
assertTrue(File(staging, LinuxRuntimeInstaller.CARRY_MARKER).exists())
}
@Test fun anUnreadableCarryMarkerStopsRecovery() {
val root = tmp.newFolder("runtime")
val staging = tmp.newFolder("runtime.new")
val old = File(tmp.root, "runtime.old")
val marker = File(staging, LinuxRuntimeInstaller.CARRY_MARKER).apply { mkdirs() }
assertThrows(IOException::class.java) { LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old) }
assertTrue(marker.isDirectory)
}
@Test fun recoveryMovesOnlyAnIntendedPathThatWasAlreadyCarried() {
val root = File(tmp.root, "runtime")
val old = tmp.newFolder("runtime.old")
val staging = tmp.newFolder("runtime.new")
put(staging, LinuxRuntimeInstaller.CARRY_MARKER, "root")
put(staging, "root/save.dat", "moved user data")
LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old)
assertEquals("moved user data", File(root, "root/save.dat").readText())
assertFalse(File(staging, LinuxRuntimeInstaller.CARRY_MARKER).exists())
}
@Test fun aSymlinkedParentCannotRedirectAUserDataMoveOutsideTheRuntime() {
val old = tmp.newFolder("old")
val staging = tmp.newFolder("staging")
val outside = tmp.newFolder("outside")
put(outside, "droiddeck-apps/game/run.sh", "outside data")
Files.createSymbolicLink(File(old, "opt").toPath(), outside.toPath())
assertThrows(IOException::class.java) { RuntimeFileTree.carryUserData(old, staging, false) }
assertEquals("outside data", File(outside, "droiddeck-apps/game/run.sh").readText())
assertFalse(File(staging, "opt/droiddeck-apps").exists())
}
@Test fun anInaccessibleLiveHomeIsNeverTreatedAsMissing() {
assumeTrue(Files.getFileStore(tmp.root.toPath()).supportsFileAttributeView("posix"))
val root = tmp.newFolder("runtime")
val staging = tmp.newFolder("runtime.new")
val old = File(tmp.root, "runtime.old")
put(root, "root/keep.dat", "current user data")
put(staging, LinuxRuntimeInstaller.CARRY_MARKER, "1")
put(staging, "root/staged.dat", "staged user data")
assertTrue(root.setExecutable(false, false))
val failure = try {
runCatching { LinuxRuntimeInstaller.recoverInterruptedSwap(root, staging, old) }.exceptionOrNull()
} finally {
root.setExecutable(true, true)
}
assertTrue("the existing live home was deleted", File(root, "root/keep.dat").isFile)
assertEquals("current user data", File(root, "root/keep.dat").readText())
assertEquals("staged user data", File(staging, "root/staged.dat").readText())
assertTrue("inaccessible data must abort recovery", failure is IOException)
}
}