LibrePortal/scripts/function/file/create_touch.sh
librelad 053a620e22 fix(reliability): split local result=$(cmd) so $? survives for checkSuccess
'local result=$(cmd)' resets $? to 0 (the local builtin's own exit), so the
following checkSuccess always saw success regardless of cmd's real exit — the
mechanism that masked the de-sudo write failures. Split declaration from
assignment ('local result; result=$(cmd)') across all 235 active-code sites
(84 files) so the command's exit reaches checkSuccess. No behaviour change
beyond $? now being accurate (no set -e in runtime code; multi-line
assignments transform safely).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-31 03:09:25 +01:00

37 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# Create an empty file with the correct owner FOR ITS LOCATION.
# under /docker/containers/<app>/ -> app data, owned by the docker install
# user -> create via runFileOp.
# anywhere else -> manager control plane -> runInstallOp
# (the current/manager user).
# Creating the file directly as the right owner avoids chown-to-another-user,
# which needs real root and isn't available to the unprivileged runtime.
# $2 (user_name) is kept for call-site compatibility but is now advisory — the
# path decides the owner, so a stale hint (e.g. passing the manager user for a
# file that lives under containers/) no longer lands the file with the wrong
# owner. Parent dirs are created with the same owner.
createTouch()
{
local file="$1"
local user_name="$2" # advisory; location determines the real owner
local silent_flag="$3"
local clean_file=$(echo "$file" | sed 's#//*#/#g')
local file_name=$(basename "$clean_file")
local file_dir=$(dirname "$clean_file")
local op="runInstallOp"
if [[ "$clean_file" == "$containers_dir"* || "$clean_file" == "${LP_CONTAINERS_DIR:-/libreportal-containers}"/* ]]; then
op="runFileOp"
fi
if [ "$silent_flag" == "silent" ]; then
$op mkdir -p "$file_dir" 2>/dev/null
$op touch "$clean_file"
else
local result; result=$($op mkdir -p "$file_dir")
local result; result=$($op touch "$clean_file")
checkSuccess "Touching $file_name"
fi
}