diff --git a/src/check.sh b/src/check.sh index 4e964671..5e72f981 100644 --- a/src/check.sh +++ b/src/check.sh @@ -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") diff --git a/src/disk.sh b/src/disk.sh index 7bf4a9dd..83f6fadf 100644 --- a/src/disk.sh +++ b/src/disk.sh @@ -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 \ @@ -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" @@ -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") diff --git a/src/install.sh b/src/install.sh index 5afa5567..a612adcd 100644 --- a/src/install.sh +++ b/src/install.sh @@ -5,6 +5,7 @@ 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" @@ -12,14 +13,14 @@ 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 @@ -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])..." @@ -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=$?; } || : @@ -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") @@ -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" @@ -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" diff --git a/src/network.sh b/src/network.sh index f1a71c24..f053e94c 100644 --- a/src/network.sh +++ b/src/network.sh @@ -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 @@ -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/') diff --git a/src/power.sh b/src/power.sh index 678895ff..99079798 100644 --- a/src/power.sh +++ b/src/power.sh @@ -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..." @@ -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 @@ -64,7 +64,7 @@ terminal() { local dev="" - if [ -f "$QEMU_OUT" ]; then + if [ -s "$QEMU_OUT" ]; then local msg msg=$(<"$QEMU_OUT") @@ -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 @@ -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 diff --git a/src/print.sh b/src/print.sh index 744c2b75..0d0d736b 100644 --- a/src/print.sh +++ b/src/print.sh @@ -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 @@ -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=$?; } || : diff --git a/src/progress.sh b/src/progress.sh index 007ce57b..d32e2725 100644 --- a/src/progress.sh +++ b/src/progress.sh @@ -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/')