Bring script headers onto the template and close safeguard gaps

Headers claimed protections the code never had, and several destructive paths had no
guard against a collapsed config value.
This commit is contained in:
Gmer4Lfe
2026-08-01 20:37:59 -04:00
parent cdce877601
commit e8b114094a
78 changed files with 3301 additions and 277 deletions
+52 -5
View File
@@ -64,11 +64,45 @@
# OPERATIONAL SAFEGUARDS
# ==============================================================================================
#
# acquire_lock "wait" — wait if previous run still active
# detect_hosts() — correct folder lists per host via MY_ID aliases
# Empty array guards — warns and exits cleanly if no folders or patterns configured
# Folder existence — skips missing folders with warning, continues others
# platform_require_cmd — notify script validated before use
# Root Enforcement
# Media files are owned by container users; deleting them requires root.
#
# Profile Required
# Exits with usage if no profile is given. There is no default profile — an
# unspecified profile must never fall through to cleaning something.
#
# Lock Acquisition
# acquire_lock "wait" — waits for a previous run to finish rather than
# skipping, so a long anime pass does not cause the media pass to be dropped.
#
# Host Detection
# detect_hosts() aliases HOST*_ANIME_CLEAN_FOLDERS / HOST*_MEDIA_CLEAN_FOLDERS
# to the correct host's values.
#
# Empty Array Guards
# Exits cleanly if the resolved folder list or pattern list is empty. An empty
# pattern list would otherwise build a find with no -iname terms and match
# every file in the tree.
#
# Clean Path Depth Guard
# Every folder must be an absolute path at least three levels deep before it is
# scanned. The patterns include *.sh, *.zip, *.rar and *.exe, so a truncated
# entry like /mnt/user — which passes an existence check — would delete
# matching files across every share on the array.
#
# Folder Existence
# Missing folders are skipped with a warning; remaining folders still process.
#
# Explicit Pattern List
# Only patterns named in ANIME_FILE_PATTERNS / MEDIA_FILE_PATTERNS are removed.
# The script never infers intent from file size, age, or location.
#
# Count Before Delete
# Matching files are counted first; a folder with zero matches short-circuits
# before any rm is constructed.
#
# Dry Run Support
# --dry-run lists every file that would be deleted and removes nothing.
#
# ==============================================================================================
# CONFIGURATION
@@ -205,6 +239,19 @@ for FOLDER in "${CLEAN_FOLDERS[@]}"; do
FOLDER_NAME=$(basename "$FOLDER")
echo "━━━ $ICON_CLEAN $FOLDER_NAME ━━━"
# The pattern list includes *.sh, *.zip, *.rar and *.exe. A truncated entry such as
# /mnt/user passes the -d check below and would sweep every share on the array, so
# require an absolute path at least three levels deep before scanning anything.
_depth="${FOLDER//[^\/]/}"
if [[ -z "$FOLDER" || "$FOLDER" != /* || "${#_depth}" -lt 3 ]]; then
error "Refusing to clean unsafe path: '${FOLDER:-empty}' — expected an absolute path at least 3 levels deep"
notify "Media cleaner ($PROFILE) refused unsafe path on $(hostname): '${FOLDER:-empty}'" \
"Media Cleaner" "warning"
FAILED+=("${FOLDER_NAME:-empty}")
echo ""
continue
fi
if [[ ! -d "$FOLDER" ]]; then
warn "$FOLDER_NAME not found — skipping"
SKIPPED+=("$FOLDER_NAME")