use local scope for private function vars

all vars affected have been carefully checked to be quite certain
that they are definitely local

where variable is assigned the return value of a function/command, the
local "declaration" is deliberately done on a separate line, since
`local FOO` is actually treated itself as a command rather than a
declaration; will thus always cause $? to be zero, and thus if done on
the same line as such an assignment can not only clobber $? but in doing
so unintentionally blocks failure of a command from triggering the
expected exit from having `set -e`.

also, from testing, i have found that when assigning "${@}" this must be
done on a separate line confusingly as otherwise an error occurs.

Gbp-Dch: Short
This commit is contained in:
Lyndon Brown 2020-02-20 06:58:11 +00:00
parent af040d7803
commit c55eb8a0c3
23 changed files with 141 additions and 84 deletions

View File

@ -19,6 +19,7 @@ In_list ()
local NEEDLE="${1}"
shift
local ITEM
for ITEM in ${@}
do
if [ "${NEEDLE}" = "${ITEM}" ]
@ -32,6 +33,7 @@ In_list ()
Truncate ()
{
local FILE
for FILE in ${@}
do
if [ ! -L ${FILE} ]

View File

@ -11,9 +11,11 @@
Check_architectures ()
{
ARCHITECTURES="${@}"
VALID=false
local ARCHITECTURES
ARCHITECTURES="${@}" #must be on separate line to 'local' declaration to avoid error
local VALID=false
local ARCHITECTURE
for ARCHITECTURE in ${ARCHITECTURES}
do
if [ "$(echo ${LB_ARCHITECTURES} | grep ${ARCHITECTURE})" ]
@ -50,6 +52,7 @@ Check_architectures ()
Check_crossarchitectures ()
{
local HOST
if [ $(which dpkg) ]
then
HOST="$(dpkg --print-architecture)"
@ -57,6 +60,7 @@ Check_crossarchitectures ()
HOST="$(uname -m)"
fi
local CROSS
case "${HOST}" in
amd64|i386|x86_64)
CROSS="amd64 i386"

View File

@ -11,6 +11,7 @@
Arguments ()
{
local ARGUMENTS
local ERR=0
ARGUMENTS="$(getopt --longoptions breakpoints,color,no-color,conffile:,debug,force,help,quiet,usage,verbose,version --name=${PROGRAM} --options c:huv --shell sh -- "${@}")" || ERR=$?

View File

@ -10,8 +10,9 @@
Is_Requested_Bootloader ()
{
OLDIFS="$IFS"
IFS=","
local OLDIFS="$IFS"
local IFS=","
local BOOTLOADER
for BOOTLOADER in ${LB_BOOTLOADERS}; do
if [ "${BOOTLOADER}" = "${1}" ]; then
IFS="$OLDIFS"

View File

@ -11,7 +11,7 @@
Breakpoint ()
{
NAME="${1}"
local NAME="${1}"
if [ "${_BREAKPOINTS}" = "true" ]
then

View File

@ -11,7 +11,7 @@
Restore_package_cache ()
{
DIRECTORY="cache/packages.${1}"
local DIRECTORY="cache/packages.${1}"
if [ "${LB_CACHE}" = "true" ] && [ "${LB_CACHE_PACKAGES}" = "true" ]
then
@ -32,7 +32,7 @@ Restore_package_cache ()
Save_package_cache ()
{
DIRECTORY="cache/packages.${1}"
local DIRECTORY="cache/packages.${1}"
if [ "${LB_CACHE}" = "true" ] && [ "${LB_CACHE_PACKAGES}" = "true" ]
then
@ -47,6 +47,7 @@ Save_package_cache ()
mkdir -p "${DIRECTORY}"
# Saving new cache
local PACKAGE
for PACKAGE in chroot/var/cache/apt/archives/*.deb
do
if [ -e "${DIRECTORY}"/"$(basename ${PACKAGE})" ]

View File

@ -11,14 +11,16 @@
Chroot ()
{
CHROOT="${1}"; shift
COMMANDS="${@}"
local CHROOT="${1}"; shift
local COMMANDS
COMMANDS="${@}" #must be on separate line to 'local' declaration to avoid error
# Executing commands in chroot
Echo_debug "Executing: %s" "${COMMANDS}"
ENV=""
local _FILE
for _FILE in config/environment config/environment.chroot
do
if [ -e "${_FILE}" ]
@ -39,8 +41,8 @@ Chroot ()
}
Chroot_has_package() {
PACKAGE="${1}"; shift
CHROOT="${2:-chroot}"; shift
local PACKAGE="${1}"; shift
local CHROOT="${2:-chroot}"; shift
if dpkg-query --admindir=${CHROOT}/var/lib/dpkg -s ${PACKAGE} >/dev/null 2>&1 | grep -q "^Status: install"
then
@ -50,7 +52,7 @@ Chroot_has_package() {
}
Chroot_package_list() {
CHROOT="${1:-chroot}"; shift
local CHROOT="${1:-chroot}"; shift
dpkg-query --admindir=${CHROOT}/var/lib/dpkg -W -f'${Package}\n'
}

View File

@ -11,6 +11,9 @@
Chroot_bind_path ()
{
local CHROOT
local BIND_SRC
local BIND_DEST
CHROOT="$(readlink -f ${1})"
BIND_SRC="$(readlink -f ${2})"
@ -27,6 +30,9 @@ Chroot_bind_path ()
Chroot_unbind_path ()
{
local CHROOT
local BIND_SRC
local BIND_DEST
CHROOT="$(readlink -f ${1})"
BIND_SRC="$(readlink -f ${2})"

View File

@ -11,10 +11,12 @@
Get_conffiles ()
{
local FILES
if [ -n "${LB_CONFIG}" ]
then
FILES="${LB_CONFIG}"
else
local FILE
for FILE in ${@}
do
FILES="${FILES} ${FILE} ${FILE}.${LB_ARCHITECTURES} ${FILE}.${DISTRIBUTION}"
@ -29,6 +31,7 @@ Get_conffiles ()
Read_conffiles ()
{
local CONFFILE
for CONFFILE in $(Get_conffiles "${@}")
do
if [ -f "${CONFFILE}" ]
@ -46,6 +49,7 @@ Read_conffiles ()
Print_conffiles ()
{
local CONFFILE
for CONFFILE in $(Get_conffiles "${@}")
do
if [ -f "${CONFFILE}" ]

View File

@ -11,29 +11,30 @@
Get_configuration ()
{
_CONFIGURATION_FILE="${1}"
_FIELD_NAME="${2}"
local CONFIGURATION_FILE="${1}"
local FIELD_NAME="${2}"
local FIELD_BODY
if [ -e "${_CONFIGURATION_FILE}" ]
if [ -e "${CONFIGURATION_FILE}" ]
then
_FIELD_BODY="$(grep ^${_FIELD_NAME}: ${_CONFIGURATION_FILE} | awk '{ $1=""; print $0 }' | sed -e 's|^ ||')"
FIELD_BODY="$(grep ^${FIELD_NAME}: ${CONFIGURATION_FILE} | awk '{ $1=""; print $0 }' | sed -e 's|^ ||')"
fi
echo ${_FIELD_BODY}
echo ${FIELD_BODY}
}
Set_configuration ()
{
_CONFIGURATION_FILE="${1}"
_FIELD_NAME="${2}"
_FIELD_BODY="${3}"
local CONFIGURATION_FILE="${1}"
local FIELD_NAME="${2}"
local FIELD_BODY="${3}"
if grep -qs "^${_FIELD_NAME}:" "${_CONFIGURATION_FILE}"
if grep -qs "^${FIELD_NAME}:" "${CONFIGURATION_FILE}"
then
# Update configuration
sed -i -e "s|^${_FIELD_NAME}:.*$|${_FIELD_NAME}: ${_FIELD_BODY}|" "${_CONFIGURATION_FILE}"
sed -i -e "s|^${FIELD_NAME}:.*$|${FIELD_NAME}: ${FIELD_BODY}|" "${CONFIGURATION_FILE}"
else
# Append configuration
echo "${_FIELD_NAME}: ${_FIELD_BODY}" >> "${_CONFIGURATION_FILE}"
echo "${FIELD_NAME}: ${FIELD_BODY}" >> "${CONFIGURATION_FILE}"
fi
}

View File

@ -96,6 +96,7 @@ Set_config_defaults ()
# Setting mode (currently: debian, progress-linux)
if [ $(which lsb_release) ]
then
local _DISTRIBUTOR
_DISTRIBUTOR="$(lsb_release -is | tr "[A-Z]" "[a-z]")"
case "${_DISTRIBUTOR}" in
@ -624,6 +625,7 @@ Set_config_defaults ()
;;
esac
local _LB_BOOTAPPEND_PRESEED
if [ -n "${LB_DEBIAN_INSTALLER_PRESEEDFILE}" ]
then
case "${LIVE_IMAGE_TYPE}" in

View File

@ -12,7 +12,7 @@ exec 3>&1
Echo ()
{
STRING="${1}"
local STRING="${1}"
shift
printf "${STRING}\n" "${@}" >&3
@ -21,7 +21,7 @@ Echo ()
Echo_debug ()
{
if [ "${_DEBUG}" = "true" ]; then
STRING="${1}"
local STRING="${1}"
shift
printf "D: ${STRING}\n" "${@}" >&3
@ -30,7 +30,7 @@ Echo_debug ()
Echo_error ()
{
STRING="${1}"
local STRING="${1}"
shift
local PREFIX="${RED}E${NO_COLOR}"
@ -45,7 +45,7 @@ Echo_message ()
{
if [ "${_QUIET}" != "true" ]
then
STRING="${1}"
local STRING="${1}"
shift
local PREFIX="${PURPLE}P${NO_COLOR}"
@ -60,7 +60,7 @@ Echo_message ()
Echo_verbose ()
{
if [ "${_VERBOSE}" = "true" ]; then
STRING="${1}"
local STRING="${1}"
shift
printf "I: ${STRING}\n" "${@}" >&3
@ -69,7 +69,7 @@ Echo_verbose ()
Echo_warning ()
{
STRING="${1}"
local STRING="${1}"
shift
local PREFIX="${YELLOW}W${NO_COLOR}"
@ -82,6 +82,7 @@ Echo_warning ()
Echo_file ()
{
local LINE
while read -r LINE
do
echo "${1}: ${LINE}" >&3

View File

@ -19,6 +19,7 @@ Firmware_List_From_Contents () {
local DISTRO_CHROOT="${2}"
local ARCHIVE_AREAS="${3}"
local _ARCHIVE_AREA
for _ARCHIVE_AREA in ${ARCHIVE_AREAS}
do
local CONTENTS_URL="${MIRROR_CHROOT}/dists/${DISTRO_CHROOT}/${_ARCHIVE_AREA}/Contents-${LB_ARCHITECTURES}.gz"

View File

@ -10,8 +10,8 @@
Lodetach ()
{
DEVICE="${1}"
ATTEMPT="${2:-1}"
local DEVICE="${1}"
local ATTEMPT="${2:-1}"
if [ "${ATTEMPT}" -gt 3 ]
then
@ -37,10 +37,12 @@ Lodetach ()
Losetup ()
{
DEVICE="${1}"
FILE="${2}"
PARTITION="${3:-1}"
local DEVICE="${1}"
local FILE="${2}"
local PARTITION="${3:-1}"
local FDISK_OUT
local LOOPDEVICE
losetup --read-only --partscan "${DEVICE}" "${FILE}"
FDISK_OUT="$(fdisk -l -u ${DEVICE} 2>&1)"
Lodetach "${DEVICE}"
@ -53,6 +55,8 @@ Losetup ()
losetup --partscan "${DEVICE}" "${FILE}"
else
local SECTORS
local OFFSET
SECTORS="$(echo "$FDISK_OUT" | sed -ne "s|^$LOOPDEVICE[ *]*\([0-9]*\).*|\1|p")"
OFFSET="$(expr ${SECTORS} '*' 512)"
@ -65,7 +69,7 @@ Losetup ()
# adapted from lib/ext2fs/mkjournal.c, default block size is 4096 bytes (/etc/mke2fs.conf).
ext2fs_default_journal_size()
{
SIZE="$1"
local SIZE="$1"
if [ "${SIZE}" -lt "8" ]; then # 2048*4096
echo 0
elif [ "${SIZE}" -lt "128" ]; then # 32768*4096
@ -83,9 +87,10 @@ ext2fs_default_journal_size()
Calculate_partition_size_without_journal ()
{
WITHOUT_JOURNAL_ORIGINAL_SIZE="${1}"
WITHOUT_JOURNAL_FILESYSTEM="${2}"
local WITHOUT_JOURNAL_ORIGINAL_SIZE="${1}"
local WITHOUT_JOURNAL_FILESYSTEM="${2}"
local PERCENT
case "${WITHOUT_JOURNAL_FILESYSTEM}" in
ext2|ext3|ext4)
PERCENT="6"
@ -100,11 +105,16 @@ Calculate_partition_size_without_journal ()
Calculate_partition_size ()
{
ORIGINAL_SIZE="${1}"
FILESYSTEM="${2}"
local ORIGINAL_SIZE="${1}"
local FILESYSTEM="${2}"
case "${FILESYSTEM}" in
ext3|ext4)
local NON_JOURNAL_SIZE
local PROJECTED_JOURNAL_SIZE
local PROJECTED_PARTITION_SIZE
local PRE_FINAL_PARTITION_SIZE
local JOURNAL_SIZE
NON_JOURNAL_SIZE=$(Calculate_partition_size_without_journal ${ORIGINAL_SIZE} ${FILESYSTEM})
PROJECTED_JOURNAL_SIZE=$(ext2fs_default_journal_size ${NON_JOURNAL_SIZE})
PROJECTED_PARTITION_SIZE=$(expr ${ORIGINAL_SIZE} + ${PROJECTED_JOURNAL_SIZE})

View File

@ -11,17 +11,20 @@
Expand_packagelist ()
{
local _LB_EXPAND_QUEUE
_LB_EXPAND_QUEUE="$(basename "${1}")"
shift
while [ -n "${_LB_EXPAND_QUEUE}" ]
do
local _LB_LIST_NAME
local _LB_EXPAND_QUEUE
_LB_LIST_NAME="$(echo ${_LB_EXPAND_QUEUE} | cut -d" " -f1)"
_LB_EXPAND_QUEUE="$(echo ${_LB_EXPAND_QUEUE} | cut -s -d" " -f2-)"
_LB_LIST_LOCATION=""
_LB_NESTED=0
_LB_ENABLED=1
local _LB_LIST_LOCATION=""
local _LB_NESTED=0
local _LB_ENABLED=1
for _LB_SEARCH_PATH in ${@}
do
@ -42,6 +45,7 @@ Expand_packagelist ()
do
case "${_LB_LINE}" in
\!*)
local _EXEC
_EXEC="$(echo ${_LB_LINE} | sed -e 's|^!||')"
case "${LB_BUILD_WITH_CHROOT}" in
@ -63,6 +67,10 @@ Expand_packagelist ()
fi
_LB_NESTED=1
local _LB_NEEDLE
local _LB_HAYSTACK
local _LB_NEEDLE_PART
local _LB_HAYSTACK_PART
_LB_NEEDLE="$(echo "${_LB_LINE}" | cut -d' ' -f3-)"
_LB_HAYSTACK="$(eval "echo \$LB_$(echo "${_LB_LINE}" | cut -d' ' -f2)")"
@ -87,6 +95,10 @@ Expand_packagelist ()
fi
_LB_NESTED=1
local _LB_NEEDLE
local _LB_HAYSTACK
local _LB_NEEDLE_PART
local _LB_HAYSTACK_PART
_LB_NEEDLE="$(echo "${_LB_LINE}" | cut -d' ' -f3-)"
_LB_HAYSTACK="$(eval "echo \$LB_$(echo "${_LB_LINE}" | cut -d' ' -f2)")"
@ -126,13 +138,15 @@ Expand_packagelist ()
Discover_package_architectures ()
{
_LB_EXPANDED_PKG_LIST="${1}"
_LB_DISCOVERED_ARCHITECTURES=""
local _LB_EXPANDED_PKG_LIST="${1}"
local _LB_DISCOVERED_ARCHITECTURES=""
shift
if [ -e "${_LB_EXPANDED_PKG_LIST}" ] && [ -s "${_LB_EXPANDED_PKG_LIST}" ]
then
local _LB_PACKAGE_LINE
local _LB_PACKAGE_LINE_PART
while read _LB_PACKAGE_LINE
do
# Lines from the expanded package list may have multiple, space-separated packages

View File

@ -9,11 +9,12 @@
## under certain conditions; see COPYING for details.
# Note, updates _LB_PACKAGES
Check_package ()
{
CHROOT="${1}"
FILE="${2}"
PACKAGE="${3}"
local CHROOT="${1}"
local FILE="${2}"
local PACKAGE="${3}"
Check_installed "${CHROOT}" "${FILE}" "${PACKAGE}"
@ -68,9 +69,9 @@ Remove_package ()
# 2 if package isn't installed and we aren't in an apt managed system
Check_installed ()
{
CHROOT="${1}"
FILE="${2}"
PACKAGE="${3}"
local CHROOT="${1}"
local FILE="${2}"
local PACKAGE="${3}"
if [ "${LB_BUILD_WITH_CHROOT}" = "true" ] && [ "${CHROOT}" = "chroot" ]
then

View File

@ -44,11 +44,15 @@ Create_stagefile ()
Require_stagefile ()
{
local NAME
local FILES
local NUMBER
NAME="$(basename ${0})"
FILES="${@}"
FILES="${@}" #must be on separate line to 'local' declaration to avoid error
NUMBER="$(echo ${@} | wc -w)"
CONTINUE=false
local FILE
local CONTINUE=false
for FILE in ${FILES}
do
# Find at least one of the required stages

View File

@ -11,7 +11,7 @@
Apt ()
{
CHROOT="${1}"
local CHROOT="${1}"
shift
case "${LB_APT}" in

View File

@ -50,10 +50,10 @@ Install_package
# Local functions
Grub_live_entry ()
{
LABEL="${1}"
KERNEL="${2}"
INITRD="${3}"
APPEND="${4}"
local LABEL="${1}"
local KERNEL="${2}"
local INITRD="${3}"
local APPEND="${4}"
LINUX_LIVE="${LINUX_LIVE}\ntitle\t\tDebian GNU/Linux - ${LABEL}"
LINUX_LIVE="${LINUX_LIVE}\nkernel\t\t/${KERNEL} ${INITFS:+boot=${INITFS} }config LB_BOOTAPPEND_LIVE ${APPEND}"
@ -62,15 +62,14 @@ Grub_live_entry ()
Grub_install_entry ()
{
LABEL="${1}"
KERNEL="${2}"
INITRD="${3}"
APPEND="${4}"
local LABEL="${1}"
local KERNEL="${2}"
local INITRD="${3}"
local APPEND="${4}"
APPEND="${APPEND} LB_BOOTAPPEND_INSTALL"
local APPEND="${APPEND} LB_BOOTAPPEND_INSTALL"
if [ "${LABEL}" != "expert" ] && [ "${LABEL}" != "expertgui" ]
then
if [ "${LABEL}" != "expert" ] && [ "${LABEL}" != "expertgui" ]; then
APPEND="${APPEND} --- quiet"
fi

View File

@ -287,6 +287,8 @@ find /live/vmlinuz
EOM
EOF
local rootpartition
local hdddev
rootpartition="$(Chroot chroot 'sh grub.sh' 2>/dev/null | grep -A 1 'find /live/vmlinuz' | grep -v 'find /live/vmlinuz')"
hdddev="$(echo $rootpartition | sed -e 's|,[[:digit:]]||')"
echo "Root partition is $rootpartition, device is: $hdddev"

View File

@ -66,11 +66,11 @@ Grub_live_entry ()
Grub_install_entry ()
{
LABEL="${1}"
TYPE="${2}"
KERNEL="${3}"
INITRD="${4}"
APPEND="${5}"
local LABEL="${1}"
local TYPE="${2}"
local KERNEL="${3}"
local INITRD="${4}"
local APPEND="${5}"
# Drop "quiet" kernel parameter for expert entries
case $TYPE in

View File

@ -137,6 +137,7 @@ USAGE="${PROGRAM} [--apt apt|aptitude]\n\
Local_arguments ()
{
local LONG_OPTIONS
LONG_OPTIONS="apt:,apt-ftp-proxy:,apt-http-proxy:,apt-options:,aptitude-options:,debootstrap-options:,debootstrap-script:,
apt-pipeline:,apt-recommends:,apt-secure:,apt-source-archives:,cache:,cache-indices:,cache-packages:,
cache-stages:,debconf-frontend:,debconf-priority:,dump,
@ -165,6 +166,7 @@ Local_arguments ()
# Remove spaces added by indentation
LONG_OPTIONS="$(echo ${LONG_OPTIONS} | tr -d ' ')"
local ARGUMENTS
local ERR=0
ARGUMENTS="$(getopt --longoptions ${LONG_OPTIONS} --name="${PROGRAM}" --options a:d:m:k:b:s:c:huv --shell sh -- "${@}")" || ERR=$?
@ -183,6 +185,7 @@ Local_arguments ()
case "${1}" in
--dump)
# Dump version
local VERSION_DPKG
if [ $(which dpkg-query) ]
then
VERSION_DPKG="$(dpkg-query -f='${Version}' --show live-build)"
@ -194,6 +197,7 @@ Local_arguments ()
Print_conffiles $(Common_config_files)
# Dump contents of directories that contain text files
local DIRECTORY
for DIRECTORY in config/package-lists config/apt config/preseed config/rootfs
do
if Find_files "${DIRECTORY}"

View File

@ -151,8 +151,7 @@ case "${LB_ARCHITECTURES}" in
esac
Install_file() {
local FILE
FILE="${1}"
local FILE="${1}"
Echo_debug "Installing file %s" "${FILE}"
@ -166,9 +165,9 @@ Install_file() {
ARCHIVE_AREA="main"
fi
local TARGET
TARGET="${2}/${ARCHIVE_AREA}"
local TARGET="${2}/${ARCHIVE_AREA}"
local SOURCE
SOURCE="$(dpkg -f ${FILE} Source | awk '{ print $1 }')"
if [ -z "${SOURCE}" ]
@ -176,6 +175,7 @@ Install_file() {
SOURCE="$(basename ${FILE} | awk -F_ '{ print $1 }')"
fi
local LETTER
case "${SOURCE}" in
lib?*)
LETTER="$(echo ${SOURCE} | sed 's|\(....\).*|\1|')"
@ -198,14 +198,12 @@ Install_file() {
_LB_CACHE_DIR="$(pwd)/cache/installer_debian-installer"
Download_file () {
local _LB_TARGET
_LB_TARGET="${1}"
local _LB_URL
_LB_URL="${2}"
local _LB_TARGET="${1}"
local _LB_URL="${2}"
Echo_debug "Downloading file \`%s\` from \`%s\`" "${_LB_TARGET}" "${_LB_URL}"
local _LB_CACHE_FILE
_LB_CACHE_FILE="${_LB_CACHE_DIR}/$(echo "${_LB_URL}" | sed 's|/|_|g')"
if [ ! -f "${_LB_CACHE_FILE}" ]
@ -795,11 +793,9 @@ fi
Repack_initrd()
{
local TARGET_INITRD
local INCLUDE_PATH
TARGET_INITRD="${1}"
INCLUDE_PATH="${2}"
REPACK_TMPDIR="unpacked-initrd"
local TARGET_INITRD="${1}"
local INCLUDE_PATH="${2}"
local REPACK_TMPDIR="unpacked-initrd"
Echo_debug "Repacking initrd \`%s\`" "${TARGET_INITRD}"
@ -816,6 +812,7 @@ Repack_initrd()
if [ ! -d "${INCLUDE_PATH}" ]
then
# Invoked the old way, just copy the preseeds
local _FILE
for _FILE in ../config/preseed/*.cfg ../config/preseed/*.cfg.installer
do
if [ -e "${_FILE}" ]
@ -825,7 +822,7 @@ Repack_initrd()
done
else
# New way, include target directory content in the initrd
REPACK_TMPDIR_ABS="${PWD}"
local REPACK_TMPDIR_ABS="${PWD}"
cd "${INCLUDE_PATH}"
find -print0 | cpio -pumd0 --no-preserve-owner "${REPACK_TMPDIR_ABS}/"
cd "${OLDPWD}"