changeset 530:c3db1ec91f02

common.subr: _get_mounts_at_directory() and _get_zfs_dataset_for_mountpoint() now also work for path and/or datasets with spaces in their names/values. For this to work textproc/jq must be installed.
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 04 Sep 2024 21:53:43 +0200
parents 703e9f357339
children 07a916bd830c
files share/local-bsdtools/common.subr
diffstat 1 files changed, 34 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/common.subr	Wed Sep 04 20:52:57 2024 +0200
+++ b/share/local-bsdtools/common.subr	Wed Sep 04 21:53:43 2024 +0200
@@ -220,16 +220,30 @@
 
     _mountpoint="$1"
 
-    /sbin/mount -t zfs -p \
-    | {
-        while IFS=' '$'\t' read -r _ds _mount _rest ; do
-            if [ "$_mount" = "$_mountpoint" ]; then
-                echo "${_ds}"
-                return 0
-            fi
-        done
-        return 1
-    }
+    if [ -x "${JQ}" ]; then
+        /sbin/mount -t zfs -p --libxo=json,no-locale \
+        | LC_ALL=C "${JQ}" -r $'.mount.fstab.[] | [.device, .mntpoint, .fstype, .opts, .dump, .pass] | @tsv ' \
+        | {
+            while IFS=$'\t' read -r _ds _mount _rest ; do
+                if [ "$_mount" = "$_mountpoint" ]; then
+                    echo "${_ds}"
+                    return 0
+                fi
+            done
+            return 1
+          }
+    else
+        /sbin/mount -t zfs -p \
+        | {
+            while IFS=' '$'\t' read -r _ds _mount _rest ; do
+                if [ "$_mount" = "$_mountpoint" ]; then
+                    echo "${_ds}"
+                    return 0
+                fi
+            done
+            return 1
+          }
+    fi
 }
 
 
@@ -351,8 +365,16 @@
             exit 1;
             ;;
     esac
-    _fstab="$(/sbin/mount -p | awk -v pa1="^${_directory}\$" -v pa2="^${_directory}/" '($2 ~ pa1) || ($2 ~ pa2 ) { print; }' | sort -k3)"
-    echo "${_fstab}"
+    if [ -x "${JQ}" ]; then
+        /sbin/mount -p --libxo=json,no-locale \
+        | LC_ALL=C "${JQ}" -r $'.mount.fstab.[] | [.device, .mntpoint, .fstype, .opts, .dump, .pass] | @tsv ' \
+        | LC_ALL=C /usr/bin/awk -F $'\\t+' -v OFS=$'\t' -v ds1="${_directory}" -v ds2="${_directory}/" $'{ if (($2 == ds1) || (index($2, ds2) == 1)) { print; } }' \
+        | LC_ALL=C /usr/bin/sort -t $'\t' -k1
+    else
+
+        _fstab="$(/sbin/mount -p | awk -v pa1="^${_directory}\$" -v pa2="^${_directory}/" '($2 ~ pa1) || ($2 ~ pa2 ) { print; }' | sort -k3)"
+        echo "${_fstab}"
+    fi
 }