Files
2026-07-04 11:20:44 +02:00

130 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
PLUGIN_DIR="$(cd "$(dirname "$0")/.." && pwd)"
CONFIG_FILE="${PLUGIN_DIR}/plugin-settings.conf"
DATA_DIR="${PLUGIN_DIR}/data"
JOB_DIR="${DATA_DIR}/jobs"
LOG_DIR="${DATA_DIR}/logs"
PID_DIR="${DATA_DIR}/pids"
CANCEL_DIR="${DATA_DIR}/cancel"
TMP_DIR="${DATA_DIR}/tmp"
CSRF_DIR="${DATA_DIR}/csrf"
warn() {
printf '[fix_permissions] %s\n' "$*" >&2
}
strip_quotes() {
local v="$1"
v="${v#"${v%%[![:space:]]*}"}"
v="${v%"${v##*[![:space:]]}"}"
if [[ "$v" == \"*\" && "$v" == *\" ]]; then
v="${v:1:${#v}-2}"
elif [[ "$v" == \'*\' && "$v" == *\' ]]; then
v="${v:1:${#v}-2}"
fi
printf '%s' "$v"
}
get_cfg() {
local key="$1"
local value=""
if [ -r "$CONFIG_FILE" ]; then
while IFS= read -r line; do
line="${line%%#*}"
line="$(strip_quotes "$line")"
if [[ "$line" == "$key="* ]]; then
value="${line#*=}"
fi
done < "$CONFIG_FILE"
fi
printf '%s' "$value"
}
owner_name="$(stat -c %U "$PLUGIN_DIR" 2>/dev/null || true)"
if [ "$owner_name" != "root" ] && [ "$owner_name" != "diradmin" ]; then
owner_name="root"
fi
owner_group="$owner_name"
fix_dir() {
local path="$1"
if [ -z "$path" ]; then
return
fi
mkdir -p "$path"
chown "$owner_name":"$owner_group" "$path" || true
chmod 700 "$path" || true
}
fix_file() {
local path="$1"
local label="$2"
if [ -z "$path" ]; then
return
fi
if [ ! -e "$path" ]; then
warn "${label} missing: ${path}"
return
fi
if [ -L "$path" ] || [ ! -f "$path" ]; then
warn "${label} not a regular file: ${path}"
return
fi
chown "$owner_name":"$owner_group" "$path" || true
chmod 600 "$path" || true
}
BB_PATH="$(get_cfg "BB_PATH")"
BORG_VARIABLES_FILE="$(get_cfg "BORG_VARIABLES_FILE")"
if [ -z "$BB_PATH" ]; then
BB_PATH="/opt/bb"
fi
bbvars_path=""
if [ -n "$BORG_VARIABLES_FILE" ]; then
bbvars_path="${BORG_VARIABLES_FILE}"
if [[ "$bbvars_path" == *DA_USER* ]]; then
if [ -n "${DA_USER:-}" ]; then
bbvars_path="${bbvars_path//DA_USER/${DA_USER}}"
elif [ -f "/opt/bb/bbvars.sh" ]; then
bbvars_path="/opt/bb/bbvars.sh"
else
bbvars_path=""
fi
fi
else
if [[ "$BB_PATH" == *.sh ]]; then
bbvars_path="$BB_PATH"
else
bbvars_path="${BB_PATH%/}/bbvars.sh"
fi
fi
fix_dir "$DATA_DIR"
fix_dir "$JOB_DIR/pending"
fix_dir "$JOB_DIR/processing"
fix_dir "$JOB_DIR/done"
fix_dir "$LOG_DIR"
fix_dir "$PID_DIR"
fix_dir "$CANCEL_DIR"
fix_dir "$TMP_DIR"
fix_dir "$CSRF_DIR"
fix_file "$CONFIG_FILE" "plugin-settings.conf"
if [ -n "$bbvars_path" ]; then
fix_file "$bbvars_path" "bbvars.sh"
else
warn "bbvars.sh path not resolved; skipped"
fi
for f in "$JOB_DIR"/pending/*.env "$JOB_DIR"/processing/*.env "$JOB_DIR"/done/*.{ok,fail,cancel,env.ok,env.fail,env.cancel}; do
if [ -f "$f" ]; then
fix_file "$f" "job file"
fi
done
exit 0