Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
bootiso: simplify a few instances of return status handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamr committed May 30, 2020
1 parent 6c6b3c6 commit abda462
Showing 1 changed file with 30 additions and 51 deletions.
81 changes: 30 additions & 51 deletions bootiso
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ function term_printLog() {
function term_indentAll() {
while read -r line; do
term_printColumn " " "$term_logPrefixLength" "$line"
done <"${1:-/dev/stdin}"
done < "${1:-/dev/stdin}"
}

# $*: The message to print.
Expand Down Expand Up @@ -423,29 +423,15 @@ function fs_findFileFromPatterns() {

function fs_createTempFile() {
local -r _prefix=$1
local _tmpFileTemplate="$ct_tempRoot/$_prefix-XXX" _status
mktemp "$_tmpFileTemplate"
_status=$?
if [ ! $_status -eq 0 ]; then
ps_failAndExit IO_ERROR "Failed to create temporary file."
fi
local _tmpFileTemplate="$ct_tempRoot/$_prefix-XXX"
mktemp "$_tmpFileTemplate" || ps_failAndExit IO_ERROR "Failed to create temporary file."
}
# Print the name of the new folder if operation
# succeeded, fails otherwise.
#
# $1 - The folder name prefix
function fs_createMountFolder() {
local _tmpFileTemplate _status
if ((EUID == 0)); then
_tmpFileTemplate="$ct_mountRoot/$1-XXX"
else
_tmpFileTemplate="$ct_tempRoot/$1-XXX"
fi
mktemp -d "$_tmpFileTemplate"
_status=$?
if [ ! $_status -eq 0 ]; then
ps_failAndExit IO_ERROR "Failed to create temporary mount point with pattern '$_tmpFileTemplate'."
fi
mktemp -d "$ct_mountRoot/$1-XXX" || ps_failAndExit IO_ERROR "Failed to create temporary mount point with pattern '$_tmpFileTemplate'."
}

function fs_syncdev() {
Expand Down Expand Up @@ -494,15 +480,15 @@ function fs_mountUSB() {
st_usbMountPoint=$(fs_createMountFolder usb) || exit "$?"
st_temporaryAssets+=("$st_usbMountPoint")
term_echoinfo "Created USB device mount point at '$st_usbMountPoint'"
if ! mount -t "$_type" "$st_targetPartition" "$st_usbMountPoint" >/dev/null; then
if ! mount -t "$_type" "$st_targetPartition" "$st_usbMountPoint" > /dev/null; then
ps_failAndExit IO_ERROR "Could not mount USB device."
fi
}

# $1 - mountPoint
function fs_mountElToritoFile() {
local -r _mountPoint="$1"
if ! mount -r -o loop -- "$sourceImageFile" "$_mountPoint" >/dev/null; then
if ! mount -r -o loop -- "$sourceImageFile" "$_mountPoint" > /dev/null; then
ps_failAndExit IO_ERROR "Could not mount image file."
else
st_temporaryAssets+=("$_mountPoint")
Expand Down Expand Up @@ -601,7 +587,7 @@ function fs_syncWithProgress() {

# $1 - The name of the command to check in $PATH.
function sys_hasCommand() {
command -v "$1" &>/dev/null
command -v "$1" &> /dev/null
return $?
}

Expand Down Expand Up @@ -942,7 +928,7 @@ function asrt_checkSudo() {
if [[ -t 1 ]] && sys_hasCommand sudo; then
sudo --preserve-env "$0" "$@"
elif sys_hasCommand gksu; then
exec 1>output_file
exec 1> output_file
gksu --preserve-env "$0" "$@"
else
ps_failAndExit MISSING_PRIVILEGE "You must run $scriptName as root."
Expand Down Expand Up @@ -989,15 +975,14 @@ function asrt_checkImageHash() {
printf "%s" \
"You can disable this check with $(term_boldify "-H, --no-hash-check") flags." | term_indentAll
st_temporaryAssets+=("$_hashStoreFile")
(
(
local _hash
local -i _status=0
local -i
_hash=$($_hashName "$_imageName" | awk "{print \$1; exit }")
_status=$?
if ((_status == 0)); then
printf "%s" "$_hash" >"$_hashStoreFile"
if (($? == 0)); then
printf "%s" "$_hash" > "$_hashStoreFile"
else
printf "%s" 1 >"$_hashStoreFile"
printf "%s" 1 > "$_hashStoreFile"
fi
) &
st_backgroundProcess=$!
Expand All @@ -1015,7 +1000,7 @@ function asrt_checkImageHash() {
local -r _hashPath=$1 # Path to file containing _hashes
local -r _imageName=$2 # File to be checked
local -r _hashName=$3 # Name of command of _hash
local _status _answer
local _answer
# Hash from _hash file
local _gHash
_gHash=$(awk -v pattern="$_imageName$" '$0 ~ pattern { print $1; exit }' "$_hashPath")
Expand All @@ -1042,11 +1027,7 @@ function asrt_checkImageHash() {
esac
fi
# Hash from iso
_computeHashWithProgress $_hashName "$_imageName"
_status=$?
if ((_status != 0)); then
ps_failAndExit THIRD_PARTY_ERROR "$_hashName command failed with status $_status"
fi
_computeHashWithProgress $_hashName "$_imageName" || ps_failAndExit THIRD_PARTY_ERROR "$_hashName command failed with status $?"
if [ "$_gHash" != "$_lHash" ]; then
if [ "$enableForceHashCheck" == 'true' ]; then
ps_failAndExit ASSERTION_FAILED "Hash mismatch in '$_hashPath' (${_hashName%sum})."
Expand Down Expand Up @@ -1102,11 +1083,11 @@ function asrt_checkPackages() {
sys_checkCommand "$_pkg"
done
# test grep supports -P option
if ! echo 1 | grep -P '1' &>/dev/null; then
if ! echo 1 | grep -P '1' &> /dev/null; then
ps_failAndExit MISSING_DEPENDENCY \
"You're using an old version of grep which does not support perl regular expression (-P option)."
fi
if echo "" | column -t -N t -W t &>/dev/null; then
if echo "" | column -t -N t -W t &> /dev/null; then
st_hasLegacyColumn=false
else
# Old BSD command, see https://git.io/JfauE
Expand Down Expand Up @@ -1193,7 +1174,7 @@ function asrt_checkFSType() {
ps_failAndExit SYNOPSIS_NONCOMPL "Filesystem type '$_fsType' not supported." \
"Supported filesystem types: $(sh_joinBy "," "${asrt_supportedFS[*]}")."
fi
if ! command -v "mkfs.$_fsType" &>/dev/null; then
if ! command -v "mkfs.$_fsType" &> /dev/null; then
ps_failAndExit MISSING_DEPENDENCY \
"Program 'mkfs.$_fsType' could not be found on your system." \
"Please install it and retry."
Expand Down Expand Up @@ -1396,7 +1377,7 @@ function devi_configureLabel() {
# Fallback to "USER_VENDOR" if format
if [[ "$targetAction" == format ]]; then
_user=${SUDO_USER:-$USER}
_vendor=$(lsblk -ldno VENDOR "$targetDevice" 2>/dev/null)
_vendor=$(lsblk -ldno VENDOR "$targetDevice" 2> /dev/null)
_vendor=${_vendor:-FLASH}
targetPartitionLabel=${targetPartitionLabel:-"${_user^^}_${_vendor^^}"}
else
Expand Down Expand Up @@ -1549,7 +1530,7 @@ function devi_partitionUSB() {
# unmount any partition on selected device
mapfile -t devicePartitions < <(grep -oP "^\\K$targetDevice\\S*" /proc/mounts)
for _partition in "${devicePartitions[@]}"; do
if ! umount "$_partition" >/dev/null; then
if ! umount "$_partition" > /dev/null; then
ps_failAndExit IO_ERROR \
"Failed to unmount $_partition. It's likely that the partition is busy."
fi
Expand All @@ -1558,7 +1539,7 @@ function devi_partitionUSB() {
function _eraseDevice() {
term_echoinfo "Erasing contents of '$targetDevice'..."
# clean signature from selected device
wipefs --all --force "$targetDevice" &>/dev/null
wipefs --all --force "$targetDevice" &> /dev/null
# erase drive
dd if=/dev/zero of="$targetDevice" bs=512 count=1 conv=notrunc status=none |&
term_indentAll ||
Expand Down Expand Up @@ -1792,10 +1773,8 @@ function devi_inspectHybridImage() {
_supportsEFIBoot=false
fi
}
_diskReport=$(sfdisk -lJ -- "$sourceImageFile" 2>/dev/null)
if (($? != 0)); then
ps_failAndExit IO_ERROR "sfdisk couldn't read the partition table on the image file, which is likely corrupted."
fi
_diskReport=$(sfdisk -lJ -- "$sourceImageFile" 2> /dev/null) \
|| ps_failAndExit IO_ERROR "sfdisk couldn't read the partition table on the image file, which is likely corrupted."
_partScheme=$(echo "$_diskReport" | jq -r '.partitiontable.label')
case $_partScheme in
dos) _inspectMBRPartTable ;;
Expand Down Expand Up @@ -1926,7 +1905,7 @@ function step_installBootloader() {
fi
fi
term_echoinfo "Found local SYSLINUX version '$_localSyslinuxVersion'"
sh_compute "$_localSyslinuxVersion == ${st_isoInspections[syslinuxVer]}" >/dev/null
sh_compute "$_localSyslinuxVersion == ${st_isoInspections[syslinuxVer]}" > /dev/null
_versionsMatch=$?
if ((_versionsMatch == 0)); then
term_echogood "image SYSLINUX version matches local version."
Expand Down Expand Up @@ -1972,7 +1951,7 @@ function step_installBootloader() {
function _installWtKernelOrgExtlinux() {
local _isExt32 _isHost32 _fallbackToLocal=false
term_echoinfo "Installing SYSLINUX bootloader in '$_syslinuxFolder' with kernel.org version '$st_targetSyslinuxVersion'..."
file -b -- "${st_syslinuxBinaries['extBin']}" | awk '{print $2}' | grep -e '^32' &>/dev/null
file -b -- "${st_syslinuxBinaries['extBin']}" | awk '{print $2}' | grep -e '^32' &> /dev/null
_isExt32=$?
echo "$ct_architecture" | grep -e '32|i386'
_isHost32=$?
Expand All @@ -1996,7 +1975,7 @@ function step_installBootloader() {
fi
fi
if [[ "$_fallbackToLocal" == true ]]; then
_installWtLocalExtlinux >/dev/null
_installWtLocalExtlinux > /dev/null
fi
fs_syncdev
}
Expand Down Expand Up @@ -2036,7 +2015,7 @@ function step_copyWithRsync() {
term_echowarn "Detected a Windows install.wim file but wimsplit has been disabled with $(term_boldify '--no-wim-split') option."
fi
fi
(
(
# shellcheck disable=SC2086
rsync -r -q -I --no-links --no-perms --no-owner --no-group $_rsyncOptions "$st_elToritoMountPoint"/. "$st_usbMountPoint"
_status=$?
Expand All @@ -2045,7 +2024,7 @@ function step_copyWithRsync() {
echo
wimlib-imagex split "$_wimFile" "$st_usbMountPoint/sources/install.swm" 1024 |& term_indentAll
fi
echo "$_status" >"$_statusFile"
echo "$_status" > "$_statusFile"
) &
st_backgroundProcess=$!
echo -n "$scriptName: Copying files from image to USB device with 'rsync' "
Expand All @@ -2071,9 +2050,9 @@ function step_copyWithDD() {
local _status
_statusFile=$(fs_createTempFile "bootiso-status")
st_temporaryAssets+=("$_statusFile")
(
(
dd if="$sourceImageFile" of="$targetDevice" bs="$targetDDBusSize" status=none
echo "$?" >"$_statusFile"
echo "$?" > "$_statusFile"
) &
st_backgroundProcess=$!
echo -n "$scriptName: Copying files from image to USB device with 'dd' "
Expand Down

0 comments on commit abda462

Please sign in to comment.