From 4203d2a5cf8ec1af893ee6daa6045a285fc2f40f Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Tue, 13 Oct 2026 18:45:26 +0200 Subject: [PATCH] fix(linux): preserve custom launchers with damaged baselines and external integration (#2905) --- packaging/linux/AppRun | 114 +++++++++++++++++++++++++++++++-- packaging/linux/apprun-test.sh | 83 ++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 6 deletions(-) diff --git a/packaging/linux/AppRun b/packaging/linux/AppRun index f04c5f4eab..852aad4f0d 100755 --- a/packaging/linux/AppRun +++ b/packaging/linux/AppRun @@ -14,6 +14,29 @@ set -u APPDIR="$(dirname "$(readlink -f "$0")")" DESK_ID=ai.storyteller.photocraft +# A damaged snapshot is not evidence that an absent user key was newly added. +# Treat it like a legacy installation and preserve the existing launcher. +valid_baseline() { + [ -s "$1" ] || return 1 + awk ' + /^\[/ { group = $0; if (group == "[Desktop Entry]") sections++; next } + /^[ \t]*($|#)/ { next } + /^[^= \t][^=]*=/ { + name = $0; sub(/=.*/, "", name) + k = group SUBSEP name + if (seen[k]++) bad = 1 + if (group == "[Desktop Entry]") { + if ($0 == "Type=Application") type = 1 + if ($0 ~ /^Name=.+$/) title = 1 + if ($0 ~ /^Exec=".+" %F$/) executable = 1 + } + next + } + { bad = 1 } + END { exit !(sections == 1 && type && title && executable && !bad) } + ' "$1" +} + integrate() { case "${PHOTOCRAFT_NO_DESKTOP_INTEGRATION:-}" in 1 | true | yes) return 0 ;; @@ -40,6 +63,8 @@ integrate() { # own if left over from an earlier run, and still install the hicolor icons below. apps="$data/applications" mkdir -p "$apps" 2>/dev/null || return 0 + desktop="$apps/$DESK_ID.desktop" + baseline="$apps/.$DESK_ID.generated" external=0 if [ -n "${DESKTOPINTEGRATION:-}" ]; then external=1 @@ -55,9 +80,12 @@ integrate() { done fi if [ "$external" = 1 ]; then - if [ -f "$apps/$DESK_ID.desktop" ]; then - rm -f "$apps/$DESK_ID.desktop" 2>/dev/null || true - command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$apps" >/dev/null 2>&1 + # Only a byte-identical, valid generated snapshot proves this is ours to + # remove. A customized or legacy launcher belongs to the user. + if valid_baseline "$baseline" && cmp -s "$baseline" "$desktop"; then + if rm -f "$desktop" 2>/dev/null; then + command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$apps" >/dev/null 2>&1 + fi fi else # Rewrite Exec to this AppImage (the binary is not on the host PATH) and drop @@ -65,19 +93,93 @@ integrate() { tmp="$apps/.$DESK_ID.tmp.$$" esc="$(printf '%s' "$appimage" | sed 's/[\\&|]/\\&/g')" if sed -e "s|^Exec=.*|Exec=\"$esc\" %F|" -e '/^TryExec=/d' "$src_desktop" 2>/dev/null >"$tmp"; then - if cmp -s "$tmp" "$apps/$DESK_ID.desktop"; then + generated="$tmp.generated" + cp "$tmp" "$generated" 2>/dev/null || { rm -f "$tmp"; return 0; } + if [ -f "$desktop" ]; then + # Three-way merge: update shipped keys only if the user has not changed + # them. Keep custom keys, comments and deletions. The snapshot is hidden + # and has no .desktop suffix, so desktop menus do not discover it. + # For older installs without a snapshot, the current template is the + # conservative baseline. An unknown previous executable is left alone. + previous="$baseline" + bootstrap=0 + if ! valid_baseline "$previous"; then + previous="$tmp.previous" + cp "$generated" "$previous" 2>/dev/null || { rm -f "$tmp" "$generated"; return 0; } + bootstrap=1 + fi + if ! awk -v bootstrap="$bootstrap" ' + function key(line) { + if (line ~ /^\[/) { group = line; return "" } + if (line ~ /^[^#= \t][^=]*=/) { + name = line; sub(/=.*/, "", name); return group SUBSEP name + } + return "" + } + FNR == 1 { group = "" } + FILENAME == ARGV[1] { + k = key($0); if (k != "") old[k] = $0 + if (group == "[Desktop Entry]" && $0 ~ /^Exec=".*" %F$/) + old_exec = substr($0, 6, length($0) - 8) + next + } + FILENAME == ARGV[2] { + k = key($0); if (k != "") { fresh[k] = $0; order[++n] = k } + if (group == "[Desktop Entry]" && $0 ~ /^Exec=/) + new_exec = substr($0, 6, length($0) - 8) + next + } + function additions( i, k) { + for (i = 1; i <= n; i++) { + k = order[i] + if (index(k, "[Desktop Entry]" SUBSEP) == 1 && !(k in old) && !(k in seen)) + print fresh[k] + } + } + { + if ($0 ~ /^\[/ && group == "[Desktop Entry]") additions() + k = key($0) + if (k != "") { + seen[k] = 1 + if (k in old && $0 == old[k]) { + if (k in fresh) print fresh[k] + next + } + if (group == "[Desktop Entry]" && name == "Exec") { + # Literal substitution, never a regex or shell evaluation: keep + # environment wrappers and arguments around the managed path. + pos = index($0, old_exec) + prefix = substr($0, 1, pos - 1) + if (old_exec != "" && pos > 0 && prefix ~ /^Exec=(env[ \t]+([^ \t=]+=[^ \t]*[ \t]+)*)?$/) + $0 = substr($0, 1, pos - 1) new_exec substr($0, pos + length(old_exec)) + } + } + print + } + END { if (group == "[Desktop Entry]") additions() } + ' "$previous" "$generated" "$desktop" >"$tmp"; then + rm -f "$tmp" "$generated" + [ "$bootstrap" = 0 ] || rm -f "$previous" + return 0 + fi + [ "$bootstrap" = 0 ] || rm -f "$previous" + fi + if cmp -s "$tmp" "$desktop"; then rm -f "$tmp" else - if mv -f "$tmp" "$apps/$DESK_ID.desktop" 2>/dev/null; then + if mv -f "$tmp" "$desktop" 2>/dev/null; then command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$apps" >/dev/null 2>&1 else - rm -f "$tmp" + rm -f "$tmp" "$generated" + return 0 fi fi + mv -f "$generated" "$baseline" 2>/dev/null || rm -f "$generated" else rm -f "$tmp" return 0 fi + fi # Icons: per-file compare so an unchanged install costs a handful of cmp calls. diff --git a/packaging/linux/apprun-test.sh b/packaging/linux/apprun-test.sh index 475fb5ee5e..5955550e17 100755 --- a/packaging/linux/apprun-test.sh +++ b/packaging/linux/apprun-test.sh @@ -72,6 +72,89 @@ export APPIMAGE="$TMP/moved.AppImage" "$APPDIR/AppRun" >/dev/null || fail "launch after move exited non-zero" grep -Fxq "Exec=\"$(readlink -f "$APPIMAGE")\" %F" "$DESKTOP" || fail "Exec not refreshed after move" +# Customized launch options and metadata survive launches and relocation (#2905). +old_path="$(readlink -f "$APPIMAGE")" +sed -i "s|^Exec=.*|Exec=env WAYLAND_DISPLAY= \"$old_path\" --custom %F|; s|^Name=.*|Name=My PhotoCraft|" "$DESKTOP" +printf '\nX-User-Setting=keep me\n' >>"$DESKTOP" +cp "$DESKTOP" "$TMP/custom.desktop" +before="$(stat -c %i "$DESKTOP")" +"$APPDIR/AppRun" >/dev/null +cmp -s "$DESKTOP" "$TMP/custom.desktop" || fail "customized launcher overwritten" +[ "$before" = "$(stat -c %i "$DESKTOP")" ] || fail "custom launcher rewritten although unchanged" +mv "$APPIMAGE" "$TMP/new version.AppImage" +export APPIMAGE="$TMP/new version.AppImage" +"$APPDIR/AppRun" >/dev/null +grep -Fxq "Exec=env WAYLAND_DISPLAY= \"$APPIMAGE\" --custom %F" "$DESKTOP" || fail "custom Exec not relocated" +grep -Fxq 'Name=My PhotoCraft' "$DESKTOP" || fail "custom Name lost" +grep -Fxq 'X-User-Setting=keep me' "$DESKTOP" || fail "custom key lost" + +# Packaged updates apply only to untouched keys; user deletions stay deleted. +sed -i '/^Comment=/d' "$DESKTOP" +sed -i 's/^Name=.*/Name=Packaged update/; s/^Categories=.*/Categories=Graphics;/; s/^Comment=.*/Comment=Updated description/' "$APPDIR/usr/share/applications/$APP_ID.desktop" +printf '\nX-New-Packaged-Key=added\n' >>"$APPDIR/usr/share/applications/$APP_ID.desktop" +"$APPDIR/AppRun" >/dev/null +grep -Fxq 'Name=My PhotoCraft' "$DESKTOP" || fail "packaged update erased custom Name" +grep -Fxq 'Categories=Graphics;' "$DESKTOP" || fail "untouched metadata not updated" +grep -q '^Comment=' "$DESKTOP" && fail "user deletion undone" +grep -Fxq 'X-New-Packaged-Key=added' "$DESKTOP" || fail "new packaged key missing" +if command -v desktop-file-validate >/dev/null; then + desktop-file-validate "$DESKTOP" || fail "customized entry is invalid" +fi + +# A replaced executable may mention the old AppImage as an argument: leave it. +sed -i "s|^Exec=.*|Exec=\"/custom/wrapper\" \"$APPIMAGE\" %F|" "$DESKTOP" +expected_wrapper="$(grep '^Exec=' "$DESKTOP")" +mv "$APPIMAGE" "$TMP/another version.AppImage" +export APPIMAGE="$TMP/another version.AppImage" +"$APPDIR/AppRun" >/dev/null +grep -Fxq "$expected_wrapper" "$DESKTOP" || fail "wrapper argument mistaken for managed executable" + +# Existing launchers from before snapshots: bootstrap without losing edits. +rm -f "$XDG_DATA_HOME/applications/.$APP_ID.generated" +cp "$DESKTOP" "$TMP/legacy.desktop" +"$APPDIR/AppRun" >/dev/null +cmp -s "$DESKTOP" "$TMP/legacy.desktop" || fail "legacy custom launcher overwritten" +rm -f "$XDG_DATA_HOME/applications/.$APP_ID.generated" +sed -i 's|^Exec=.*|Exec="/custom/wrapper" %F|' "$DESKTOP" +"$APPDIR/AppRun" >/dev/null +grep -Fxq 'Exec="/custom/wrapper" %F' "$DESKTOP" || fail "legacy executable customization overwritten" + + +# Empty or malformed snapshots must never resurrect user-deleted keys. +for invalid in empty garbage missing-exec; do + baseline="$XDG_DATA_HOME/applications/.$APP_ID.generated" + case "$invalid" in + empty) : >"$baseline" ;; + garbage) printf 'not a desktop entry\n' >"$baseline" ;; + missing-exec) printf '[Desktop Entry]\nType=Application\nName=PhotoCraft\n' >"$baseline" ;; + esac + sed -i '/^Comment=/d; /^StartupNotify=/d' "$DESKTOP" + cp "$DESKTOP" "$TMP/before-invalid.desktop" + "$APPDIR/AppRun" >/dev/null || fail "invalid baseline blocked launch" + cmp -s "$DESKTOP" "$TMP/before-invalid.desktop" || fail "$invalid baseline changed user launcher" +done + +# External integration must preserve customized and unknown legacy entries. +for baseline_kind in valid missing empty; do + baseline="$XDG_DATA_HOME/applications/.$APP_ID.generated" + case "$baseline_kind" in + missing) rm -f "$baseline" ;; + empty) : >"$baseline" ;; + esac + cp "$DESKTOP" "$TMP/before-external.desktop" + DESKTOPINTEGRATION=AppImageLauncher "$APPDIR/AppRun" >/dev/null + cmp -s "$DESKTOP" "$TMP/before-external.desktop" || fail "external integrator removed $baseline_kind custom entry" + ext="$XDG_DATA_HOME/applications/external.desktop" + printf '[Desktop Entry]\nType=Application\nName=External\nExec="%s" %%F\n' "$APPIMAGE" >"$ext" + "$APPDIR/AppRun" >/dev/null + cmp -s "$DESKTOP" "$TMP/before-external.desktop" || fail "external desktop removed $baseline_kind custom entry" + rm -f "$ext" +done + +# Reset the stub fixture so the duplicate-removal test starts with an untouched entry. +rm -rf "$XDG_DATA_HOME" +"$APPDIR/AppRun" >/dev/null + # 6. External AppImage integrator (#3623): if another .desktop file in $XDG_DATA_HOME/applications # already launches this AppImage (e.g. AppImageLauncher's appimagekit-*.desktop), AppRun removes # its own duplicate .desktop entry while keeping the hicolor icons installed.