fix(appimage): catalog AppImages start without proot (#3077)

* fix(appimage): catalog AppImages start without proot

* fix(appimage): cache cleanup does not follow symlinked entries

* fix(appimage): keep unpacked copies in use and track subsecond changes

* fix(appimage): extract native images through an open descriptor

---------

Co-authored-by: maxjivi05 <maxjividen@gmail.com>
This commit is contained in:
The412Bannerandmaxjivi05 authored and GitHub committed 2026-11-24 09:37:09 -05:00
1 parent 39debda0b0
commit cf44aaed14
4 files changed
+246 -5

No files matched your search

+67
View File
@@ -1023,6 +1023,73 @@ class ExtractTest(FexTestCase):
self.assertIn("-no-xattrs", commands[-1])
self.assertTrue(Path("squashfs-root/AppRun").is_file())
def seven_zip(self, tool, which, works=True):
commands = []
def call(argv, **kwargs):
commands.append(argv)
if argv[0] != "/usr/bin/7z" or not works:
return 2
(self.work / "squashfs-root").mkdir()
(self.work / "squashfs-root/AppRun").touch()
return 0
with mock.patch.dict(G, {
"URUNTIME": tool,
"prepare": lambda: None,
"fex_install": lambda: None,
"steam_running": lambda: False,
"want_fex": lambda: None,
}), mock.patch("shutil.which", side_effect=lambda name: which.get(name)), \
mock.patch("subprocess.call", side_effect=call):
status, said = self.quiet(FEX["extract"], "image.AppImage")
return status, said, commands
def test_without_proot_the_shipped_runtime_reads_the_open_image(self):
self.write(self.image, elf(183) + b"payload", 0o755)
tool = self.uruntime('cmp "$TARGET_APPIMAGE" "%s" || exit 9\nmkdir -p AppDir && touch AppDir/AppRun\n' % self.image)
with mock.patch.dict(os.environ, {"DDNATV_ROOT": str(self.tmp / "rootfs")}):
status, said, commands = self.seven_zip(tool, {"7z": "/usr/bin/7z"})
self.assertEqual((status, said), (0, ""))
self.assertEqual([], commands)
self.assertTrue(Path("squashfs-root/AppRun").is_file())
def test_without_proot_runtime_failure_falls_back_to_7zip(self):
self.write(self.image, elf(183) + b"payload", 0o755)
tool = self.uruntime("exit 1\n")
with mock.patch.dict(os.environ, {"DDNATV_ROOT": str(self.tmp / "rootfs")}):
status, said, commands = self.seven_zip(tool, {"7z": "/usr/bin/7z"})
self.assertEqual(status, 0)
self.assertIn("uruntime could not extract it (status 1)", said)
self.assertEqual("/usr/bin/7z", commands[-1][0])
self.assertTrue(Path("squashfs-root/AppRun").is_file())
def test_with_proot_the_shipped_runtime_is_still_tried_first(self):
ran = self.tmp / "uruntime-ran"
tool = self.uruntime("touch %s\nexit 1\n" % ran)
env = {k: v for k, v in os.environ.items() if k != "DDNATV_ROOT"}
with mock.patch.dict(os.environ, env, clear=True):
status, said, commands = self.seven_zip(tool, {"7z": "/usr/bin/7z"})
self.assertEqual(status, 0)
self.assertTrue(ran.exists())
self.assertIn("uruntime could not extract it (status 1)", said)
self.assertEqual("/usr/bin/7z", commands[-1][0])
def test_unsquashfs_is_preferred_over_7zip(self):
tool = self.uruntime("exit 1\n")
status, said, commands = self.seven_zip(tool, {"unsquashfs": "/usr/bin/unsquashfs", "7z": "/usr/bin/7z"})
self.assertEqual(status, 0)
self.assertIn("unsquashfs could not extract it (status 2)", said)
self.assertEqual(["/usr/bin/unsquashfs", "/usr/bin/7z"], [c[0] for c in commands])
def test_a_7zip_failure_leaves_nothing_unpacked(self):
tool = self.uruntime("exit 1\n")
status, said, _ = self.seven_zip(tool, {"7z": "/usr/bin/7z"}, works=False)
self.assertEqual(status, 1)
self.assertIn("7-Zip could not extract it (status 2)", said)
self.assertNotIn("no unsquashfs", said)
self.assertFalse(os.path.lexists("squashfs-root"))
class LaunchTest(FexTestCase):
def test_the_guest_environment(self):
+128 -2
View File
@@ -1,3 +1,4 @@
import fcntl
import os
import re
import shutil
@@ -37,13 +38,28 @@ class AppImageExecTest(unittest.TestCase):
path.chmod(0o755)
return path
def launch(self):
def launch(self, **extra):
script = self.dir / "exec"
script.write_text(EXEC.read_text().replace(URUNTIME, str(self.uruntime)).replace(FEX, str(self.fex)))
env = {k: v for k, v in os.environ.items() if k not in ("TARGET_APPIMAGE", "APPIMAGE_EXTRACT_AND_RUN")}
env = {k: v for k, v in os.environ.items() if k not in ("TARGET_APPIMAGE", "APPIMAGE_EXTRACT_AND_RUN", "DDNATV_ROOT")}
env.update(extra)
return subprocess.run(["bash", str(script), str(self.image), "--no-gui"], env=env,
capture_output=True, text=True, timeout=30).stdout.splitlines()
def native(self, **extra):
return self.launch(DDNATV_ROOT=str(self.dir / "rootfs"), XDG_CACHE_HOME=str(self.cache), **extra)
def unpacker(self, works=True, apprun=None):
self.cache = self.dir / "cache"
self.unpacks = self.dir / "unpacks"
apprun = apprun or "#!/bin/sh\nprintf '%s\\n' \"APPIMAGE=$APPIMAGE APPDIR=$APPDIR TARGET=${TARGET_APPIMAGE:-}\" apprun \"$@\"\n"
self.fex.write_text(
"#!/bin/sh\n[ \"$1\" = extract ] || exit 9\necho \"$2\" >> %s\n" % self.unpacks +
("mkdir -p squashfs-root\ncat > squashfs-root/AppRun <<'EOF'\n%sEOF\nchmod +x squashfs-root/AppRun\n" % apprun if works else "exit 1\n"))
def unpacked_dirs(self):
return [d for d in self.cache.glob("droiddeck-appimage/*") if d.is_dir()]
def test_an_arm64_image_runs_through_the_shipped_runtime(self):
self.assertEqual(["TARGET=%s EXTRACT=1" % self.image, "uruntime", "--no-gui"], self.launch())
@@ -61,6 +77,116 @@ class AppImageExecTest(unittest.TestCase):
self.fex.unlink()
self.assertEqual(["TARGET=%s EXTRACT=1" % self.image, "uruntime", "--no-gui"], self.launch())
def test_without_proot_an_arm64_image_is_unpacked_once_and_runs_from_its_apprun(self):
self.unpacker()
expected = ["== appimage: unpacking %s" % self.image]
ran = ["APPIMAGE=%s APPDIR=%s/squashfs-root TARGET=" % (self.image, "%s"), "apprun", "--no-gui"]
first = self.native()
self.assertEqual(expected, first[:1])
[unpacked] = self.unpacked_dirs()
ran[0] = ran[0] % unpacked
self.assertEqual(ran, first[1:])
self.assertEqual(ran, self.native())
self.assertEqual([str(self.image)], self.unpacks.read_text().splitlines())
def test_without_proot_a_changed_image_is_unpacked_again(self):
self.unpacker()
self.native()
os.utime(self.image, (1, 1))
self.assertEqual("apprun", self.native()[2])
self.assertEqual(2, len(self.unpacks.read_text().splitlines()))
self.assertEqual(1, len(self.unpacked_dirs()))
def test_without_proot_a_same_size_change_within_one_second_is_unpacked_again(self):
self.unpacker()
os.utime(self.image, ns=(1_000_000_100_000_000, 1_000_000_100_000_000))
self.native()
os.utime(self.image, ns=(1_000_000_900_000_000, 1_000_000_900_000_000))
self.assertEqual("apprun", self.native()[2])
self.assertEqual(2, len(self.unpacks.read_text().splitlines()))
self.assertEqual(1, len(self.unpacked_dirs()))
def hold(self, unpacked):
handle = open(unpacked / "source")
self.addCleanup(handle.close)
fcntl.flock(handle, fcntl.LOCK_SH)
return handle
def test_without_proot_a_running_copy_survives_an_update_until_it_exits(self):
self.unpacker()
self.native()
[old] = self.unpacked_dirs()
running = self.hold(old)
os.utime(self.image, (1, 1))
self.assertEqual("apprun", self.native()[2])
self.assertTrue((old / "squashfs-root/AppRun").exists())
[new] = [d for d in self.unpacked_dirs() if d != old]
running.close()
self.assertEqual("apprun", self.native()[1])
self.assertEqual([new], self.unpacked_dirs())
def test_without_proot_a_running_copy_of_a_removed_image_is_kept(self):
self.unpacker()
self.native()
[old] = self.unpacked_dirs()
self.hold(old)
self.image.unlink()
self.image = self.dir / "dolphin.AppImage"
self.machine(183)
self.native()
self.assertTrue((old / "squashfs-root/AppRun").exists())
self.assertEqual(2, len(self.unpacked_dirs()))
def test_without_proot_the_running_application_holds_its_copy(self):
self.unpacker(apprun="#!/bin/sh\nflock -n -x \"$APPDIR/../source\" true && echo free || echo held\n")
self.assertEqual(["held"], self.native()[1:])
def test_without_proot_nothing_runs_when_the_cache_cannot_be_locked(self):
self.unpacker()
tools = self.dir / "tools"
tools.mkdir()
(tools / "flock").write_text("#!/bin/sh\nexit 1\n")
(tools / "flock").chmod(0o755)
self.assertEqual([], self.native(PATH="%s:%s" % (tools, os.environ["PATH"])))
self.assertFalse(self.unpacks.exists())
def test_without_proot_the_unpacked_copy_of_a_removed_image_is_dropped(self):
self.unpacker()
self.native()
[old] = self.unpacked_dirs()
self.image.unlink()
self.image = self.dir / "dolphin.AppImage"
self.machine(183)
self.native()
self.assertFalse(old.exists())
self.assertEqual(1, len(self.unpacked_dirs()))
def test_without_proot_a_linked_cache_entry_and_its_target_survive(self):
self.unpacker()
outside = self.dir / "outside"
outside.mkdir()
(outside / "source").write_text("%s\n" % (self.dir / "gone.AppImage"))
(outside / "keep").write_text("data")
link = self.cache / "droiddeck-appimage" / "linked"
link.parent.mkdir(parents=True)
link.symlink_to(outside, target_is_directory=True)
self.assertEqual("apprun", self.native()[2])
self.assertTrue(link.is_symlink())
self.assertEqual("data", (outside / "keep").read_text())
def test_without_proot_an_image_that_cannot_be_unpacked_runs_nothing(self):
self.unpacker(works=False)
out = self.native()
self.assertNotIn("uruntime", out)
self.assertNotIn("apprun", out)
self.assertEqual([], self.unpacked_dirs())
self.assertEqual([], list(self.cache.glob("droiddeck-appimage/.unpack.*")))
def test_without_proot_an_x86_image_still_starts_under_fex(self):
self.machine(62)
self.cache = self.dir / "cache"
self.assertEqual(["TARGET= EXTRACT=1", "fex", "run", "--mode", "on", "--", str(self.image), "--no-gui"], self.native())
def test_the_session_starts_images_through_the_shared_launcher(self):
script = ("set -u\n"
"run_program() { printf '%%s\\n' \"$@\"; exit 0; }\n"