Properly handle case where $parent is empty to simplify condition

This commit is contained in:
Alexandre Aubin 2024-06-28 16:55:39 +02:00
parent 3608c5678c
commit 3b26ccc2a5

View file

@ -237,23 +237,24 @@ _ynh_apply_default_permissions() {
local target=$1
is_in_dir() {
# Returns false if child or parent is empty
child=$(realpath "$1" 2>/dev/null)
parent=$(realpath "$2" 2>/dev/null)
# Returns false if parent is empty
[ -n "$2" ] || return 1
local child=$(realpath "$1" 2>/dev/null)
local parent=$(realpath "$2" 2>/dev/null)
[[ "${child/$parent/}" != "$child" ]]
}
# App files can have files of their own
if ynh_system_user_exists --username="$app"; then
# If this is a file in $install_dir or $data_dir : it should be owned and read+writable by $app only
if [ -f "$target" ] && (([[ -z "${install_dir:-}" ]] is_in_dir "$target" "$install_dir") || ([[ -z "${install_dir:-}" ]] is_in_dir "$target" "$data_dir"))
if [ -f "$target" ] && (is_in_dir "$target" "${install_dir:-}" || is_in_dir "$target" "${data_dir:-}")
then
chmod 600 "$target"
chown "$app:$app" "$target"
return
fi
# If this is the install dir (so far this is the only way this helper is called with a directory)
if [ "$target" == "$install_dir" ]
if [ "$target" == "${install_dir:-}" ]
then
# Files inside should be owned by $app/www-data with rw-r----- (+x for folders or files that already have +x)
chmod -R u=rwX,g=r-X,o=--- "$target"