From 85d108958790c4719182b8d6595ac295cfdb19b2 Mon Sep 17 00:00:00 2001 From: Guilherme Tadashi Maeoka Date: Sat, 2 Mar 2024 23:24:31 -0300 Subject: [PATCH] Fix path existence test order --- src/fopus.sh | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/fopus.sh b/src/fopus.sh index 0fe1f3d..17efcbb 100644 --- a/src/fopus.sh +++ b/src/fopus.sh @@ -44,22 +44,25 @@ cleanup() trap - SIGINT SIGTERM EXIT declare -rg CLEANUP_DIR - local -r target=$(realpath -e "$CLEANUP_DIR") if [[ "$RC" -eq 0 ]]; then : - elif [[ ! -e "$target" ]]; then - : - elif [[ ! -O "$target" ]]; then - : - elif [[ ! -d "$target" ]]; then - : - elif [[ -n "${target%/*}" ]]; then - : - elif [[ "$target" == "/" ]]; then + elif [[ ! -e "$CLEANUP_DIR" ]]; then : else - rm -r "$target" + local -r target=$(realpath -e "$CLEANUP_DIR") + + if [[ ! -O "$target" ]]; then + : + elif [[ ! -d "$target" ]]; then + : + elif [[ -z "${target%/*}" ]]; then + : + elif [[ "$target" == "/" ]]; then + : + else + rm -r "$target" + fi fi kill -SIGINT $$ @@ -535,12 +538,8 @@ main() fi OUTPUT_PATH="${CONFIG[repopath]}" - OUTPUT_PATH=$(realpath -e "$OUTPUT_PATH") - if [[ -z "${OUTPUT_PATH%/*}" ]]; then - >&2 echo "fopus: $OUTPUT_PATH: Permission denied" - exit 1 - elif [[ ! -d "$OUTPUT_PATH" ]]; then + if [[ ! -d "$OUTPUT_PATH" ]]; then >&2 echo "fopus: $OUTPUT_PATH: No such directory" exit 1 elif [[ ! -w "$OUTPUT_PATH" ]]; then @@ -548,8 +547,14 @@ main() exit 1 fi + OUTPUT_PATH=$(realpath -e "$OUTPUT_PATH") declare -r OUTPUT_PATH="$OUTPUT_PATH" + if [[ -z "${OUTPUT_PATH%/*}" ]]; then + >&2 echo "fopus: $OUTPUT_PATH: Permission denied" + exit 1 + fi + for file in "${FILES[@]}"; do if [[ "$OUTPUT_PATH" == "$file/"* ]]; then >&2 echo "fopus: invalid output path"