Skip to content

Commit

Permalink
Improve link creation
Browse files Browse the repository at this point in the history
  • Loading branch information
HanabishiRecca committed Nov 8, 2022
1 parent 0994202 commit e4d13a5
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions caches-manager
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,38 @@ set -euo pipefail
shopt -s nullglob

create_cache() {
echo "L: $2"
local dir
local parent
dir="$(realpath -s "$2")"
parent="$(dirname "${dir}")"
echo -n "Linking '${dir}'... "
mkdir -p "$1"
[ -e "$2" ] && rm -r "$2"
ln -sT "$1" "$2" || true

if [ ! -d "${parent}" ]; then
echo "parent directory does not exist, skip."
return
fi

if [ ! -w "${parent}" ]; then
echo "no write access to parent directory, skip."
return
fi

if [ -L "${dir}" ]; then
if [ "$(readlink "${dir}")" == "$1" ]; then
echo "done."
return
fi
elif [ -e "${dir}" ]; then
echo -n "target exists, backing up... "
mv -fT "${dir}" "${dir}.bak"
fi

if ln -sfT "$1" "${dir}"; then
echo "done."
else
echo "failed."
fi
}

declare -a TARGETS
Expand Down

0 comments on commit e4d13a5

Please sign in to comment.