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) # "go up" path component => delete the leaf dir (if any)
if [ ${#comps[@]} -ne 0 ]; then if [ ${#comps[@]} -ne 0 ]; then
unset comps[-1] unset comps[-1]
fi fi
elif [ "$thisComp" != "/." ] && [ "$thisComp" != "/" ] && [ "$thisComp" != "" ]; then ;;
(/. | / | "") ;;
(*)
# normal, non-empty path component => append it # normal, non-empty path component => append it
comps+=("$thisComp") comps+=("$thisComp")
fi ;;
esac
done done
# join the components # join the components
@@ -356,7 +360,8 @@ tryPath() {
local path=$1 local path=$1
local how=$2 local how=$2
if [ "$how" = "existing" ]; then case $how in
(existing)
# the caller wants to access either a file, or a directory (possibly a symlink to such a thing) # the caller wants to access either a file, or a directory (possibly a symlink to such a thing)
if [ -e "$path" ]; then if [ -e "$path" ]; then
relativeToPwd _absPath "$path" relativeToPwd _absPath "$path"
@@ -364,7 +369,8 @@ tryPath() {
return 0 return 0
fi fi
return 1 return 1
elif [ "$how" = "existingFile" ]; then ;;
(existingFile)
# the caller wants to access a file, and explicitly *not* a directory (though it could be a symlink *to a file*) # 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 if [ -f "$path" ]; then
relativeToPwd _absPath "$path" relativeToPwd _absPath "$path"
@@ -372,17 +378,21 @@ tryPath() {
return 0 return 0
fi fi
return 1 return 1
elif [ "$how" = "parent" ]; then ;;
(parent)
# the caller wants access to the entire directory containing this directory regardless of the file's existence. # the caller wants access to the entire directory containing this directory regardless of the file's existence.
parent _tryPathParent "$path" parent _tryPathParent "$path"
tryPath "$_tryPathParent" "existing" tryPath "$_tryPathParent" "existing"
elif [ "$how" = "existingOrParent" ]; then ;;
(existingOrParent)
# 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. # 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.
tryPath "$path" "existing" || tryPath "$path" "parent" tryPath "$path" "existing" || tryPath "$path" "parent"
elif [ "$how" = "existingFileOrParent" ]; then ;;
(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. # 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" tryPath "$path" "existingFile" || tryPath "$path" "parent"
fi ;;
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 # absolute path
path=$arg path=$arg
elif [ "${arg:0:8}" = "file:///" ]; then ;;
(file:///*)
# URI to an absolute path which is presumably on this vfs # URI to an absolute path which is presumably on this vfs
# commonly found when xdg-open/mimeo passes a path on to an application # commonly found when xdg-open/mimeo passes a path on to an application
# if URIs to relative paths exist, this implementation doesn't support them # if URIs to relative paths exist, this implementation doesn't support them
urldecode _path "${arg:7}" urldecode _path "${arg:7}"
path=$_path path=$_path
elif [ "${path:0:1}" = "-" ]; then ;;
(-*)
# 99% chance it's a CLI argument. if not, use `./-<...>` # 99% chance it's a CLI argument. if not, use `./-<...>`
return return
else ;;
(*)
# assume relative path # assume relative path
_path=$PWD/$arg path=$PWD/$arg
fi ;;
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=() bwrapVirtualizeDev=()
bwrapVirtualizeProc=() bwrapVirtualizeProc=()
bwrapVirtualizeTmp=() bwrapVirtualizeTmp=()
elif [ "$1" = /dev ]; then ;;
(/dev)
bwrapVirtualizeDev=() bwrapVirtualizeDev=()
elif [ "$1" = /proc ]; then ;;
(/proc)
bwrapVirtualizeProc=() bwrapVirtualizeProc=()
elif [ "$1" = /tmp ]; then ;;
(/tmp)
bwrapVirtualizeTmp=() bwrapVirtualizeTmp=()
fi ;;
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"