From cf44aaed145327b0bfd94b10bd33c0d511a82656 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:37:09 -0500 Subject: [PATCH] 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 --- .../usr/local/bin/droiddeck-appimage-exec | 34 +++++ .../overlay/usr/local/bin/droiddeck-fex | 20 ++- tools/tests/test_droiddeck_fex.py | 67 +++++++++ tools/tests/test_session_appimage.py | 130 +++++++++++++++++- 4 files changed, 246 insertions(+), 5 deletions(-) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-exec b/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-exec index 454333efa9..5bcbacbdd8 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-exec +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-exec @@ -9,6 +9,40 @@ if [ -x /usr/local/bin/droiddeck-fex ]; then 3|62) exec /usr/local/bin/droiddeck-fex run --mode on -- "$image" "$@" ;; esac fi +if [ -n "${DDNATV_ROOT:-}" ]; then + image=$(realpath -- "$image") || exit 127 + cache=${XDG_CACHE_HOME:-${HOME:-/root}/.cache}/droiddeck-appimage + mkdir -p "$cache" || exit 1 + exec 9>"$cache/.lock" + flock 9 || exit 1 + state() { printf '%s\n%s' "$1" "$(TZ=UTC0 stat -Lc '%s %y' -- "$1" 2>/dev/null)"; } + for dir in "$cache"/*/; do + dir=${dir%/} + [ ! -L "$dir" ] && [ -f "$dir/source" ] || continue + [ "$(cat "$dir/source")" = "$(state "$(head -n 1 "$dir/source")")" ] && continue + (flock -n 7 && rm -rf -- "${dir:?}") 7<"$dir/source" + done + find "$cache" -mindepth 1 -maxdepth 1 -name '.unpack.*' -mmin +60 -exec rm -rf -- {} + 2>/dev/null + source=$(state "$image") + unpacked=$cache/$(printf '%s' "$source" | md5sum | cut -c1-16) + if [ "$(cat "$unpacked/source" 2>/dev/null)" != "$source" ] || [ ! -e "$unpacked/squashfs-root/AppRun" ]; then + echo "== appimage: unpacking $image" + work=$(mktemp -d "$cache/.unpack.XXXXXX") || exit 1 + if ! (cd "$work" && /usr/local/bin/droiddeck-fex extract "$image") || [ ! -e "$work/squashfs-root/AppRun" ]; then + rm -rf -- "${work:?}" + echo "droiddeck-appimage-exec: could not unpack $image" >&2 + exit 1 + fi + printf '%s\n' "$source" > "$work/source" + rm -rf -- "${unpacked:?}" + mv -- "$work" "$unpacked" || exit 1 + fi + exec 8<"$unpacked/source" + flock -s 8 || exit 1 + exec 9>&- + export APPIMAGE=$image APPDIR=$unpacked/squashfs-root ARGV0=$image OWD=$PWD + exec "$APPDIR/AppRun" "$@" +fi if [ -x /usr/local/lib/droiddeck/uruntime ]; then export TARGET_APPIMAGE=$image exec /usr/local/lib/droiddeck/uruntime "$@" diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex b/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex index 7c1dd3176f..a25b755cee 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex @@ -851,8 +851,12 @@ def extract(image): clear_extracted() x86 = elf_arch(image) in X86.values() if os.access(URUNTIME, os.X_OK): - run = subprocess.run([URUNTIME, "--appimage-extract"], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, - env=dict(os.environ, TARGET_APPIMAGE=image)) + with open(image, "rb") as source: + native = bool(os.environ.get("DDNATV_ROOT")) + target = "/proc/self/fd/%d" % source.fileno() if native else image + run = subprocess.run([URUNTIME, "--appimage-extract"], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, + env=dict(os.environ, TARGET_APPIMAGE=target), + pass_fds=(source.fileno(),) if native else ()) errors = run.stderr.decode("utf-8", "replace") sys.stderr.write(errors) status = run.returncode @@ -882,7 +886,17 @@ def extract(image): want_fex() return 0 say("unsquashfs could not extract it (status %d)" % status) - elif fex is None: + seven = shutil.which("7z") + if seven: + clear_extracted() + status = subprocess.call([seven, "x", "-tSquashFS", "-y", "-osquashfs-root", image], stdout=subprocess.DEVNULL) + if status == 0 and extracted(): + if x86: + want_fex() + return 0 + say("7-Zip could not extract it (status %d)" % status) + clear_extracted() + elif fex is None and not unsquashfs: say("this AppImage could not be unpacked here: no AppImage runtime for ARM64, no FEX yet and no unsquashfs") return 1 diff --git a/tools/tests/test_droiddeck_fex.py b/tools/tests/test_droiddeck_fex.py index ce474de509..993d668d95 100644 --- a/tools/tests/test_droiddeck_fex.py +++ b/tools/tests/test_droiddeck_fex.py @@ -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): diff --git a/tools/tests/test_session_appimage.py b/tools/tests/test_session_appimage.py index 3fc5352427..295e73da3b 100644 --- a/tools/tests/test_session_appimage.py +++ b/tools/tests/test_session_appimage.py @@ -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"