Harald Hoyer 55891e
From c1609dd497bb8f8f083a258ff2f7702385eb940b Mon Sep 17 00:00:00 2001
Harald Hoyer 55891e
From: Michal Soltys <soltys@ziu.info>
Harald Hoyer 55891e
Date: Fri, 7 Oct 2011 22:23:49 +0200
Harald Hoyer 55891e
Subject: [PATCH] convert_abs_rel() fixups
Harald Hoyer 55891e
Harald Hoyer 55891e
- IFS was not preserved, and modified value could leak to outside functions
Harald Hoyer 55891e
Harald Hoyer 55891e
- the '.' relative path should be returned for arguments such as /x/y/z
Harald Hoyer 55891e
  /x/y - but not for $1 == $2 ones
Harald Hoyer 55891e
Harald Hoyer 55891e
- $1 == $2 is self-looping link, so it returns final component of its
Harald Hoyer 55891e
  name
Harald Hoyer 55891e
Harald Hoyer 55891e
Signed-off-by: Michal Soltys <soltys@ziu.info>
Harald Hoyer 55891e
---
Harald Hoyer 55891e
 dracut-functions |   18 +++++++++++-------
Harald Hoyer 55891e
 1 files changed, 11 insertions(+), 7 deletions(-)
Harald Hoyer 55891e
Harald Hoyer 55891e
diff --git a/dracut-functions b/dracut-functions
Harald Hoyer 55891e
index c4f7f61..12dfa70 100755
Harald Hoyer 55891e
--- a/dracut-functions
Harald Hoyer 55891e
+++ b/dracut-functions
Harald Hoyer 55891e
@@ -91,20 +91,24 @@ normalize_path() {
Harald Hoyer 55891e
 }
Harald Hoyer 55891e
 
Harald Hoyer 55891e
 convert_abs_rel() {
Harald Hoyer 55891e
-    local __current __absolute __abssize __cursize __i __level __newpath
Harald Hoyer 55891e
+    local __current __absolute __abssize __cursize __newpath="" __oldifs
Harald Hoyer 55891e
+    local -i __i __level=0
Harald Hoyer 55891e
 #    PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
Harald Hoyer 55891e
 
Harald Hoyer 55891e
-    if [[ "$1" == "$2" ]]
Harald Hoyer 55891e
-    then
Harald Hoyer 55891e
-        echo "."
Harald Hoyer 55891e
-        return
Harald Hoyer 55891e
-    fi
Harald Hoyer 55891e
+    # corner case #1 - self looping link
Harald Hoyer 55891e
+    [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
Harald Hoyer 55891e
+
Harald Hoyer 55891e
+    # corner case #2 - own dir link
Harald Hoyer 55891e
+    [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
Harald Hoyer 55891e
+
Harald Hoyer 55891e
     __current=$(normalize_path "$1")
Harald Hoyer 55891e
     __absolute=$(normalize_path "$2")
Harald Hoyer 55891e
-    IFS="/"
Harald Hoyer 55891e
 
Harald Hoyer 55891e
+    __oldifs="$IFS"
Harald Hoyer 55891e
+    IFS="/"
Harald Hoyer 55891e
     __current=($__current)
Harald Hoyer 55891e
     __absolute=($__absolute)
Harald Hoyer 55891e
+    IFS="$__oldifs"
Harald Hoyer 55891e
 
Harald Hoyer 55891e
     __abssize=${#__absolute[@]}
Harald Hoyer 55891e
     __cursize=${#__current[@]}