[pacman-dev] [PATCH] repo-add: added a realpath implementation in bash
When realpath or readlink is not available it will fallback to a realpath implementation in bash. An extra check is also added for "readlink -f", because not all platforms support the -f flag. The readlink implementation in bash is borrowed from Debian's dpkg tools. Signed-off-by: Yun Zheng Hu <yunzheng.hu@gmail.com> --- scripts/repo-add.sh.in | 23 ++++++++++++++++++++--- 1 files changed, 20 insertions(+), 3 deletions(-) diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in index c89e2a5..ea16731 100644 --- a/scripts/repo-add.sh.in +++ b/scripts/repo-add.sh.in @@ -297,11 +297,28 @@ fi # check for and store the name of a realpath-like program if [ $(type -t realpath) ]; then realpath='realpath' -elif [ $(type -t readlink) ]; then +elif [ $(type -t readlink) -a "$(readlink -f . 2>/dev/null)" != "" ]; then realpath='readlink -f' else - error "$(gettext "Either realpath or readlink are required by repo-add.")" - exit 1 # $E_MISSING_PROGRAM + realpath_bash() + { + fname=${1%/} # strips trailing '/' + while [ -L "$fname" ]; do + oldfname="$fname" + fname="$(command ls -l $fname)" + fname="${fname#*\> }" + if [ "$fname" = . ] ; then + fname="$(dirname $oldfname)" + elif echo $fname | grep -vq '^/' - ; then + fname="$(dirname $oldfname)/$fname" + fi + done + pushd $(dirname $fname) > /dev/null + fname=$(pwd -P)/$(basename $fname) + popd > /dev/null + echo $fname + } + realpath=realpath_bash fi # main routine -- 1.6.1.2
On 11/02/2009, at 7:33 AM, Yun Zheng Hu wrote:
When realpath or readlink is not available it will fallback to a realpath implementation in bash. An extra check is also added for "readlink -f", because not all platforms support the -f flag.
The readlink implementation in bash is borrowed from Debian's dpkg tools.
Awesome! This, along with "fix eval and quote issues", makes repo-add compatible with Mac OS X (afaik)! P.S. I haven't actually tested it on symlinks. [1] http://www.archlinux.org/pipermail/pacman-dev/2009-February/008127.html
Yun Zheng Hu wrote:
When realpath or readlink is not available it will fallback to a realpath implementation in bash. An extra check is also added for "readlink -f", because not all platforms support the -f flag.
The readlink implementation in bash is borrowed from Debian's dpkg tools.
Signed-off-by: Yun Zheng Hu <yunzheng.hu@gmail.com> --- scripts/repo-add.sh.in | 23 ++++++++++++++++++++--- 1 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in index c89e2a5..ea16731 100644 --- a/scripts/repo-add.sh.in +++ b/scripts/repo-add.sh.in @@ -297,11 +297,28 @@ fi # check for and store the name of a realpath-like program if [ $(type -t realpath) ]; then realpath='realpath' -elif [ $(type -t readlink) ]; then +elif [ $(type -t readlink) -a "$(readlink -f . 2>/dev/null)" != "" ]; then realpath='readlink -f' else - error "$(gettext "Either realpath or readlink are required by repo-add.")" - exit 1 # $E_MISSING_PROGRAM + realpath_bash() + { + fname=${1%/} # strips trailing '/' + while [ -L "$fname" ]; do + oldfname="$fname" + fname="$(command ls -l $fname)" + fname="${fname#*\> }" + if [ "$fname" = . ] ; then + fname="$(dirname $oldfname)" + elif echo $fname | grep -vq '^/' - ; then + fname="$(dirname $oldfname)/$fname" + fi + done + pushd $(dirname $fname) > /dev/null + fname=$(pwd -P)/$(basename $fname) + popd > /dev/null + echo $fname + } + realpath=realpath_bash fi
# main routine
I haven't looked at this in detail yet, but if this solution is more portable and has no feature loss, why not use it by default? Allan
On Wed, Feb 11, 2009 at 10:15 AM, Allan McRae <allan@archlinux.org> wrote:
I haven't looked at this in detail yet, but if this solution is more portable and has no feature loss, why not use it by default?
I had the same question :P
On Wed, Feb 11, 2009 at 6:59 AM, Xavier <shiningxc@gmail.com> wrote:
On Wed, Feb 11, 2009 at 10:15 AM, Allan McRae <allan@archlinux.org> wrote:
I haven't looked at this in detail yet, but if this solution is more portable and has no feature loss, why not use it by default?
I had the same question :P
My guess would be optimization - that calling a C binary may be faster than a bash loop. Not sure though, just a guess.
On Wed, Feb 11, 2009 at 5:31 PM, Aaron Griffin <aaronmgriffin@gmail.com> wrote:
On Wed, Feb 11, 2009 at 6:59 AM, Xavier <shiningxc@gmail.com> wrote:
On Wed, Feb 11, 2009 at 10:15 AM, Allan McRae <allan@archlinux.org> wrote:
I haven't looked at this in detail yet, but if this solution is more portable and has no feature loss, why not use it by default?
I had the same question :P
My guess would be optimization - that calling a C binary may be faster than a bash loop. Not sure though, just a guess.
readlink and realpath would be faster i think, but not sure if that really matters for repo-add. I'll submit a new patch later which will replace readlink and realpath with the bash implementation.
participants (5)
-
Aaron Griffin
-
Allan McRae
-
Sebastian Nowicki
-
Xavier
-
Yun Zheng Hu