409 lines
17 KiB
Bash
Executable File
409 lines
17 KiB
Bash
Executable File
#!/bin/sh
|
|
# Stable BookSprite supervisor. Never replace this file in an OTA archive.
|
|
# BusyBox ash / FAT compatible; no firmware writes and no stock KOReader access.
|
|
#
|
|
# Release contract:
|
|
# pending.tar.gz contains ONLY koreader/, regular files and directories.
|
|
# koreader/booksprite-files.txt lists EVERY shipped regular file, one relative
|
|
# path per line (including itself), sorted, without a "koreader/" prefix.
|
|
# No links, control characters, dot components, or duplicate archive members.
|
|
# Update policy: fresh payload + old untracked files + explicit mutable data.
|
|
# Modified shipped code is replaced; removed shipped files are not resurrected.
|
|
# previous/koreader is a complete pre-update snapshot, including runtime state.
|
|
# --rollback swaps those complete snapshots, not just binaries.
|
|
#
|
|
# BOOKSPRITE_HOME is a test-only override; production always uses the default.
|
|
|
|
umask 022
|
|
LC_ALL=C
|
|
export LC_ALL
|
|
HOME_DIR=${BOOKSPRITE_HOME:-/mnt/onboard/.adds/booksprite}
|
|
case "$HOME_DIR" in
|
|
/*/booksprite) ;;
|
|
*) echo "BookSprite: unsafe installation directory" >&2; exit 1 ;;
|
|
esac
|
|
if [ -L "$HOME_DIR" ] || ! HOME_DIR=$(CDPATH='' cd "$HOME_DIR" && pwd -P); then
|
|
echo "BookSprite: installation directory is missing or linked" >&2
|
|
exit 1
|
|
fi
|
|
case "$HOME_DIR" in
|
|
/*/booksprite) ;;
|
|
*) echo "BookSprite: unsafe resolved installation directory" >&2; exit 1 ;;
|
|
esac
|
|
|
|
# Upstream moves koreader.sh to /tmp for USBMS. This supervisor must likewise
|
|
# have neither an open script/log descriptor nor a cwd on onboard while it waits.
|
|
if [ "${BOOKSPRITE_RELOCATED:-}" != "$HOME_DIR" ]; then
|
|
TEMP_SCRIPT=$(mktemp /tmp/booksprite-launch.XXXXXX) || exit 1
|
|
if ! cp "$0" "$TEMP_SCRIPT"; then
|
|
rm -f "$TEMP_SCRIPT"
|
|
exit 1
|
|
fi
|
|
BOOKSPRITE_HOME=$HOME_DIR BOOKSPRITE_RELOCATED=$HOME_DIR \
|
|
BOOKSPRITE_TEMP_SCRIPT=$TEMP_SCRIPT \
|
|
exec sh "$TEMP_SCRIPT" "$@"
|
|
exit 1
|
|
fi
|
|
cd / || exit 1
|
|
|
|
CURRENT=$HOME_DIR/koreader
|
|
PREVIOUS=$HOME_DIR/previous
|
|
STAGE=$HOME_DIR/.staging
|
|
JOURNAL=$HOME_DIR/.transaction
|
|
LOCK=$HOME_DIR/.launcher.lock
|
|
LOCKED=false
|
|
CHILD=
|
|
RECOVERED_ROLLBACK=false
|
|
|
|
log() {
|
|
printf '%s\n' "BookSprite: $*" >&2
|
|
if [ ! -L "$HOME_DIR/launcher.log" ]; then
|
|
printf '%s %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" >>"$HOME_DIR/launcher.log"
|
|
fi
|
|
}
|
|
die() { log "$*"; exit 1; }
|
|
cleanup() {
|
|
if [ "$LOCKED" = true ]; then
|
|
rm -f "$LOCK/owner.$$"
|
|
rm -f "$LOCK/pid"
|
|
rmdir "$LOCK" 2>/dev/null || :
|
|
fi
|
|
case "${BOOKSPRITE_TEMP_SCRIPT:-}" in
|
|
/tmp/booksprite-launch.*) rm -f "$BOOKSPRITE_TEMP_SCRIPT" ;;
|
|
esac
|
|
}
|
|
interrupted() {
|
|
if [ -n "$CHILD" ]; then
|
|
kill -TERM "$CHILD" 2>/dev/null || :
|
|
wait "$CHILD" 2>/dev/null || :
|
|
fi
|
|
exit 143
|
|
}
|
|
trap cleanup EXIT
|
|
trap interrupted HUP INT TERM
|
|
|
|
# Fail closed on unexpected links at all paths which may be replaced/deleted.
|
|
for path in "$CURRENT" "$PREVIOUS" "$PREVIOUS/koreader" "$STAGE" \
|
|
"$JOURNAL" "$JOURNAL.new" "$LOCK" "$HOME_DIR/pending.tar.gz" \
|
|
"$HOME_DIR/rejected.tar.gz" "$HOME_DIR/launcher.log"; do
|
|
[ ! -L "$path" ] || die "Refusing linked control path: $path"
|
|
done
|
|
|
|
read_owner() {
|
|
OWNER=
|
|
for marker in "$LOCK"/owner.*; do
|
|
[ -f "$marker" ] || continue
|
|
candidate=${marker##*.}
|
|
case "$candidate" in ''|*[!0-9]*) die "Invalid lock owner" ;; esac
|
|
[ -z "$OWNER" ] || die "Multiple lock owners; recover over USB"
|
|
OWNER=$candidate
|
|
done
|
|
# Accept the plain PID layout as well, including stale pre-marker locks.
|
|
[ -n "$OWNER" ] || OWNER=$(cat "$LOCK/pid" 2>/dev/null)
|
|
}
|
|
if ! mkdir "$LOCK" 2>/dev/null; then
|
|
[ -d "$LOCK" ] || die "Invalid launcher lock"
|
|
# Allow an incumbent time to publish its PID after mkdir.
|
|
[ -f "$LOCK/pid" ] || sleep 1
|
|
read_owner
|
|
case "$OWNER" in
|
|
''|*[!0-9]*) OWNER= ;;
|
|
*) if kill -0 "$OWNER" 2>/dev/null; then
|
|
log "Already running (PID $OWNER)"
|
|
exit 75
|
|
fi ;;
|
|
esac
|
|
# rmdir deliberately refuses an unexpected/nonempty lock directory.
|
|
[ ! -L "$LOCK/pid" ] || die "Invalid lock PID"
|
|
# Remove only the observed owner's marker. If two launchers race to reclaim
|
|
# a stale lock, the loser cannot delete the winner's differently named live
|
|
# owner marker; its rmdir fails instead of admitting a second reader.
|
|
[ -z "$OWNER" ] || rm -f "$LOCK/owner.$OWNER" || die "Cannot clear stale owner"
|
|
PID_NOW=$(cat "$LOCK/pid" 2>/dev/null)
|
|
if [ "$PID_NOW" = "$OWNER" ] || [ -z "$PID_NOW" ]; then
|
|
rm -f "$LOCK/pid" || die "Cannot clear stale lock"
|
|
fi
|
|
rmdir "$LOCK" 2>/dev/null || die "Cannot clear stale lock directory"
|
|
mkdir "$LOCK" 2>/dev/null || die "Another launcher acquired the lock"
|
|
log "Recovered stale launcher lock"
|
|
fi
|
|
: >"$LOCK/owner.$$" || die "Cannot write launcher owner"
|
|
printf '%s\n' "$$" >"$LOCK/pid" || die "Cannot write launcher lock"
|
|
LOCKED=true
|
|
|
|
remove_stage() {
|
|
[ "$STAGE" = "$HOME_DIR/.staging" ] && [ ! -L "$STAGE" ] ||
|
|
die "Unsafe staging path"
|
|
rm -rf "$STAGE"
|
|
}
|
|
write_phase() {
|
|
printf '%s\n' "$1" >"$JOURNAL.new" || die "Cannot write transaction"
|
|
sync
|
|
mv "$JOURNAL.new" "$JOURNAL" || die "Cannot publish transaction"
|
|
sync
|
|
}
|
|
has_reader() { [ -d "$1" ] && [ ! -L "$1" ] && [ -f "$1/koreader.sh" ]; }
|
|
|
|
# Write-ahead phases plus rename-state checks make every individual move
|
|
# repeatable after power loss, even between a rename and the next phase write.
|
|
recover() {
|
|
[ -f "$JOURNAL" ] || return 0
|
|
PHASE=$(cat "$JOURNAL") || die "Cannot read transaction"
|
|
log "Recovering transaction: $PHASE"
|
|
case "$PHASE" in
|
|
update-save|update-promote|update-finish|rollback-save|rollback-restore|rollback-finish) ;;
|
|
*) die "Unknown transaction; recover installation over USB" ;;
|
|
esac
|
|
case "$PHASE" in rollback-*) RECOVERED_ROLLBACK=true ;; esac
|
|
if [ "$PHASE" = update-save ]; then
|
|
has_reader "$STAGE/koreader" || die "Staged update is incomplete"
|
|
if has_reader "$CURRENT"; then
|
|
[ ! -L "$PREVIOUS" ] || die "Linked backup directory"
|
|
# Only the oldest backup is discarded; the running tree is intact.
|
|
if [ -e "$PREVIOUS" ]; then
|
|
[ -d "$PREVIOUS" ] || die "Invalid backup directory"
|
|
rm -rf "$PREVIOUS"
|
|
fi
|
|
mkdir "$PREVIOUS" || die "Cannot create backup directory"
|
|
mv "$CURRENT" "$PREVIOUS/koreader" || die "Cannot save current installation"
|
|
sync
|
|
else
|
|
[ ! -e "$CURRENT" ] && has_reader "$PREVIOUS/koreader" ||
|
|
die "Cannot determine update backup state"
|
|
fi
|
|
write_phase update-promote
|
|
PHASE=update-promote
|
|
fi
|
|
if [ "$PHASE" = update-promote ]; then
|
|
has_reader "$PREVIOUS/koreader" || die "Update backup is missing"
|
|
if [ -d "$STAGE/koreader" ]; then
|
|
[ ! -e "$CURRENT" ] || die "Update destination already exists"
|
|
mv "$STAGE/koreader" "$CURRENT" || die "Cannot promote update"
|
|
sync
|
|
fi
|
|
has_reader "$CURRENT" || die "Promoted installation is incomplete"
|
|
write_phase update-finish
|
|
PHASE=update-finish
|
|
fi
|
|
if [ "$PHASE" = rollback-save ]; then
|
|
has_reader "$PREVIOUS/koreader" || die "Rollback backup is missing"
|
|
if has_reader "$CURRENT"; then
|
|
[ ! -e "$STAGE/koreader" ] || die "Rollback staging collision"
|
|
mv "$CURRENT" "$STAGE/koreader" || die "Cannot stage current installation"
|
|
sync
|
|
else
|
|
[ ! -e "$CURRENT" ] && has_reader "$STAGE/koreader" ||
|
|
die "Cannot determine rollback state"
|
|
fi
|
|
write_phase rollback-restore
|
|
PHASE=rollback-restore
|
|
fi
|
|
if [ "$PHASE" = rollback-restore ]; then
|
|
has_reader "$STAGE/koreader" || die "Rollback staging tree is missing"
|
|
if [ -d "$PREVIOUS/koreader" ]; then
|
|
[ ! -e "$CURRENT" ] || die "Rollback destination already exists"
|
|
mv "$PREVIOUS/koreader" "$CURRENT" || die "Cannot restore backup"
|
|
sync
|
|
fi
|
|
has_reader "$CURRENT" || die "Restored installation is incomplete"
|
|
write_phase rollback-finish
|
|
PHASE=rollback-finish
|
|
fi
|
|
if [ "$PHASE" = rollback-finish ]; then
|
|
if [ -d "$STAGE/koreader" ]; then
|
|
[ ! -e "$PREVIOUS/koreader" ] || die "Rollback backup collision"
|
|
mv "$STAGE/koreader" "$PREVIOUS/koreader" || die "Cannot retain displaced installation"
|
|
sync
|
|
fi
|
|
has_reader "$CURRENT" && has_reader "$PREVIOUS/koreader" ||
|
|
die "Rollback snapshots are incomplete"
|
|
fi
|
|
has_reader "$CURRENT" || die "Transaction has no runnable installation"
|
|
remove_stage || die "Cannot clean transaction staging"
|
|
rm -f "$JOURNAL" "$JOURNAL.new" || die "Cannot finish transaction"
|
|
sync
|
|
log "Transaction complete"
|
|
}
|
|
|
|
# Strict ASCII release paths; spaces are supported, shell metacharacters and
|
|
# ambiguous tar escaping are not. Runtime filenames need not follow this rule.
|
|
valid_paths() {
|
|
awk '
|
|
!/^[A-Za-z0-9_.\/+@=,() -]+$/ { exit 1 }
|
|
/^\// || /\/\// || /(^|\/)\.\.?($|\/)/ { exit 1 }
|
|
{ if (length($0) == 0) exit 1 }
|
|
' "$1"
|
|
}
|
|
valid_manifest() {
|
|
[ -f "$1" ] && [ ! -L "$1" ] && [ -s "$1" ] || return 1
|
|
valid_paths "$1" || return 1
|
|
# Manifests describe files, never directories. Require the shipped sentinel.
|
|
awk '/\/$/ { exit 1 }' "$1" || return 1
|
|
grep -qxF booksprite-files.txt "$1" || return 1
|
|
sort -u "$1" >"$STAGE/manifest.sorted" || return 1
|
|
cmp -s "$1" "$STAGE/manifest.sorted"
|
|
}
|
|
reject_update() {
|
|
log "Update not installed: $* (current and backup unchanged)"
|
|
if [ -f "$STAGE/payload.tar.gz" ]; then
|
|
mv "$STAGE/payload.tar.gz" "$HOME_DIR/rejected.tar.gz" ||
|
|
die "Cannot retain rejected archive"
|
|
fi
|
|
remove_stage || die "Cannot clean rejected staging"
|
|
}
|
|
|
|
stage_update() {
|
|
mkdir "$STAGE" || { log "Cannot create staging directory"; return; }
|
|
if ! mv "$HOME_DIR/pending.tar.gz" "$STAGE/payload.tar.gz"; then
|
|
reject_update "cannot claim pending archive"; return
|
|
fi
|
|
ARCHIVE=$STAGE/payload.tar.gz
|
|
# Validate both names and types BEFORE extraction. The official Kobo release
|
|
# contains no links; rejecting all links avoids tar traversal through parents.
|
|
if ! tar -tzf "$ARCHIVE" >"$STAGE/members" 2>"$STAGE/tar-error" ||
|
|
! valid_paths "$STAGE/members" ||
|
|
! awk '$0 != "koreader/" && $0 !~ /^koreader\/.+/ { exit 1 }' "$STAGE/members" ||
|
|
! tar -tvzf "$ARCHIVE" >"$STAGE/types" 2>>"$STAGE/tar-error" ||
|
|
! awk 'substr($0,1,1) != "-" && substr($0,1,1) != "d" { exit 1 }' "$STAGE/types"; then
|
|
reject_update "unsafe or corrupt archive"; return
|
|
fi
|
|
sort "$STAGE/members" | uniq -d >"$STAGE/duplicates"
|
|
if [ -s "$STAGE/duplicates" ] || [ ! -s "$STAGE/members" ]; then
|
|
reject_update "empty archive or duplicate members"; return
|
|
fi
|
|
# Demand at least 512 MiB free, or the full new payload + current tree +
|
|
# 64 MiB headroom, whichever is greater. The current tree is a conservative
|
|
# upper bound for mutable data copied into the new payload.
|
|
AVAILABLE=$(df -Pk "$HOME_DIR" | awk 'END { print $(NF-2) }')
|
|
OLD_SIZE=$(du -sk "$CURRENT" 2>/dev/null | awk '{print $1}')
|
|
NEEDED=$(awk -v old="$OLD_SIZE" '
|
|
{ if ($3 !~ /^[0-9]+$/) bad=1; bytes += $3 }
|
|
END {
|
|
n = int(bytes / 1024) + 1 + old + 65536;
|
|
if (n < 524288) n = 524288;
|
|
if (bad) print "invalid"; else printf "%.0f\n", n;
|
|
}' "$STAGE/types")
|
|
for value in "$AVAILABLE" "$OLD_SIZE" "$NEEDED"; do
|
|
case "$value" in
|
|
''|*[!0-9]*) reject_update "cannot determine free space"; return ;;
|
|
esac
|
|
done
|
|
if [ "$AVAILABLE" -lt "$NEEDED" ]; then
|
|
reject_update "insufficient free space (need ${NEEDED} KiB, have ${AVAILABLE} KiB)"; return
|
|
fi
|
|
if ! tar -xzf "$ARCHIVE" -C "$STAGE" 2>>"$STAGE/tar-error"; then
|
|
reject_update "extraction failed"; return
|
|
fi
|
|
if ! has_reader "$STAGE/koreader" ||
|
|
[ ! -f "$STAGE/koreader/reader.lua" ] ||
|
|
! valid_manifest "$STAGE/koreader/booksprite-files.txt" ||
|
|
! valid_manifest "$CURRENT/booksprite-files.txt"; then
|
|
reject_update "missing launcher/reader or invalid release manifest"; return
|
|
fi
|
|
# A manifest cannot claim files outside the payload or omit shipped files.
|
|
if ! (CDPATH='' cd "$STAGE/koreader" && find . -type f) >"$STAGE/new-files.raw" ||
|
|
! sed 's|^\./||' "$STAGE/new-files.raw" >"$STAGE/new-files" ||
|
|
! sort "$STAGE/new-files" >"$STAGE/actual"; then
|
|
reject_update "cannot enumerate payload"; return
|
|
fi
|
|
if ! cmp -s "$STAGE/koreader/booksprite-files.txt" "$STAGE/actual"; then
|
|
reject_update "release manifest does not match payload"; return
|
|
fi
|
|
# FAT has no symlinks. Refuse links on test/Unix installations too, rather
|
|
# than following a user-created link outside either installation.
|
|
if ! find "$CURRENT" -type l >"$STAGE/links" || [ -s "$STAGE/links" ]; then
|
|
reject_update "current installation contains links"; return
|
|
fi
|
|
if ! (CDPATH='' cd "$CURRENT" && find . -type f) >"$STAGE/old-files.raw" ||
|
|
! sed 's|^\./||' "$STAGE/old-files.raw" >"$STAGE/old-files"; then
|
|
reject_update "cannot enumerate runtime data"; return
|
|
fi
|
|
if ! awk 'FILENAME != ARGV[3] { tracked[$0]=1; next } !($0 in tracked)' \
|
|
"$CURRENT/booksprite-files.txt" "$STAGE/koreader/booksprite-files.txt" \
|
|
"$STAGE/old-files" >"$STAGE/copy-files"; then
|
|
reject_update "cannot identify mutable data"; return
|
|
fi
|
|
# Explicit mutable paths may intentionally override shipped defaults. User
|
|
# plugins, fonts, defaults.custom.lua, databases etc. are otherwise untracked.
|
|
if ! awk '/^(settings\/|history\/|cache\/|clipboard\/|patches\/|data\/dict\/|data\/tessdata\/)/ ||
|
|
/^(settings\.reader\.lua|settings\.reader\.lua\.old)$/' \
|
|
"$STAGE/old-files" >>"$STAGE/copy-files" ||
|
|
! sort -u "$STAGE/copy-files" >"$STAGE/copy.sorted"; then
|
|
reject_update "cannot identify runtime overlays"; return
|
|
fi
|
|
COPY_OK=true
|
|
while IFS= read -r file; do
|
|
# A path is from find, not the archive. Prefix absolute paths and quote
|
|
# every operation; never evaluate filenames or copy directory links.
|
|
if [ -d "$STAGE/koreader/$file" ] ||
|
|
! mkdir -p "$(dirname "$STAGE/koreader/$file")" ||
|
|
! cp -p "$CURRENT/$file" "$STAGE/koreader/$file"; then
|
|
COPY_OK=false
|
|
break
|
|
fi
|
|
done <"$STAGE/copy.sorted"
|
|
if [ "$COPY_OK" != true ]; then
|
|
reject_update "cannot preserve mutable data"; return
|
|
fi
|
|
sync
|
|
write_phase update-save
|
|
recover
|
|
}
|
|
|
|
recover
|
|
# No journal means an interruption happened before commit. Requeue the claimed
|
|
# archive, but never replace a newer USB-uploaded pending archive.
|
|
if [ -e "$STAGE" ]; then
|
|
[ -d "$STAGE" ] || die "Invalid staging directory"
|
|
if [ -f "$STAGE/payload.tar.gz" ]; then
|
|
if [ -e "$HOME_DIR/pending.tar.gz" ]; then
|
|
mv "$STAGE/payload.tar.gz" "$HOME_DIR/rejected.tar.gz" ||
|
|
die "Cannot retain abandoned archive"
|
|
else
|
|
mv "$STAGE/payload.tar.gz" "$HOME_DIR/pending.tar.gz" ||
|
|
die "Cannot requeue abandoned archive"
|
|
fi
|
|
fi
|
|
remove_stage || die "Cannot clean abandoned staging"
|
|
fi
|
|
rm -f "$JOURNAL.new"
|
|
|
|
if [ "${1:-}" = --rollback ]; then
|
|
shift
|
|
if [ "$RECOVERED_ROLLBACK" = true ]; then
|
|
# A repeated NickelMenu action completes the interrupted swap, rather
|
|
# than swapping straight back to the version the user wanted to leave.
|
|
:
|
|
elif ! has_reader "$PREVIOUS/koreader"; then
|
|
die "No previous installation; restore over USB"
|
|
elif [ ! -e "$CURRENT" ]; then
|
|
# Recovery from a manually removed/broken current directory.
|
|
mv "$PREVIOUS/koreader" "$CURRENT" || die "Cannot restore previous installation"
|
|
sync
|
|
else
|
|
has_reader "$CURRENT" || die "Current directory is incomplete; recover over USB"
|
|
mkdir "$STAGE" || die "Cannot create rollback staging"
|
|
write_phase rollback-save
|
|
recover
|
|
fi
|
|
log "Manual rollback complete; pending update was not applied"
|
|
else
|
|
has_reader "$CURRENT" || die "Installation missing; use --rollback or restore over USB"
|
|
if [ -f "$HOME_DIR/pending.tar.gz" ]; then
|
|
stage_update
|
|
fi
|
|
fi
|
|
|
|
has_reader "$CURRENT" || die "No runnable installation"
|
|
export KOREADER_DIR="$CURRENT"
|
|
log "Starting $KOREADER_DIR"
|
|
# Upstream owns 85 (restart), 86 (USBMS), 88 (halt), crash retry, framebuffer
|
|
# restoration, and return to Nickel. Do not reinterpret them as update failure.
|
|
sh "$CURRENT/koreader.sh" "$@" &
|
|
CHILD=$!
|
|
wait "$CHILD"
|
|
STATUS=$?
|
|
CHILD=
|
|
log "KOReader exited with status $STATUS; no automatic rollback"
|
|
exit "$STATUS"
|