Merge remote-tracking branch 'origin/pr/90'

This commit is contained in:
Julian Ospald 2019-05-24 10:08:36 +08:00
commit 8a6ff2ff16
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
1 changed files with 10 additions and 4 deletions

14
ghcup
View File

@ -1130,29 +1130,35 @@ get_distro_alias() {
# and # and
# https://stackoverflow.com/a/246128 # https://stackoverflow.com/a/246128
# #
# If the file does not exist, just prints the argument unchanged. # If the file does not exist, just prints it appended to the current directory.
# @STDOUT: realpath of the given file # @STDOUT: realpath of the given file
posix_realpath() { posix_realpath() {
[ -z "$1" ] && die "Internal error: no argument given to posix_realpath" [ -z "$1" ] && die "Internal error: no argument given to posix_realpath"
current_loop=0
max_loops=50
mysource=$1 mysource=$1
while [ -h "${mysource}" ]; do while [ -h "${mysource}" ]; do
current_loop=$((current_loop+1))
mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null 2>&1 && pwd )" mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null 2>&1 && pwd )"
mysource="$(readlink "${mysource}")" mysource="$(readlink "${mysource}")"
[ "${mysource%${mysource#?}}"x != '/x' ] && mysource="${mydir}/${mysource}" [ "${mysource%${mysource#?}}"x != '/x' ] && mysource="${mydir}/${mysource}"
if [ ${current_loop} -gt ${max_loops} ] ; then
(>&2 echo "${1}: Too many levels of symbolic links")
break
fi
done done
mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null 2>&1 && pwd )" mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null 2>&1 && pwd )"
# TODO: better distinguish between "does not exist" and "permission denied" # TODO: better distinguish between "does not exist" and "permission denied"
if [ -z "${mydir}" ] ; then if [ -z "${mydir}" ] ; then
(>&2 echo "${1}: Permission denied") (>&2 echo "${1}: Permission denied")
elif [ ! -e "$1" ] ; then
echo "${mysource}"
else else
echo "${mydir%/}/$(basename "${mysource}")" echo "${mydir%/}/$(basename "${mysource}")"
fi fi
unset mysource mydir posix_realpath_error unset current_loop max_loops mysource mydir
} }