2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/platform/common/spinning_zsync
NiLuJe de15ab3a87
Unbreak OTA on crappy shells (#5850)
* Unbreak OTA on crappy shells

  Very, very old ash versions abort on set -o failures (as mandated by
POSIX, granted). This makes it impossible to reliably catch failures in
a single step.

So do it from the Lua side instead.

Should fix #5844

* Launch the zsync wrapper through bash on Cervantes
2020-02-12 00:07:05 +01:00

53 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
# We're going to need to remember failures from the *left* side of a pipeline...
# But because we can't have nice things, some devices (hi, Kindle) may use an extremely old busybox build,
# one in which pipefail was not yet implemented,
# (it appeared in busybox 1.16.0, and is contingent on bash compatibility being enabled in ash).
# NOTE: We inherit WITH_PIPEFAIL from the env via OTAManager, because of course old busybox ash versions abort on set -o failures...
# c.f., https://github.com/koreader/koreader/pull/5844
if [ "${WITH_PIPEFAIL}" = "true" ]; then
# shellcheck disable=SC2039
set -o pipefail
fi
# Figure out whether that's a delta or a full download given the number of arguments passed...
if [ $# -lt 7 ]; then
ZSYNC_MESSAGE="Downloading update data"
else
ZSYNC_MESSAGE="Computing zsync delta"
fi
# Small zsync wrapper so we can print its output while it works...
./fbink -q -y -7 -pmh "${ZSYNC_MESSAGE} . . ."
# Clear any potential leftover from the local OTA tarball creation.
./fbink -q -y -6 -pm ' '
# Launch zsync2, and remember its exit code...
# We'll be piping its output to FBInk in order to print it live, in a smaller font than usual...
# NOTE: This depends on an undocumented FBInk hack (-z) in order to deal with the progress bars (and their CR) sanely.
rc=0
if [ "${WITH_PIPEFAIL}" = "true" ]; then
# We can use pipefail, let the shell do the heavy lifting for us...
./zsync2 "$@" 2>&1 | tee -a ./crash.log | ./fbink -q -z -pm -F scientifica
rc=$?
else
# We cannot use pipefail, extreme shell wizardry is required...
# Pure magic courtesy of https://unix.stackexchange.com/a/70675
# Much more compact than the stock answer from the Usenet shell FAQ: http://cfajohnson.com/shell/cus-faq-2.html#Q11
{ { { {
./zsync2 "$@" 2>&1 3>&- 4>&-
echo $? >&3
} | ./fbink -q -z -pm -F scientifica >&4; } 3>&1; } | {
read -r xs
exit "${xs}"
}; } 4>&1
rc=$?
fi
# And return with zsync's exit code
exit ${rc}