changeset 531:07a916bd830c

common.subr: _get_jail_from_path() now works with spaces in path values. If textproc/jq is installed then spaces in jail names are allowed also.
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 04 Sep 2024 22:08:23 +0200
parents c3db1ec91f02
children c615279bb797
files share/local-bsdtools/common.subr
diffstat 1 files changed, 36 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/common.subr	Wed Sep 04 21:53:43 2024 +0200
+++ b/share/local-bsdtools/common.subr	Wed Sep 04 22:08:23 2024 +0200
@@ -299,28 +299,45 @@
 #:   3: no running jail found
 #:
 _get_jail_from_path() {
-  local _location
-
-  local _name _path _dying
+    local _location
 
-  _location="${1-}"
-  [ -z "${_location}" ] && { echo "ERROR: no mountpoint given" 1>&2; return 1; }
+    local _name _path _dying
 
+    _location="${1-}"
+    [ -z "${_location}" ] && { echo "ERROR: no mountpoint given" 1>&2; return 1; }
 
-  jls -d name path dying \
-  | {
-      while IFS=' '$'\t' read -r _name _path _dying ; do
-        if [ "${_path}" = "${_location}" ]; then
-          if [ "${_dying}" != "false" ]; then
-            echo "Jail \`${_name}' is currently dying" 1>&2
-            return 2
-          fi
-          echo "${_name}"
-          return 0
-        fi
-      done
-      return 3
-  }
+    if [ -x "${JQ}" ]; then
+        jls --libxo=json,no-locale -d dying name path \
+        | LC_ALL=C "${JQ}" -r $'.["jail-information"].jail[] | [.dying, .name, .path] | @tsv ' \
+        | { # '
+            while IFS=' '$'\t' read -r _dying _name _path ; do
+                if [ "${_path}" = "${_location}" ]; then
+                    if [ "${_dying}" != "false" ]; then
+                        echo "Jail \`${_name}' is currently dying" 1>&2
+                        return 2
+                    fi
+                    echo "${_name}"
+                    return 0
+                fi
+            done
+            return 3
+          }
+    else
+        jls -d name dying path \
+        | {
+            while IFS=' '$'\t' read -r _name _dying _path ; do
+                if [ "${_path}" = "${_location}" ]; then
+                    if [ "${_dying}" != "false" ]; then
+                        echo "Jail \`${_name}' is currently dying" 1>&2
+                        return 2
+                    fi
+                    echo "${_name}"
+                    return 0
+                fi
+            done
+            return 3
+        }
+    fi
 }