Skip to content

Commit 798fce8

Browse files
committed
Normalize tests for paths.
The `-f` flag to test(1) checks for regular files, which is generally not the right thing to do when looking for ZFS keys because keylocation can point to, e.g., a device node. Every `-f` test in where we expressly intend to read (or source) a file has been moved to `-r`, and tests for ZFS keys (which are intended to be passed as locations to `zfs load-key` rather than read directly) are relaxed to the maximally generic existence test (`-e`). Also, swap `test` for `[ ]` syntax everywhere and drop sourcing of `zfsbootmenu-lib.sh` in the PWD. That was probably an artifact of early testing, but we have better test setups now. Instead, look only for the canonical `/lib/zfsbootmenu-lib.sh`. Closes #127.
1 parent d02865e commit 798fce8

File tree

7 files changed

+17
-25
lines changed

7 files changed

+17
-25
lines changed

90zfsbootmenu/zfs-chroot.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
# vim: softtabstop=2 shiftwidth=2 expandtab
33

44
# shellcheck disable=SC1091
5-
test -f /lib/zfsbootmenu-lib.sh && source /lib/zfsbootmenu-lib.sh
6-
# shellcheck disable=SC1091
7-
test -f zfsbootmenu-lib.sh && source zfsbootmenu-lib.sh
5+
[ -r /lib/zfsbootmenu-lib.sh ] && source /lib/zfsbootmenu-lib.sh
86

97
selected="${1}"
108

@@ -29,7 +27,7 @@ if mountpoint="$( allow_rw=yes mount_zfs "${selected}" )"; then
2927
fi
3028
echo -e ", /tmp is shared and read/write\n"
3129

32-
if [ -f "${mountpoint}/bin/bash" ]; then
30+
if [ -x "${mountpoint}/bin/bash" ]; then
3331
_SHELL="/bin/bash"
3432
else
3533
_SHELL="/bin/sh"

90zfsbootmenu/zfsbootmenu-countdown.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
trap '' SIGINT
66

77
# shellcheck disable=SC1091
8-
test -f /lib/zfsbootmenu-lib.sh && source /lib/zfsbootmenu-lib.sh
9-
# shellcheck disable=SC1091
10-
test -f zfsbootmenu-lib.sh && source zfsbootmenu-lib.sh
8+
[ -r /lib/zfsbootmenu-lib.sh ] && source /lib/zfsbootmenu-lib.sh
119

1210
if [ -z "${BASE}" ]; then
1311
export BASE="/zfsbootmenu"

90zfsbootmenu/zfsbootmenu-exec.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ udevadm settle
3737
# try to set console options for display and interaction
3838
# this is sometimes run as an initqueue hook, but cannot be guaranteed
3939
#shellcheck disable=SC2154
40-
test -x /lib/udev/console_init -a -c "${control_term}" \
40+
[ -x /lib/udev/console_init ] && [ -c "${control_term}" ] \
4141
&& /lib/udev/console_init "${control_term##*/}" >/dev/null 2>&1
4242

4343
#shellcheck disable=SC2154

90zfsbootmenu/zfsbootmenu-lib.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mount_zfs() {
6060
fi
6161

6262
mnt="${BASE}/${fs}/mnt"
63-
test -d "${mnt}" || mkdir -p "${mnt}"
63+
mkdir -p "${mnt}"
6464

6565
# @ always denotes a snapshot
6666
if [[ "${fs}" =~ @ ]]; then
@@ -190,7 +190,7 @@ draw_be() {
190190

191191
zdebug "using environment file: ${env}"
192192

193-
[ -f "${env}" ] || return 130
193+
[ -r "${env}" ] || return 130
194194

195195
header="$( header_wrap "[ENTER] boot" "[ESC] refresh view" "[CTRL+H] help" "" \
196196
"[CTRL+E] edit kcl" "[CTRL+K] kernels" "[CTRL+D] set bootfs" "[CTRL+S] snapshots" "" \
@@ -225,7 +225,7 @@ draw_kernel() {
225225

226226
zdebug "using kernels file: ${_kernels}"
227227

228-
test -f "${_kernels}" || return 130
228+
[ -r "${_kernels}" ] || return 130
229229

230230
header="$( header_wrap \
231231
"[ENTER] boot" "[ESC] back" "" "[CTRL+D] set default" "[CTRL+H] help" )"
@@ -320,7 +320,7 @@ draw_diff() {
320320
--preview-window="up:${PREVIEW_HEIGHT}" \
321321
--bind 'esc:execute-silent( kill $( cat /tmp/diff.pid ) )+abort'
322322

323-
test -f /tmp/diff.pid && rm /tmp/diff.pid
323+
rm -f /tmp/diff.pid
324324
umount "${mnt}"
325325

326326
return
@@ -724,7 +724,7 @@ find_root_prefix() {
724724
# OS type is in ID and ID_LIKE variables; /etc supersedes /usr/lib
725725
unset ID ID_LIKE
726726
for osrel in ${zfsbe_mnt}/{usr/lib,etc}/os-release; do
727-
if [ -f "${osrel}" ]; then
727+
if [ -r "${osrel}" ]; then
728728
# shellcheck disable=SC1090
729729
. "${osrel}" >/dev/null 2>&1
730730
fi
@@ -813,7 +813,7 @@ load_be_cmdline() {
813813

814814
# Use BE-specific cmdline if found, fall back to generic default
815815
zfsbe_args="quiet loglevel=4"
816-
if [ -f "${BASE}/${zfsbe_fs}/cmdline" ]; then
816+
if [ -r "${BASE}/${zfsbe_fs}/cmdline" ]; then
817817
zdebug "using ${BASE}/${zfsbe_fs}/cmdline as commandline for ${zfsbe_fs}"
818818
zfsbe_args="$(head -1 "${BASE}/${zfsbe_fs}/cmdline" | tr -d '\n')"
819819
fi
@@ -1304,7 +1304,7 @@ cache_key() {
13041304
fi
13051305

13061306
ret=1
1307-
if [ -f "${mnt}/${keyfile}" ]; then
1307+
if [ -e "${mnt}/${keyfile}" ]; then
13081308
keydir="${keyfile%/*}"
13091309
if [ "x${keydir}" != "x${keyfile}" ] && [ -n "${keydir}" ]; then
13101310
mkdir -p "${keycache}/${keydir}"
@@ -1364,7 +1364,7 @@ load_key() {
13641364
key="${keylocation#file://}"
13651365
key="${key#/}"
13661366

1367-
if [ -f "/${key}" ]; then
1367+
if [ -e "/${key}" ]; then
13681368
# Prefer the actual path to the key file
13691369
keypath="/${key}"
13701370
elif keysource="$( be_keysource "${fs}" )" && [ "${NO_CACHE}" -eq 0 ]; then
@@ -1378,14 +1378,14 @@ load_key() {
13781378
fi
13791379

13801380
# If the cached key exists, prefer it
1381-
if [ -f "${BASE}/.keys/${keysource}/${key}" ]; then
1381+
if [ -e "${BASE}/.keys/${keysource}/${key}" ]; then
13821382
keypath="${BASE}/.keys/${keysource}/${key}"
13831383
zdebug "cached key path for $fs is ${keypath}"
13841384
fi
13851385
fi
13861386

13871387
# Load a key from a file, if possible and necessary
1388-
if [ -f "${keypath}" ] && be_is_locked "${fs}" >/dev/null 2>&1; then
1388+
if [ -e "${keypath}" ] && be_is_locked "${fs}" >/dev/null 2>&1; then
13891389
if zfs load-key -L "file://${keypath}" "${encroot}"; then
13901390
zdebug "unlocked ${encroot} from key at ${keypath}"
13911391
return 0

90zfsbootmenu/zfsbootmenu-parse-commandline.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ if [ ${wait_for_zfs} -eq 1 ]; then
9696
ln -s /dev/null /dev/root 2>/dev/null
9797
# shellcheck disable=SC2154
9898
initqueuedir="${hookdir}/initqueue/finished"
99-
test -d "${initqueuedir}" || {
100-
initqueuedir="${hookdir}/initqueue-finished"
101-
}
99+
[ -d "${initqueuedir}" ] || initqueuedir="${hookdir}/initqueue-finished"
102100
echo '[ -e /dev/zfs ]' > "${initqueuedir}/zfs.sh"
103101
fi

90zfsbootmenu/zfsbootmenu-preview.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ RED='\033[0;31m'
1212
NC='\033[0m' # No Color
1313

1414
# shellcheck disable=SC1091
15-
test -f /lib/zfsbootmenu-lib.sh && source /lib/zfsbootmenu-lib.sh
15+
[ -r /lib/zfsbootmenu-lib.sh ] && source /lib/zfsbootmenu-lib.sh
1616

1717
while IFS= read -r line
1818
do

90zfsbootmenu/zfsbootmenu.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
##
77

88
# shellcheck disable=SC1091
9-
test -f /lib/zfsbootmenu-lib.sh && source /lib/zfsbootmenu-lib.sh
10-
# shellcheck disable=SC1091
11-
test -f zfsbootmenu-lib.sh && source zfsbootmenu-lib.sh
9+
[ -r /lib/zfsbootmenu-lib.sh ] && source /lib/zfsbootmenu-lib.sh
1210

1311
if [ -z "${BASE}" ]; then
1412
export BASE="/zfsbootmenu"

0 commit comments

Comments
 (0)