[pacman-dev] [PATCH] repo-add: bash implementation of realpath

Yun Zheng Hu yunzheng.hu at gmail.com
Sun Feb 15 10:21:38 EST 2009


Replaced readlink -f / realpath with a more portable bash implementation.

Signed-off-by: Yun Zheng Hu <yunzheng.hu at gmail.com>
---
 scripts/repo-add.sh.in |   37 ++++++++++++++++++++++++-------------
 1 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in
index c89e2a5..f266719 100644
--- a/scripts/repo-add.sh.in
+++ b/scripts/repo-add.sh.in
@@ -107,7 +107,7 @@ write_list_entry() {
 db_write_delta()
 {
 	# blank out all variables and set deltafile to absolute path
-	local deltafile=$($realpath "$1")
+	local deltafile=$(realpath "$1")
 	local filename=$(basename "$deltafile")
 	local deltavars pkgname fromver tover arch csize md5sum

@@ -138,7 +138,7 @@ db_write_delta()
 db_write_entry()
 {
 	# blank out all variables and set pkgfile to an absolute path
-	local pkgfile=$($realpath "$1")
+	local pkgfile=$(realpath "$1")
 	local pkgname pkgver pkgdesc url builddate packager csize size \
 		group depend backup license replaces provides conflict force \
 		_groups _depends _backups _licenses _replaces _provides _conflicts \
@@ -267,6 +267,27 @@ db_remove_entry() {
 	popd 2>&1 >/dev/null
 } # end db_remove_entry

+# bash implementation of realpath / readlink -f
+#   arg1 - filename
+realpath()
+{
+	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
+}
+
 # PROGRAM START

 # determine whether we have gettext; make it a no-op if we do not
@@ -294,16 +315,6 @@ if [ $# -lt 2 ]; then
 	exit 1
 fi

-# check for and store the name of a realpath-like program
-if [ $(type -t realpath) ]; then
-	realpath='realpath'
-elif [ $(type -t readlink) ]; then
-	realpath='readlink -f'
-else
-	error "$(gettext "Either realpath or readlink are required by repo-add.")"
-	exit 1 # $E_MISSING_PROGRAM
-fi
-
 # main routine
 gstmpdir=$(mktemp -d /tmp/repo-tools.XXXXXXXXXX) || (\
 	error "$(gettext "Cannot create temp directory for database building.")"; \
@@ -326,7 +337,7 @@ for arg in "$@"; do
 		QUIET=1
 	elif [ -z "$REPO_DB_FILE" ]; then
 		# store absolute path to repo DB
-		REPO_DB_FILE=$($realpath "$arg")
+		REPO_DB_FILE=$(realpath "$arg")
 		if [ -f "$REPO_DB_FILE" ]; then
 			if ! test_repo_db_file $cmd; then
 				error "$(gettext "Repository file '%s' is not a proper pacman
database.")" "$REPO_DB_FILE"
-- 
1.6.1.2


More information about the pacman-dev mailing list