This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch master
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=8c59d393220ab34cd7a6ee3791f05ae15d3cf44b

commit 8c59d393220ab34cd7a6ee3791f05ae15d3cf44b
Author: Helmut Grohne <[email protected]>
AuthorDate: Wed May 20 18:35:08 2020 +0200

    dpkg-realpath: Add support for DPKG_ROOT
    
    [[email protected]:
      - Hook the canonicalize function into the script.
      - Document the DPKG_ROOT usage in the man page. ]
    
    Signed-off-by: Guillem Jover <[email protected]>
---
 debian/changelog         |  2 ++
 man/dpkg-realpath.pod    |  4 +++
 scripts/dpkg-realpath.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/debian/changelog b/debian/changelog
index 24fee9959..120f3b23a 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -37,6 +37,8 @@ dpkg (1.20.1) UNRELEASED; urgency=medium
   * dpkg-realpath: New program, to be used by dpkg-maintscript-helper, and
     any maintainer script that needs a realpath that can handle pathnames
     relative to the dpkg root directory.
+  * dpkg-realpath: Add support for DPKG_ROOT.
+    Thanks to Helmut Grohne <[email protected]>.
   * Portability:
     - libdpkg: When using uselocale(), include <xlocale.h> for locale_t if
       the header is available. Needed on BSDs.
diff --git a/man/dpkg-realpath.pod b/man/dpkg-realpath.pod
index ae093bfbf..4ee3e6745 100644
--- a/man/dpkg-realpath.pod
+++ b/man/dpkg-realpath.pod
@@ -47,6 +47,10 @@ Show the version and exit.
 
 =over
 
+=item B<DPKG_ROOT>
+
+If set, it will be used as the filesystem root directory.
+
 =item B<DPKG_COLORS>
 
 Sets the color mode.
diff --git a/scripts/dpkg-realpath.sh b/scripts/dpkg-realpath.sh
index e150bdba1..f2747c8fe 100755
--- a/scripts/dpkg-realpath.sh
+++ b/scripts/dpkg-realpath.sh
@@ -1,5 +1,6 @@
 #!/bin/sh
 #
+# Copyright © 2020 Helmut Grohne <[email protected]>
 # Copyright © 2020 Guillem Jover <[email protected]>
 #
 # This program is free software; you can redistribute it and/or modify
@@ -45,6 +46,73 @@ Options:
 END
 }
 
+canonicalize() {
+  local src="$1"
+  local root="$2"
+  local loop=0
+  local result="$root"
+  local dst
+
+  if [ "${src#"$root"}" = "$src" ]; then
+    error "link not within root"
+  fi
+  # Remove prefixed root dir.
+  src=${src#"$root"}
+  # Remove prefixed slashes.
+  while [ "$src" != "${src#/}" ]; do
+     src=${src#/}
+  done
+  while [ -n "$src" ]; do
+    loop=$((loop + 1))
+    if [ "$loop" -gt 25 ]; then
+      error "too many levels of symbolic links"
+    fi
+    # Get the first directory component.
+    prefix=${src%%/*}
+    # Remove the first directory component from src.
+    src=${src#"$prefix"}
+    # Remove prefixed slashes.
+    while [ "$src" != "${src#/}" ]; do
+      src=${src#/}
+    done
+    # Resolve the first directory component.
+    if [ "$prefix" = . ]; then
+      # Ignore, stay at the same directory.
+      :
+    elif [ "$prefix" = .. ]; then
+      # Go up one directory.
+      result=${result%/*}
+      if [ "${result#"$root"}" = "$result" ]; then
+        result="$root"
+      fi
+    elif [ -h "$result/$prefix" ]; then
+      # Resolve the symlink within $result.
+      dst=$(readlink "$result/$prefix")
+      case "$dst" in
+      /*)
+        # Absolute pathname, reset result back to $root.
+        result=$root
+        src="$dst${src:+/$src}"
+        # Remove prefixed slashes.
+        while [ "$src" != "${src#/}" ]; do
+          src=${src#/}
+        done
+        ;;
+      *)
+        # Relative pathname.
+        src="$dst${src:+/$src}"
+        ;;
+      esac
+    else
+      # Otherwise append the prefix.
+      result="$result/$prefix"
+    fi
+  done
+  # We are done, print the resolved pathname, w/o $root.
+  result="${result#"$root"}"
+  echo "${result:-/}"
+}
+
 setup_colors
 
 [ $# -eq 1 ] || badusage "missing pathname"
@@ -73,6 +141,6 @@ while [ $# -ne 0 ]; do
   shift
 done
 
-realpath "$pathname"
+canonicalize "$pathname" "${DPKG_ROOT:-}"
 
 exit 0

-- 
Dpkg.Org's dpkg

Reply via email to