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
local thisComp=$_npThisComp
if [ "$thisComp" = "/.." ]; then
# "go up" path component => delete the leaf dir (if any)
if [ ${#comps[@]} -ne 0 ]; then
unset comps[-1]
fi
elif [ "$thisComp" != "/." ] && [ "$thisComp" != "/" ] && [ "$thisComp" != "" ]; then
# normal, non-empty path component => append it
comps+=("$thisComp")
fi
case $thisComp in
(/..)
# "go up" path component => delete the leaf dir (if any)
if [ ${#comps[@]} -ne 0 ]; then
unset comps[-1]
fi
;;
(/. | / | "") ;;
(*)
# normal, non-empty path component => append it
comps+=("$thisComp")
;;
esac
done
# join the components
@@ -356,33 +360,39 @@ tryPath() {
local path=$1
local how=$2
if [ "$how" = "existing" ]; then
# the caller wants to access either a file, or a directory (possibly a symlink to such a thing)
if [ -e "$path" ]; then
relativeToPwd _absPath "$path"
paths+=("$_absPath")
return 0
fi
return 1
elif [ "$how" = "existingFile" ]; then
# 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
relativeToPwd _absPath "$path"
paths+=("$_absPath")
return 0
fi
return 1
elif [ "$how" = "parent" ]; then
# the caller wants access to the entire directory containing this directory regardless of the file's existence.
parent _tryPathParent "$path"
tryPath "$_tryPathParent" "existing"
elif [ "$how" = "existingOrParent" ]; then
# 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"
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.
tryPath "$path" "existingFile" || tryPath "$path" "parent"
fi
case $how in
(existing)
# the caller wants to access either a file, or a directory (possibly a symlink to such a thing)
if [ -e "$path" ]; then
relativeToPwd _absPath "$path"
paths+=("$_absPath")
return 0
fi
return 1
;;
(existingFile)
# 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
relativeToPwd _absPath "$path"
paths+=("$_absPath")
return 0
fi
return 1
;;
(parent)
# the caller wants access to the entire directory containing this directory regardless of the file's existence.
parent _tryPathParent "$path"
tryPath "$_tryPathParent" "existing"
;;
(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.
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.
@@ -391,23 +401,28 @@ tryPath() {
tryArgAsPath() {
local arg=$1
local how=$2
path=
if [ "${arg:0:1}" = "/" ]; then
# absolute path
path=$arg
elif [ "${arg:0:8}" = "file:///" ]; then
# URI to an absolute path which is presumably on this vfs
# 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
urldecode _path "${arg:7}"
path=$_path
elif [ "${path:0:1}" = "-" ]; then
# 99% chance it's a CLI argument. if not, use `./-<...>`
return
else
# assume relative path
_path=$PWD/$arg
fi
local path=
case $arg in
(/*)
# absolute path
path=$arg
;;
(file:///*)
# URI to an absolute path which is presumably on this vfs
# 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
urldecode _path "${arg:7}"
path=$_path
;;
(-*)
# 99% chance it's a CLI argument. if not, use `./-<...>`
return
;;
(*)
# assume relative path
path=$PWD/$arg
;;
esac
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)
# and maximizes compatibility with apps. but if explicitly asked for the directory, then remove the virtual
# device and bind it as normal.
if [ "$1" = / ]; then
bwrapVirtualizeDev=()
bwrapVirtualizeProc=()
bwrapVirtualizeTmp=()
elif [ "$1" = /dev ]; then
bwrapVirtualizeDev=()
elif [ "$1" = /proc ]; then
bwrapVirtualizeProc=()
elif [ "$1" = /tmp ]; then
bwrapVirtualizeTmp=()
fi
case $1 in
(/)
bwrapVirtualizeDev=()
bwrapVirtualizeProc=()
bwrapVirtualizeTmp=()
;;
(/dev)
bwrapVirtualizeDev=()
;;
(/proc)
bwrapVirtualizeProc=()
;;
(/tmp)
bwrapVirtualizeTmp=()
;;
esac
}
bwrapIngestNet() {
debug "bwrapIngestNet: enabling full net access for '$1' because don't know how to restrict it more narrowly"