Skip to main content

APPLY_SCRIPT

Constant APPLY_SCRIPT 

Source
const APPLY_SCRIPT: &str = r#"
pid="$1"; dmg="$2"; sha="$3"; ver="$4"; app="$5"; channel="$6"; log="$7"; marker="$8"
exec >>"$log" 2>&1
say() { echo "$(/bin/date -u +%Y-%m-%dT%H:%M:%SZ) $*"; }
work=""; mnt=""; staging=""
cleanup() {
  [ -n "$mnt" ] && { /usr/bin/hdiutil detach -quiet "$mnt" || /usr/bin/hdiutil detach -quiet -force "$mnt"; }
  [ -n "$staging" ] && /bin/rm -rf "$staging"
  [ -n "$work" ] && /bin/rm -rf "$work"
  [ -n "$locked" ] && /bin/rm -rf "$lock"
}
# The "Updating to <version>" window (updating_window) says the update failed when this file is there. The
# folder is the window's own: with no window it isn't there and touch fails quietly (an external command, so a
# failed redirect can't end this shell, as one on a special builtin would).
mark_failed() { [ -n "$marker" ] && /usr/bin/touch "$marker" 2>/dev/null; return 0; }
fail() { say "failed: $*; opening the AgentMux that is in place"; mark_failed; cleanup; /usr/bin/open "$app"; exit 1; }
team_of() { /usr/bin/codesign -dv "$1" 2>&1 | /usr/bin/sed -n 's/^TeamIdentifier=//p'; }
parent=$(/usr/bin/dirname "$app"); base=$(/usr/bin/basename "$app")
lock="$parent/.$base.upgrade-lock"; locked=""
say "upgrade to $ver: waiting for the launcher ($pid) to exit"
i=0
while /bin/kill -0 "$pid" 2>/dev/null; do
  i=$((i+1)); [ "$i" -gt 600 ] && { say "the launcher is still running after 60 s; nothing changed"; mark_failed; /usr/bin/open "$app"; exit 1; }
  /bin/sleep 0.1
done
# One swap of this app at a time: two launchers on different channels can
# run the same .app. A lock left by a script that is gone is taken over.
if ! /bin/mkdir "$lock" 2>/dev/null; then
  other=$(/bin/cat "$lock/pid" 2>/dev/null)
  if [ -n "$other" ] && /bin/kill -0 "$other" 2>/dev/null; then
    # That swap opens the app when it is done.
    say "another upgrade of $app is running ($other); nothing changed"; exit 1
  fi
  /bin/rm -rf "$lock"; /bin/mkdir "$lock" 2>/dev/null || { say "couldn't take the upgrade lock; nothing changed"; mark_failed; /usr/bin/open "$app"; exit 1; }
fi
locked=1; echo "$$" > "$lock/pid"
work=$(/usr/bin/mktemp -d "${TMPDIR:-/tmp}/agentmux-upgrade.XXXXXX") || fail "no temporary folder"
/bin/cp "$dmg" "$work/u.dmg" || fail "couldn't copy the disk image"
got=$(/usr/bin/shasum -a 256 "$work/u.dmg" | /usr/bin/awk '{print $1}')
[ "$got" = "$sha" ] || fail "the disk image isn't the release (sha256 $got)"
mnt="$work/mnt"; /bin/mkdir "$mnt" || fail "no mount point"
/usr/bin/hdiutil attach -quiet -nobrowse -noautoopen -readonly -mountpoint "$mnt" "$work/u.dmg" || { mnt=""; fail "couldn't mount the disk image"; }
new="$mnt/AgentMux.app"
[ -d "$new" ] || new=$(/bin/ls -d "$mnt"/*.app 2>/dev/null | /usr/bin/head -n 1)
[ -n "$new" ] && [ -d "$new" ] || fail "no app in the disk image"
got_ver=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$new/Contents/Info.plist" 2>/dev/null)
[ "$got_ver" = "$ver" ] || fail "the disk image holds version '$got_ver', not $ver"
/usr/bin/codesign --verify --deep --strict "$new" || fail "the new app's code signature doesn't verify"
old_team=$(team_of "$app")
if [ -n "$old_team" ] && [ "$old_team" != "not set" ]; then
  [ "$(team_of "$new")" = "$old_team" ] || fail "the new app isn't signed by this app's team ($old_team)"
fi
if /usr/sbin/spctl -a -t exec "$app" 2>/dev/null; then
  /usr/sbin/spctl -a -t exec "$new" || fail "Gatekeeper doesn't accept the new app"
fi
# A folder of our own beside the app (created new, unpredictable name), so
# nothing another account placed there is followed or overwritten.
staging=$(/usr/bin/mktemp -d "$parent/.$base.upgrading.XXXXXX") || fail "couldn't make a folder beside the app"
/usr/bin/ditto "$new" "$staging/$base" || fail "couldn't copy the new app beside this one"
/usr/bin/codesign --verify --deep --strict "$staging/$base" || fail "the copied app's code signature doesn't verify"
/usr/bin/hdiutil detach -quiet "$mnt" || /usr/bin/hdiutil detach -quiet -force "$mnt"; mnt=""
old_ver=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$app/Contents/Info.plist" 2>/dev/null)
trash="$HOME/.Trash/${base%.app} $old_ver $(/bin/date +%Y%m%d-%H%M%S).app"
/bin/mv "$app" "$trash" || fail "couldn't move this version to the Trash"
if ! /bin/mv "$staging/$base" "$app"; then
  /bin/mv "$trash" "$app"
  fail "couldn't put the new app in place; this version is back"
fi
if [ -n "$channel" ]; then
  # Written to a new file of our own, then renamed over the record: a rename
  # replaces whatever is at that name and never follows it.
  record="$parent/.$base.agentmux-channel"
  tmp=$(/usr/bin/mktemp "$parent/.$base.agentmux-channel.XXXXXX") &&
    printf '%s\n' "$channel" > "$tmp" && /bin/mv "$tmp" "$record" || { [ -n "$tmp" ] && /bin/rm -f "$tmp"; say "couldn't record the channel beside the app"; }
fi
cleanup
say "replaced $app with $ver; this version is in the Trash as $trash"
/usr/bin/open "$app" || say "couldn't open the new app"
"#;
Expand description

The swap, run by /bin/sh once this launcher ($1) has exited. Every path is an argument, never part of the script, so nothing in one is parsed. Arguments: launcher pid, .dmg, SHA-256, version, the app to replace, the channel to record beside it (may be empty), the log, the file to leave when the upgrade failed (crate::updating_window::failed_marker).