diff --git a/docs/releasing.md b/docs/releasing.md index b1c030a02b..f39898724c 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -242,7 +242,15 @@ Why these formats: `~/.local/share/applications/ai.storyteller.photocraft.desktop` with `Exec` rewritten to the AppImage's path (`TryExec` dropped, the binary isn't on the host PATH) and the hicolor icons beside it, rewriting only when the content changed, and any failure leaves - the launch untouched. `PHOTOCRAFT_NO_DESKTOP_INTEGRATION=1` opts out; to uninstall, `rm + the launch untouched. Launcher refresh uses a hidden generated snapshot to preserve + user edits, comments and deleted keys while updating untouched packaged metadata and + known executable paths. Missing or damaged snapshots use a conservative fallback. + External integration removes a duplicate only when a regular launcher exactly matches + a valid regular snapshot. Symlinked launchers or snapshots (including dangling links) + and special files are left alone during both refresh and deduplication; icons still + install. These checks do not synchronize concurrent changes by other processes. + This overlaps the deduplication protection proposed in #5168 without depending on it. + `PHOTOCRAFT_NO_DESKTOP_INTEGRATION=1` opts out; to uninstall, `rm ~/.local/share/applications/ai.storyteller.photocraft.desktop` and `find ~/.local/share/icons/hicolor -name 'ai.storyteller.photocraft*' -delete`. The script is covered by `packaging/linux/apprun-test.sh` in the packaging-lint workflow. diff --git a/packaging/linux/AppRun b/packaging/linux/AppRun index 3414c5acc7..75632e32ce 100755 --- a/packaging/linux/AppRun +++ b/packaging/linux/AppRun @@ -25,6 +25,29 @@ case "${ARGV0:-}" in photocraft-cli | */photocraft-cli) exec "$APPDIR/usr/bin/photocraft-cli" "$@" ;; esac +# 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() { + [ -f "$1" ] && [ ! -L "$1" ] && [ -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 ;; @@ -54,6 +77,8 @@ integrate() { # "already integrated" left the menu with no entry (#5534). apps="$data/applications" mkdir -p "$apps" 2>/dev/null || return 0 + desktop="$apps/$DESK_ID.desktop" + baseline="$apps/.$DESK_ID.generated" external=0 for f in "$apps"/*.desktop; do if [ ! -f "$f" ] || [ "$f" = "$apps/$DESK_ID.desktop" ]; then @@ -74,10 +99,19 @@ integrate() { break fi done - 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 + if [ -L "$desktop" ] || [ -L "$baseline" ] || + { [ -e "$desktop" ] && [ ! -f "$desktop" ]; } || + { [ -e "$baseline" ] && [ ! -f "$baseline" ]; }; then + # A link (including a dangling one) or special file is not our installation. + # Leave both launcher and snapshot alone; icons may still be installed below. + : + elif [ "$external" = 1 ]; then + # Only a byte-identical, valid generated snapshot proves this is ours to + # remove. A customized or legacy launcher belongs to the user. + if [ -f "$desktop" ] && valid_baseline "$baseline" && cmp -s "$baseline" "$desktop" && [ ! -L "$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 @@ -85,19 +119,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 473872ebfe..f25dd007eb 100755 --- a/packaging/linux/apprun-test.sh +++ b/packaging/linux/apprun-test.sh @@ -30,6 +30,7 @@ APPIMAGE="$TMP/photocraft test.AppImage" # spaces on purpose: Exec must stay quo export APPIMAGE export HOME="$TMP/home" export XDG_DATA_HOME="$HOME/.local/share" +unset DESKTOPINTEGRATION PHOTOCRAFT_NO_DESKTOP_INTEGRATION mkdir -p "$HOME" fail() { @@ -74,6 +75,128 @@ 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 "DESKTOPINTEGRATION changed $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 + +# Linked launchers and snapshots are externally owned, even when their bytes +# match a generated entry. Check both normal refresh and external deduplication. +for integration in normal external; do + for linked in launcher dangling-launcher baseline dangling-baseline; do + rm -rf "$XDG_DATA_HOME" + "$APPDIR/AppRun" >/dev/null + baseline="$XDG_DATA_HOME/applications/.$APP_ID.generated" + target="$TMP/linked-target" + cp "$DESKTOP" "$target" + case "$linked" in + launcher|dangling-launcher) link="$DESKTOP" ;; + baseline|dangling-baseline) link="$baseline" ;; + esac + rm -f "$link" + case "$linked" in dangling-*) rm -f "$target" ;; esac + ln -s "$target" "$link" + [ ! -f "$target" ] || cp "$target" "$TMP/linked-target-before" + # A packaged metadata change would otherwise cause a symlink to be replaced. + template="$APPDIR/usr/share/applications/$APP_ID.desktop" + cp "$template" "$TMP/link-template-before" + printf '\nX-Link-Refresh=changed\n' >>"$template" + if [ "$integration" = external ]; then + ext="$XDG_DATA_HOME/applications/external.desktop" + printf '[Desktop Entry]\nType=Application\nName=External\nExec="%s" %%F\n' "$APPIMAGE" >"$ext" + DESKTOPINTEGRATION=AppImageLauncher "$APPDIR/AppRun" >/dev/null + else + "$APPDIR/AppRun" >/dev/null + fi + [ -L "$link" ] || fail "$integration replaced or removed $linked" + [ "$(readlink "$link")" = "$target" ] || fail "$integration retargeted $linked" + case "$linked" in + dangling-*) [ ! -e "$target" ] || fail "$integration created dangling target" ;; + *) cmp -s "$target" "$TMP/linked-target-before" || fail "$integration changed linked target" ;; + esac + [ -f "$XDG_DATA_HOME/icons/hicolor/256x256/apps/$APP_ID.png" ] || fail "link preservation skipped icons" + cp "$TMP/link-template-before" "$template" + done +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.