diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt index 7099dd526..7aea1e8cf 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt @@ -71,10 +71,14 @@ object NativeGuest { ).filterValues { it.isNotEmpty() } } + /** Where guest directory [cwd] is, through the session's mounts ([binds], "src:dst"); the rootfs if it is nowhere. */ + fun workingDirectory(root: File, binds: List, cwd: String): File = + File(BwrapSpawner.hostPath(binds.map { BwrapSpawner.Bind.parse(it) }, root.path, cwd)).takeIf { it.isDirectory } ?: root + /** A process for [guest] (argv) started as proot would with [binds], in guest directory [cwd]. */ fun processBuilder(context: Context, root: File, binds: List, guest: List, cwd: String = "/root"): ProcessBuilder = ProcessBuilder(command(root, guest.first(), guest.drop(1))) - .directory(File(root, cwd.removePrefix("/")).takeIf { it.isDirectory } ?: root) + .directory(workingDirectory(root, binds, cwd)) .also { builder -> builder.environment().apply { remove("LD_AUDIT") diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt index 652589fe9..1fc7309b3 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt @@ -12,6 +12,19 @@ class NativeGuestTest { File(it, "usr/lib/ld-linux-aarch64.so.1").writeText("") } + @Test + fun aWorkingDirectoryInAMountIsItsSource() { + val root = Files.createTempDirectory("rootfs").toFile() + val storage = Files.createTempDirectory("storage").toFile() + File(root, "root").mkdirs() + // a mount spec is "src:dst"; Windows' drive letters cannot be written in one + org.junit.Assume.assumeFalse(storage.path.contains(':')) + val binds = listOf("${storage.path}:/root/Storage", "/proc") + assertEquals(storage, NativeGuest.workingDirectory(root, binds, "/root/Storage")) + assertEquals(File(root, "root"), NativeGuest.workingDirectory(root, binds, "/root")) + assertEquals(root, NativeGuest.workingDirectory(root, binds, "/nowhere")) + } + @Test fun commandRunsTheProgramThroughTheRootfsLoader() { val root = rootWithLoader()