Skip to content

Commit

Permalink
fix: Verify files are not empty (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
kroese committed Apr 16, 2024
1 parent 5015597 commit e81dc0f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ set -Eeuo pipefail
: "${NETWORK:="Y"}"

[ -f "/run/shm/qemu.end" ] && echo "QEMU is shutting down.." && exit 1
[ ! -f "/run/shm/qemu.pid" ] && echo "QEMU is not running yet.." && exit 0
[ ! -s "/run/shm/qemu.pid" ] && echo "QEMU is not running yet.." && exit 0
[[ "$NETWORK" != [Yy1]* ]] && echo "Networking is disabled.." && exit 0

file="/run/shm/dsm.url"
address="/run/shm/qemu.ip"

[ ! -f "$file" ] && echo "DSM has not enabled networking yet.." && exit 1
[ ! -s "$file" ] && echo "DSM has not enabled networking yet.." && exit 1

location=$(<"$file")

Expand Down
10 changes: 5 additions & 5 deletions src/disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ set -Eeuo pipefail
BOOT="$STORAGE/$BASE.boot.img"
SYSTEM="$STORAGE/$BASE.system.img"

[ ! -f "$BOOT" ] && error "Virtual DSM boot-image does not exist ($BOOT)" && exit 81
[ ! -f "$SYSTEM" ] && error "Virtual DSM system-image does not exist ($SYSTEM)" && exit 82
[ ! -s "$BOOT" ] && error "Virtual DSM boot-image does not exist ($BOOT)" && exit 81
[ ! -s "$SYSTEM" ] && error "Virtual DSM system-image does not exist ($SYSTEM)" && exit 82

DISK_OPTS="\
-object iothread,id=io2 \
Expand Down Expand Up @@ -386,7 +386,7 @@ addDisk () {
FS=$(stat -f -c %T "$DIR")
checkFS "$FS" "$DISK_FILE" "$DISK_DESC" || exit $?

if ! [ -f "$DISK_FILE" ] ; then
if ! [ -s "$DISK_FILE" ] ; then

if [[ "${DISK_FMT,,}" != "raw" ]]; then
PREV_FMT="raw"
Expand All @@ -395,12 +395,12 @@ addDisk () {
fi
PREV_EXT=$(fmt2ext "$PREV_FMT")

if [ -f "$DISK_BASE.$PREV_EXT" ] ; then
if [ -s "$DISK_BASE.$PREV_EXT" ] ; then
convertDisk "$DISK_BASE.$PREV_EXT" "$PREV_FMT" "$DISK_FILE" "$DISK_FMT" "$DISK_BASE" "$DISK_DESC" "$FS" || exit $?
fi
fi

if [ -f "$DISK_FILE" ]; then
if [ -s "$DISK_FILE" ]; then

CUR_SIZE=$(getSize "$DISK_FILE")

Expand Down
18 changes: 10 additions & 8 deletions src/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ set -Eeuo pipefail

if [ -f "$STORAGE/dsm.ver" ]; then
BASE=$(<"$STORAGE/dsm.ver")
[ -z "$BASE" ] && BASE="DSM_VirtualDSM_69057"
else
# Fallback for old installs
BASE="DSM_VirtualDSM_42962"
fi

if [ -n "$URL" ]; then
BASE=$(basename "$URL" .pat)
if [ ! -f "$STORAGE/$BASE.system.img" ]; then
if [ ! -s "$STORAGE/$BASE.system.img" ]; then
BASE=$(basename "${URL%%\?*}" .pat)
: "${BASE//+/ }"; printf -v BASE '%b' "${_//%/\\x}"
BASE=$(echo "$BASE" | sed -e 's/[^A-Za-z0-9._-]/_/g')
fi
fi

if [[ -f "$STORAGE/$BASE.boot.img" ]] && [[ -f "$STORAGE/$BASE.system.img" ]]; then
if [[ -s "$STORAGE/$BASE.boot.img" ]] && [[ -s "$STORAGE/$BASE.system.img" ]]; then
return 0 # Previous installation found
fi

Expand Down Expand Up @@ -106,7 +107,7 @@ fi
ROOT="Y"
RDC="$STORAGE/dsm.rd"

if [ ! -f "$RDC" ]; then
if [ ! -s "$RDC" ]; then

MSG="Downloading installer..."
PRG="Downloading installer ([P])..."
Expand All @@ -118,6 +119,7 @@ if [ ! -f "$RDC" ]; then
LOC="$DL/release/7.0.1/42218/DSM_VirtualDSM_42218.pat"

rm -f "$RD"
rm -f "$RDC"
/run/progress.sh "$RD" "$PRG" &
{ curl -r "$POS" -sfk -S -o "$RD" "$LOC"; rc=$?; } || :

Expand Down Expand Up @@ -215,7 +217,7 @@ else

fi

[ ! -f "$PAT" ] && error "Failed to download $URL" && exit 69
[ ! -s "$PAT" ] && error "Failed to download $URL" && exit 69

SIZE=$(stat -c%s "$PAT")

Expand Down Expand Up @@ -252,7 +254,7 @@ MSG="Preparing system partition..."
info "Install: $MSG" && html "$MSG"

BOOT=$(find "$TMP" -name "*.bin.zip")
[ ! -f "$BOOT" ] && error "The PAT file contains no boot image." && exit 67
[ ! -s "$BOOT" ] && error "The PAT file contains no boot image." && exit 67

BOOT=$(echo "$BOOT" | head -c -5)
unzip -q -o "$BOOT".zip -d "$TMP"
Expand Down Expand Up @@ -313,15 +315,15 @@ IDB="$TMP/indexdb"
PKG="$TMP/packages"
HDP="$TMP/synohdpack_img"

[ ! -f "$HDA.tgz" ] && error "The PAT file contains no OS image." && exit 64
[ ! -s "$HDA.tgz" ] && error "The PAT file contains no OS image." && exit 64
mv "$HDA.tgz" "$HDA.txz"

[ -d "$PKG" ] && mv "$PKG/" "$MOUNT/.SynoUpgradePackages/"
rm -f "$MOUNT/.SynoUpgradePackages/ActiveInsight-"*

[ -f "$HDP.txz" ] && tar xpfJ "$HDP.txz" --absolute-names -C "$MOUNT/"
[ -s "$HDP.txz" ] && tar xpfJ "$HDP.txz" --absolute-names -C "$MOUNT/"

if [ -f "$IDB.txz" ]; then
if [ -s "$IDB.txz" ]; then
INDEX_DB="$MOUNT/usr/syno/synoman/indexdb/"
mkdir -p "$INDEX_DB"
tar xpfJ "$IDB.txz" --absolute-names -C "$INDEX_DB"
Expand Down
4 changes: 2 additions & 2 deletions src/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ closeNetwork() {
else

local pid="/var/run/dnsmasq.pid"
[ -f "$pid" ] && pKill "$(<"$pid")"
[ -s "$pid" ] && pKill "$(<"$pid")"

ip link set "$VM_NET_TAP" down promisc off || true
ip link delete "$VM_NET_TAP" || true
Expand Down Expand Up @@ -228,7 +228,7 @@ getInfo() {

if [ -z "$VM_NET_MAC" ]; then
local file="$STORAGE/dsm.mac"
[ -f "$file" ] && VM_NET_MAC=$(<"$file")
[ -s "$file" ] && VM_NET_MAC=$(<"$file")
if [ -z "$VM_NET_MAC" ]; then
# Generate MAC address based on Docker container ID in hostname
VM_NET_MAC=$(echo "$HOST" | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:11:32:\3:\4:\5/')
Expand Down
10 changes: 5 additions & 5 deletions src/power.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ finish() {

touch "$QEMU_END"

if [ -f "$QEMU_PID" ]; then
if [ -s "$QEMU_PID" ]; then

pid=$(<"$QEMU_PID")
echo && error "Forcefully terminating QEMU process, reason: $reason..."
Expand All @@ -45,7 +45,7 @@ finish() {
while isAlive "$pid"; do
sleep 1
# Workaround for zombie pid
[ ! -f "$QEMU_PID" ] && break
[ ! -s "$QEMU_PID" ] && break
done
fi

Expand All @@ -64,7 +64,7 @@ terminal() {

local dev=""

if [ -f "$QEMU_OUT" ]; then
if [ -s "$QEMU_OUT" ]; then

local msg
msg=$(<"$QEMU_OUT")
Expand Down Expand Up @@ -113,7 +113,7 @@ _graceful_shutdown() {
touch "$QEMU_END"
echo && info "Received $1 signal, sending shutdown command..."

if [ ! -f "$QEMU_PID" ]; then
if [ ! -s "$QEMU_PID" ]; then
echo && error "QEMU PID file does not exist?"
finish "$code" && return "$code"
fi
Expand Down Expand Up @@ -157,7 +157,7 @@ _graceful_shutdown() {
[[ "$DEBUG" == [Yy1]* ]] && info "Shutting down, waiting... ($cnt/$QEMU_TIMEOUT)"

# Workaround for zombie pid
[ ! -f "$QEMU_PID" ] && break
[ ! -s "$QEMU_PID" ] && break

done

Expand Down
4 changes: 2 additions & 2 deletions src/print.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ resp_err="Guest returned an invalid response:"
curl_err="Failed to connect to guest: curl error"
jq_err="Failed to parse response from guest: jq error"

while [ ! -f "$file" ]
while [ ! -s "$file" ]
do

# Check if not shutting down
Expand All @@ -30,7 +30,7 @@ do
sleep 3

[ -f "$shutdown" ] && exit 1
[ -f "$file" ] && break
[ -s "$file" ] && break

# Retrieve network info from guest VM
{ json=$(curl -m 20 -sk "$url"); rc=$?; } || :
Expand Down
2 changes: 1 addition & 1 deletion src/progress.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fi

while true
do
if [ -f "$file" ]; then
if [ -s "$file" ]; then
bytes=$(du -sb "$file" | cut -f1)
if (( bytes > 1000 )); then
size=$(echo "$bytes" | numfmt --to=iec --suffix=B | sed -r 's/([A-Z])/ \1/')
Expand Down

0 comments on commit e81dc0f

Please sign in to comment.