sanebox: prefer case statments over if/elif/elif... constructs

This commit is contained in:
2024-05-17 23:32:51 +00:00
parent 26aa68ee59
commit 795786f46b

View File

@@ -199,15 +199,19 @@ normPath() {
unparsed=$_npUnparsed unparsed=$_npUnparsed
local thisComp=$_npThisComp local thisComp=$_npThisComp
if [ "$thisComp" = "/.." ]; then case $thisComp in
# "go up" path component => delete the leaf dir (if any) (/..)
if [ ${#comps[@]} -ne 0 ]; then # "go up" path component => delete the leaf dir (if any)
unset comps[-1] if [ ${#comps[@]} -ne 0 ]; then
fi unset comps[-1]
elif [ "$thisComp" != "/." ] && [ "$thisComp" != "/" ] && [ "$thisComp" != "" ]; then fi
# normal, non-empty path component => append it ;;
comps+=("$thisComp") (/. | / | "") ;;
fi (*)
# normal, non-empty path component => append it
comps+=("$thisComp")
;;
esac
done done
# join the components # join the components
@@ -356,33 +360,39 @@ tryPath() {
local path=$1 local path=$1
local how=$2 local how=$2
if [ "$how" = "existing" ]; then case $how in
# the caller wants to access either a file, or a directory (possibly a symlink to such a thing) (existing)
if [ -e "$path" ]; then # the caller wants to access either a file, or a directory (possibly a symlink to such a thing)
relativeToPwd _absPath "$path" if [ -e "$path" ]; then
paths+=("$_absPath") relativeToPwd _absPath "$path"
return 0 paths+=("$_absPath")
fi return 0
return 1 fi
elif [ "$how" = "existingFile" ]; then return 1
# the caller wants to access a file, and explicitly *not* a directory (though it could be a symlink *to a file*) ;;
if [ -f "$path" ]; then (existingFile)
relativeToPwd _absPath "$path" # the caller wants to access a file, and explicitly *not* a directory (though it could be a symlink *to a file*)
paths+=("$_absPath") if [ -f "$path" ]; then
return 0 relativeToPwd _absPath "$path"
fi paths+=("$_absPath")
return 1 return 0
elif [ "$how" = "parent" ]; then fi
# the caller wants access to the entire directory containing this directory regardless of the file's existence. return 1
parent _tryPathParent "$path" ;;
tryPath "$_tryPathParent" "existing" (parent)
elif [ "$how" = "existingOrParent" ]; then # the caller wants access to the entire directory containing this directory regardless of the file's existence.
# the caller wants access to the path, or write access to the parent directory so it may create the path if it doesn't exist. parent _tryPathParent "$path"
tryPath "$path" "existing" || tryPath "$path" "parent" tryPath "$_tryPathParent" "existing"
elif [ "$how" = "existingFileOrParent" ]; then ;;
# the caller wants access to the file, or write access to the parent directory so it may create the file if it doesn't exist. (existingOrParent)
tryPath "$path" "existingFile" || tryPath "$path" "parent" # the caller wants access to the path, or write access to the parent directory so it may create the path if it doesn't exist.
fi tryPath "$path" "existing" || tryPath "$path" "parent"
;;
(existingFileOrParent)
# the caller wants access to the file, or write access to the parent directory so it may create the file if it doesn't exist.
tryPath "$path" "existingFile" || tryPath "$path" "parent"
;;
esac
} }
# if the argument looks path-like, then add it to paths. # if the argument looks path-like, then add it to paths.
@@ -391,23 +401,28 @@ tryPath() {
tryArgAsPath() { tryArgAsPath() {
local arg=$1 local arg=$1
local how=$2 local how=$2
path= local path=
if [ "${arg:0:1}" = "/" ]; then case $arg in
# absolute path (/*)
path=$arg # absolute path
elif [ "${arg:0:8}" = "file:///" ]; then path=$arg
# URI to an absolute path which is presumably on this vfs ;;
# commonly found when xdg-open/mimeo passes a path on to an application (file:///*)
# if URIs to relative paths exist, this implementation doesn't support them # URI to an absolute path which is presumably on this vfs
urldecode _path "${arg:7}" # commonly found when xdg-open/mimeo passes a path on to an application
path=$_path # if URIs to relative paths exist, this implementation doesn't support them
elif [ "${path:0:1}" = "-" ]; then urldecode _path "${arg:7}"
# 99% chance it's a CLI argument. if not, use `./-<...>` path=$_path
return ;;
else (-*)
# assume relative path # 99% chance it's a CLI argument. if not, use `./-<...>`
_path=$PWD/$arg return
fi ;;
(*)
# assume relative path
path=$PWD/$arg
;;
esac
tryPath "$path" "$how" tryPath "$path" "$how"
} }
@@ -617,17 +632,22 @@ bwrapIngestPath() {
# default to virtualizing a few directories in a way that's safe (doesn't impact outside environment) # default to virtualizing a few directories in a way that's safe (doesn't impact outside environment)
# and maximizes compatibility with apps. but if explicitly asked for the directory, then remove the virtual # and maximizes compatibility with apps. but if explicitly asked for the directory, then remove the virtual
# device and bind it as normal. # device and bind it as normal.
if [ "$1" = / ]; then case $1 in
bwrapVirtualizeDev=() (/)
bwrapVirtualizeProc=() bwrapVirtualizeDev=()
bwrapVirtualizeTmp=() bwrapVirtualizeProc=()
elif [ "$1" = /dev ]; then bwrapVirtualizeTmp=()
bwrapVirtualizeDev=() ;;
elif [ "$1" = /proc ]; then (/dev)
bwrapVirtualizeProc=() bwrapVirtualizeDev=()
elif [ "$1" = /tmp ]; then ;;
bwrapVirtualizeTmp=() (/proc)
fi bwrapVirtualizeProc=()
;;
(/tmp)
bwrapVirtualizeTmp=()
;;
esac
} }
bwrapIngestNet() { bwrapIngestNet() {
debug "bwrapIngestNet: enabling full net access for '$1' because don't know how to restrict it more narrowly" debug "bwrapIngestNet: enabling full net access for '$1' because don't know how to restrict it more narrowly"