From 6df1cdeab9e39d229598ef00c84ee09b564c92ef Mon Sep 17 00:00:00 2001 From: Narrat Date: Tue, 6 Aug 2024 14:59:51 +0200 Subject: [PATCH] list_tomb_mounts: simplify the function previously it had dedicated cases for listing all tombs and a singular one, which duplicated code. The function got reworked, that it uses a different approach for findmnt. Instead of filtering the general result, it now uses --source on the tomb specific crypsetup mapper. Those are searched via general globbing of the devices in /dev/mapper. This allows to combine the previous separate cases. Additionally remove the usage of _sudo for findmnt, as it is not necessary. --- tomb | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/tomb b/tomb index 7eae235..ea02fd8 100755 --- a/tomb +++ b/tomb @@ -2756,34 +2756,18 @@ awk "/mapper/"' { print $2 ";" $3 ";" $4 ";" $5 }'` # # 5. tomb name list_tomb_mounts() { - [[ -z "$1" ]] && { - # list all open tombs - _sudo findmnt -rvo SOURCE,TARGET,FSTYPE,OPTIONS,LABEL \ + # execute in subshell to avoid ZSH non-POSIX behaviour if globbing doesn't match something + ( for dev in /dev/mapper/tomb*$1.*; do + findmnt --source ${dev} -rnvo SOURCE,TARGET,FSTYPE,OPTIONS,LABEL \ | awk ' BEGIN { main="" } -/^\/dev\/mapper\/tomb/ { +{ if(main==$1) next; print $1 ";" $2 ";" $3 ";(" $4 ");[" $5 "]" main=$1 } ' - } || { - # list a specific tomb - # add square parens if not present (detection) - local tname - if [[ "${1[1]}" = "[" ]]; then tname="$1" - else tname="[$1]"; fi - _sudo findmnt -rvo SOURCE,TARGET,FSTYPE,OPTIONS,LABEL \ - | awk -vtomb="$tname" ' -BEGIN { main="" } -/^\/dev\/mapper\/tomb/ { - if("["$5"]"!=tomb) next; - if(main==$1) next; - print $1 ";" $2 ";" $3 ";(" $4 ");[" $5 "]" - main=$1 -} -' - } + done ) 2>/dev/null } # list_tomb_binds