Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

fix(assets): stop re-extracting the guest images on every start

ExTv5a8d5db03569
diff --git a/app/src/main/java/com/excp/podroid/PodroidApplication.kt b/app/src/main/java/com/excp/podroid/PodroidApplication.kt
index cd739ea..a0d91db 100644
--- a/app/src/main/java/com/excp/podroid/PodroidApplication.kt
+++ b/app/src/main/java/com/excp/podroid/PodroidApplication.kt
@@ -3,7 +3,7 @@
* Copyright (C) 2024-2026 Podroid contributors
*
* Application class — extracts QEMU, kernel, and initrd assets on first run
- * (and on app upgrade when an asset's size changes).
+ * (and on app upgrade when the install-time stamp drifts).
*/
package com.excp.podroid
@@ -96,14 +96,15 @@ class PodroidApplication : Application() {
// Fan out the four top-level extractions across a small thread pool.
// Disk-write throughput is the bottleneck for the squashfs (~225 MB),
- // but decompression, asset-FD lookup, and skip-when-size-matches all
- // overlap usefully across threads. Runs on a background coroutine
- // (not the main thread); the VM launch path awaits awaitAssetsReady.
+ // but decompression, asset-FD lookup, and the skip-when-already-
+ // complete check all overlap usefully across threads. Runs on a
+ // background coroutine (not the main thread); the VM launch path
+ // awaits awaitAssetsReady.
val tasks: List<() -> Unit> = listOf(
{ copyAssetDir("qemu", filesDir, forceCopy) },
- { copyAssetIfNeeded("vmlinuz-virt", filesDir, forceCopy) },
- { copyAssetIfNeeded("initrd.img", filesDir, forceCopy) },
- { copyAssetIfNeeded("alpine-rootfs.squashfs", filesDir, forceCopy) },
+ { copyAssetIfNeeded("vmlinuz-virt", File(filesDir, "vmlinuz-virt"), forceCopy) },
+ { copyAssetIfNeeded("initrd.img", File(filesDir, "initrd.img"), forceCopy) },
+ { copyAssetIfNeeded("alpine-rootfs.squashfs", File(filesDir, "alpine-rootfs.squashfs"), forceCopy) },
)
val pool = Executors.newFixedThreadPool(tasks.size.coerceAtMost(4))
var allSucceeded = true
@@ -116,8 +117,9 @@ class PodroidApplication : Application() {
})
for (f in futures) {
try { f.get() } catch (e: Exception) {
- // copyAssetIfNeeded / copyAssetFileIfNeeded already log
- // their own failures; this catches anything that escaped.
+ // copyAssetIfNeeded already logs its own failures and
+ // rethrows; this catches the propagated exception so
+ // one failed asset doesn't stop the others.
Log.w(TAG, "Asset extraction task failed", e)
allSucceeded = false
}
@@ -162,29 +164,35 @@ class PodroidApplication : Application() {
}
/**
- * Copies an asset to destDir if missing OR if the size differs OR if the
- * install-time stamp drifted. The stamp is the key bit: `mksquashfs` is
- * deterministic, so an upgrade can ship a same-size squashfs with
- * different content (e.g. an init.d script edited) — size-only checks
- * would silently keep the stale copy and the VM boots the old rootfs.
+ * Copies an asset to destFile unless it's already complete: destFile
+ * exists, forceCopy (install-stamp drift) is false, and either the size
+ * is unknown (assets.openFd() throws for the compressed assets shipped
+ * here: squashfs/initrd/kernel/qemu, so this is the common case) or it
+ * matches. Copies are atomic (tmp + fsync + rename), so an existing
+ * destination file is always complete, never partial. A stamp mismatch
+ * on upgrade is what forces a re-copy of same-size-but-different-content
+ * files (`mksquashfs` is deterministic, so size-only checks would
+ * silently keep a stale copy and the VM would boot the old rootfs).
+ * Exceptions from the copy propagate to the caller so the stamp is not
+ * committed after a failed extraction.
*/
- private fun copyAssetIfNeeded(assetName: String, destDir: File, forceCopy: Boolean) {
- val destFile = File(destDir, assetName)
- try {
- val assetSize = try { assets.openFd(assetName).use { it.length } } catch (_: Exception) { -1L }
- if (!forceCopy && assetSize >= 0 && destFile.exists() && destFile.length() == assetSize) return
+ private fun copyAssetIfNeeded(assetPath: String, destFile: File, forceCopy: Boolean) {
+ val assetSize = try { assets.openFd(assetPath).use { it.length } } catch (_: Exception) { -1L }
+ if (!forceCopy && destFile.exists() && (assetSize < 0 || destFile.length() == assetSize)) return
- destFile.parentFile?.mkdirs()
- copyAssetAtomically(assetName, destFile)
+ destFile.parentFile?.mkdirs()
+ try {
+ copyAssetAtomically(assetPath, destFile)
} catch (e: Exception) {
- Log.w(TAG, "Failed to extract $assetName", e)
+ Log.w(TAG, "Failed to extract $assetPath", e)
+ throw e
}
}
/**
- * Walks an asset directory tree and mirrors it under destDir.
- * Each file is copied if missing OR if its size differs OR if forceCopy
- * is true (install-stamp drift).
+ * Walks an asset directory tree and mirrors it under destDir, delegating
+ * each file to [copyAssetIfNeeded]. Exceptions from a file copy propagate
+ * up through this walk to the caller.
*/
private fun copyAssetDir(assetPath: String, destDir: File, forceCopy: Boolean) {
val entries = assets.list(assetPath) ?: return
@@ -196,22 +204,11 @@ class PodroidApplication : Application() {
dest.mkdirs()
copyAssetDir(src, dest, forceCopy)
} else {
- copyAssetFileIfNeeded(src, dest, forceCopy)
+ copyAssetIfNeeded(src, dest, forceCopy)
}
}
}
- private fun copyAssetFileIfNeeded(assetPath: String, destFile: File, forceCopy: Boolean) {
- try {
- val assetSize = try { assets.openFd(assetPath).use { it.length } } catch (_: Exception) { -1L }
- if (!forceCopy && assetSize >= 0 && destFile.exists() && destFile.length() == assetSize) return
-
- copyAssetAtomically(assetPath, destFile)
- } catch (e: Exception) {
- Log.w(TAG, "Failed to extract $assetPath", e)
- }
- }
-
/**
* Streams [assetPath] to `<destFile>.tmp`, fsyncs the data to disk, then
* atomically renames it onto [destFile]. The final canonical path therefore