From 31cc719784a0611e842b28e586934eaca60b2d40 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Fri, 6 Sep 2019 12:41:39 -0400 Subject: [PATCH 1/5] (hopeful) fix for #2 --- realpath | 101 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 45 deletions(-) diff --git a/realpath b/realpath index a931b57..7fbfb93 100755 --- a/realpath +++ b/realpath @@ -63,51 +63,62 @@ function realpath() if [ -z "$path" ]; then success=false else - # start with the file name (sans the trailing slash) - path="${path%/}" - - # if we stripped off the trailing slash and were left with nothing, that means we're in the root directory - if [ -z "$path" ]; then - path="/" - fi - - # get the basename of the file (ignoring '.' & '..', because they're really part of the path) - local file_basename="${path##*/}" - if [[ ( "$file_basename" = "." ) || ( "$file_basename" = ".." ) ]]; then - file_basename="" - fi - - # extracts the directory component of the full path, if it's empty then assume '.' (the current working directory) - local directory="${path%$file_basename}" - if [ -z "$directory" ]; then - directory='.' - fi - - # attempt to change to the directory - if ! cd "$directory" &>/dev/null ; then - success=false - fi - - if $success; then - # does the filename exist? - if [[ ( -n "$file_basename" ) && ( ! -e "$file_basename" ) ]]; then - success=false - fi - - # get the absolute path of the current directory & change back to previous directory - local abs_path="$(pwd -P)" - cd "-" &>/dev/null - - # Append base filename to absolute path - if [ "${abs_path}" = "/" ]; then - abs_path="${abs_path}${file_basename}" - else - abs_path="${abs_path}/${file_basename}" - fi - - # output the absolute path - echo "$abs_path" - fi + while $success; do + # start with the file name (sans the trailing slash) + path="${path%/}" + + # if we stripped off the trailing slash and were left with nothing, that means we're in the root directory + if [ -z "$path" ]; then + path="/" + fi + + # get the basename of the file (ignoring '.' & '..', because they're really part of the path) + local file_basename="${path##*/}" + if [[ ( "$file_basename" = "." ) || ( "$file_basename" = ".." ) ]]; then + file_basename="" + fi + + # extracts the directory component of the full path, if it's empty then assume '.' (the current working directory) + local directory="${path%$file_basename}" + if [ -z "$directory" ]; then + directory='.' + fi + + # attempt to change to the directory + if ! cd "$directory" &>/dev/null ; then + success=false + fi + + if $success; then + # does the filename exist? + if [[ ( -n "$file_basename" ) && ( ! -e "$file_basename" ) ]]; then + success=false + fi + + # get the absolute path of the current directory & change back to previous directory + local abs_path="$(pwd -P)" + cd "-" &>/dev/null + + # Append base filename to absolute path + if [ "${abs_path}" = "/" ]; then + abs_path="${abs_path}${file_basename}" + else + abs_path="${abs_path}/${file_basename}" + fi + + # If is a symlink, follow the symlink and try again with the newly discovered path + if [ -L "$abs_path" ]; then + path="$( readlink "$abs_path" )" + if [[ "$path:0:1" != '/' ]]; then + path="$abs_path_bak/$path" + fi + continue + fi + + # output the absolute path + echo "$abs_path" + fi + done fi $success From 3ed8068f4fbf8adb44a1b6fe60d6d5801a48d7de Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Fri, 6 Sep 2019 12:44:30 -0400 Subject: [PATCH 2/5] Update realpath --- realpath | 2 ++ 1 file changed, 2 insertions(+) diff --git a/realpath b/realpath index 7fbfb93..6f28bd7 100755 --- a/realpath +++ b/realpath @@ -98,6 +98,8 @@ function realpath() # get the absolute path of the current directory & change back to previous directory local abs_path="$(pwd -P)" cd "-" &>/dev/null + + local abs_path_bak="$abs_path" # Append base filename to absolute path if [ "${abs_path}" = "/" ]; then From 8f7df080c7e3e9b13fbd5879c4287d2acdf77b09 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Fri, 6 Sep 2019 12:46:09 -0400 Subject: [PATCH 3/5] Update realpath --- realpath | 1 + 1 file changed, 1 insertion(+) diff --git a/realpath b/realpath index 6f28bd7..fdbf363 100755 --- a/realpath +++ b/realpath @@ -119,6 +119,7 @@ function realpath() # output the absolute path echo "$abs_path" + break fi done fi From 65f007f16a9a4bfbcd9a7c8ce3e80ca2ff163a5c Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Fri, 6 Sep 2019 12:51:02 -0400 Subject: [PATCH 4/5] Update realpath-test.sh --- realpath-test.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/realpath-test.sh b/realpath-test.sh index 8b9eab4..e588e39 100755 --- a/realpath-test.sh +++ b/realpath-test.sh @@ -83,4 +83,18 @@ it_fails_for_relative_home_paths() { out=$(set +e ; ./realpath "$rel_path" >/dev/null ; echo $?) test $out -gt 0 -} \ No newline at end of file +} + +it_follows_symlink() { + dir="$(mktemp -d)" + cd "$dir" + mkdir -p tmp + ln -s .. ./tmp/symlink + cwd="$(pwd)" + + rel_path="tmp/symlink" + abs_path="${cwd}" + out_path="$(./realpath "$rel_path")" + + test "$out_path" = "$abs_path" +} From 451139796f44f15f20a7fcd18a0716cc63d92975 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Fri, 6 Sep 2019 13:19:49 -0400 Subject: [PATCH 5/5] Update realpath-test.sh --- realpath-test.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/realpath-test.sh b/realpath-test.sh index e588e39..78a64aa 100755 --- a/realpath-test.sh +++ b/realpath-test.sh @@ -86,6 +86,7 @@ it_fails_for_relative_home_paths() { } it_follows_symlink() { + cmd="$(pwd)/realpath" dir="$(mktemp -d)" cd "$dir" mkdir -p tmp @@ -94,7 +95,7 @@ it_follows_symlink() { rel_path="tmp/symlink" abs_path="${cwd}" - out_path="$(./realpath "$rel_path")" + out_path="$("$cmd" "$rel_path")" test "$out_path" = "$abs_path" }