Skip to content

Commit

Permalink
Fix path existence test order
Browse files Browse the repository at this point in the history
  • Loading branch information
guimspace committed Mar 3, 2024
1 parent 665fd1e commit 85d1089
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/fopus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $$
Expand Down Expand Up @@ -535,21 +538,23 @@ 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
>&2 echo "fopus: $OUTPUT_PATH: Permission denied"
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"
Expand Down

0 comments on commit 85d1089

Please sign in to comment.