okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0213-guidata (4c4596746) into f-0213-int22a3d7068

main
5.4 KiB95 linesraw
1#!/usr/bin/env bash
2# Put the bundled payloads back to the bytes they were staged with, inside a finished AppImage.
3#
4#   apps/desktop/scripts/repack-appimage.sh <in.AppImage> <out.AppImage> [--resources <src-tauri/resources>] [--check]
5#
6# Why: Tauri hands the AppDir to linuxdeploy, which treats every ELF file under usr/lib as a library to fix up: it rewrites the RPATH of
7# the engine (strata, libstrata_kq.so, libcudart), of the bundled Python and Node and of the Harness's native modules. The files then no
8# longer match the SHA-256 and size their manifests list (engine-component.json), so the host refuses the bundled engine, and the
9# installer's own integrity promise ("what we staged is what runs") is broken. This script unpacks the AppImage, copies the staged
10# folders (control, harness, node, engine-bundle) over the AppDir byte for byte, and packs it again with the same runtime.
11#
12# --check does not write anything: it lists every file of control, harness, node and engine-bundle whose bytes differ from what was staged, then
13# looks at every static PIE in the AppImage (appimage_elf.py: the bundled ripgrep is one; linuxdeploy's RPATH rewrite leaves such a program with a
14# filled .dynamic and it crashes at start, #4337) and runs the bundled `rg` once. A single "0 file(s) differ" and "ok" is what a release needs.
15set -euo pipefail
16
17IN="$(realpath "${1:?in.AppImage}")"; OUT="${2:-}"
18RES="$(cd "$(dirname "${BASH_SOURCE[0]}")/../src-tauri/resources" && pwd)"
19CHECK=0
20shift 2 || true
21while [ $# -gt 0 ]; do
22  case "$1" in
23    --resources) RES="$(cd "$2" && pwd)"; shift 2 ;;
24    --check) CHECK=1; shift ;;
25    *) echo "unknown option $1" >&2; exit 2 ;;
26  esac
27done
28
29OFFSET="$("$IN" --appimage-offset)"
30WORK="$(mktemp -d -t strata-repack.XXXXXX)"
31trap 'rm -rf "$WORK"' EXIT
32unsquashfs -no-progress -o "$OFFSET" -d "$WORK/AppDir" "$IN" >/dev/null
33# the product folder under usr/lib (Tauri names it after productName)
34LIB="$(find "$WORK/AppDir/usr/lib" -maxdepth 2 -type d -name engine-bundle -printf '%h\n' | head -1)"
35[ -n "$LIB" ] || LIB="$(find "$WORK/AppDir/usr/lib" -maxdepth 2 -type d -name control -printf '%h\n' | head -1)"
36[ -n "$LIB" ] || { echo "no Strata resource folder in the AppImage" >&2; exit 1; }
37
38HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
39
40# Every static PIE under the AppDir must still have a sound .dynamic, and the bundled ripgrep must start (x86-64 Linux only: it is run).
41elf_and_rg_check() {
42  local bad=0
43  if command -v python3 >/dev/null 2>&1; then
44    python3 "$HERE/appimage_elf.py" scan "$LIB" || bad=1
45  else
46    echo "python3 is not here: the static PIE check was skipped" >&2
47  fi
48  local rg
49  rg="$(find "$LIB/harness" -path '*ripgrep-linux-x64/bin/rg' -type f 2>/dev/null | head -1)"
50  if [ -n "$rg" ] && [ "$(uname -s)" = Linux ] && [ "$(uname -m)" = x86_64 ]; then
51    local t; t="$(mktemp -d -t strata-rg.XXXXXX)"
52    echo 'needle in a haystack' > "$t/h.txt"
53    if "$rg" --version >/dev/null 2>&1 && [ "$("$rg" --no-config -c needle "$t/h.txt" 2>/dev/null)" = 1 ]; then echo "bundled rg runs: ok"
54    else echo "the bundled rg does not run: $rg" >&2; bad=1; fi
55    rm -rf "$t"
56  fi
57  return $bad
58}
59
60if [ "$CHECK" = 1 ]; then
61  bad=0
62  while IFS= read -r f; do
63    rel="${f#$RES/}"
64    if ! cmp -s "$f" "$LIB/$rel"; then echo "differs: $rel"; bad=$((bad + 1)); fi
65  done < <(for d in control harness node engine-bundle; do [ -d "$RES/$d" ] && find "$RES/$d" -type f ! -name .gitignore; done)
66  echo "$bad file(s) differ from what was staged"
67  elf_and_rg_check || bad=$((bad + 1))
68  [ "$bad" = 0 ]
69  exit
70fi
71
72[ -n "$OUT" ] || { echo "give out.AppImage" >&2; exit 2; }
73for d in control harness node engine-bundle; do
74  [ -d "$RES/$d" ] || continue
75  rsync -aL --checksum --delete --exclude .gitignore "$RES/$d/" "$LIB/$d/"
76done
77# What was put back must be sound before it is packed: a damaged static PIE (the bundled ripgrep) stops the pack here, not on a person's computer.
78elf_and_rg_check || { echo "the restored AppDir failed the static PIE / ripgrep check; not packing" >&2; exit 1; }
79# The launcher records the environment the AppImage was started from, before its own hooks add the AppImage's libraries and GTK/GIO
80# settings (LD_LIBRARY_PATH, GTK_*, XDG_DATA_DIRS ...). The host gives it back to every program that is not Strata's own (zenity,
81# kdialog, xdg-open, the assistant's shell): apps/host/server/external-env.mjs. Without it a system program loads the AppImage's libssl
82# and dies ("OPENSSL_3.2.0 not found"). The line goes right after `this_dir=...`, ahead of every hook.
83if ! grep -q STRATA_ORIGINAL_ENV "$WORK/AppDir/AppRun"; then
84  sed -i '/^this_dir=/a # Strata: the environment this AppImage was started from (given back to programs that are not Strata'"'"'s own)\nexport STRATA_ORIGINAL_ENV="$(env -0 | base64 -w0)"' "$WORK/AppDir/AppRun"
85  grep -q STRATA_ORIGINAL_ENV "$WORK/AppDir/AppRun" || { echo "could not add the environment record to AppRun" >&2; exit 1; }
86fi
87mksquashfs "$WORK/AppDir" "$WORK/app.squashfs" -root-owned -noappend -comp zstd -b 131072 -no-progress >/dev/null
88head -c "$OFFSET" "$IN" > "$OUT"
89cat "$WORK/app.squashfs" >> "$OUT"
90chmod +x "$OUT"
91# The checksum is computed here, from the finished file, every time: a README or a note must take it from this file
92# (apps/desktop/scripts/make-tester-readme.sh) and never from an older build.
93( cd "$(dirname "$OUT")" && sha256sum "$(basename "$OUT")" > "$(basename "$OUT").sha256" )
94echo "repacked: $OUT ($(stat -c %s "$OUT") bytes)"
95cat "$OUT.sha256"