tree-wide: replace realpath with readlink -f
authorŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 19 Mar 2024 13:33:16 +0000 (14:33 +0100)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 19 Mar 2024 13:33:16 +0000 (14:33 +0100)
Tizen's coreutils doesn't include 'realpath' utility, which was
introduced in coreutils 8.15. However, Tizen's coreutils includes 'readlink'
utility, which is very close to 'realpath' if called with '-f' param.

Change-Id: I08b6ed68fc6bf0c384b599f4de3724724345d5c7
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
meson.build
test/test-functions
tools/debug-sd-boot.sh
tools/find-build-dir.sh
tools/relative-path.sh [new file with mode: 0755]

index bc27274..fed5ffb 100644 (file)
@@ -25,8 +25,9 @@ conf.set('PROJECT_VERSION', meson.project_version(),
 # the wrong result when systemd is being built as a meson subproject
 project_source_root = meson.current_source_dir()
 project_build_root = meson.current_build_dir()
-relative_source_path = run_command('realpath',
-                                   '--relative-to=@0@'.format(project_build_root),
+relative_path_sh = find_program('tools/relative-path.sh')
+relative_source_path = run_command(relative_path_sh,
+                                   '@0@'.format(project_build_root),
                                    project_source_root,
                                    check : true).stdout().strip()
 conf.set_quoted('RELATIVE_SOURCE_PATH', relative_source_path)
index 556346d..2c06a99 100644 (file)
@@ -98,9 +98,9 @@ if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
 fi
 
 # The calling test.sh scripts have TEST_BASE_DIR set via their Makefile, but we don't need them to provide it
-TEST_BASE_DIR=${TEST_BASE_DIR:-$(realpath "$(dirname "${BASH_SOURCE[0]}")")}
-TEST_UNITS_DIR="$(realpath "$TEST_BASE_DIR/units")"
-SOURCE_DIR=$(realpath "$TEST_BASE_DIR/..")
+TEST_BASE_DIR=${TEST_BASE_DIR:-$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")}
+TEST_UNITS_DIR="$(readlink -f "$TEST_BASE_DIR/units")"
+SOURCE_DIR=$(readlink -f "$TEST_BASE_DIR/..")
 # These variables are used by test scripts
 export TEST_BASE_DIR TEST_UNITS_DIR SOURCE_DIR
 
@@ -126,7 +126,7 @@ if [ -z "$TESTFILE" ]; then
     echo "ERROR: test-functions must be sourced from one of the TEST-*/test.sh scripts" >&2
     exit 1
 fi
-TESTNAME="$(basename "$(dirname "$(realpath "$TESTFILE")")")"
+TESTNAME="$(basename "$(dirname "$(readlink -f "$TESTFILE")")")"
 
 WORKDIR="/var/tmp/systemd-tests"
 if get_bool "${NO_BUILD:=}"; then
@@ -215,7 +215,6 @@ BASICTOOLS=(
     pkill
     ps
     readlink
-    realpath
     rev
     rm
     rmdir
@@ -3302,7 +3301,7 @@ test_setup() {
     fi
 
     if [ -e "${IMAGE_PRIVATE:?}" ]; then
-        echo "Reusing existing image $IMAGE_PRIVATE → $(realpath "$IMAGE_PRIVATE")"
+        echo "Reusing existing image $IMAGE_PRIVATE → $(readlink -f "$IMAGE_PRIVATE")"
         mount_initdir
     else
         if [ ! -e "${IMAGE_PUBLIC:?}" ]; then
@@ -3323,18 +3322,18 @@ test_setup() {
                 IMAGE_PUBLIC="${image_old}"
             fi
             if [ "${IMAGE_NAME:?}" != "default" ] && ! get_bool "${TEST_FORCE_NEWIMAGE}"; then
-                cp -v "$(realpath "${IMAGESTATEDIR}/default.img")" "$IMAGE_PUBLIC"
+                cp -v "$(readlink -f "${IMAGESTATEDIR}/default.img")" "$IMAGE_PUBLIC"
             fi
         fi
 
         local hook_defined
         declare -f -F test_append_files >/dev/null && hook_defined=yes || hook_defined=no
 
-        echo "Reusing existing cached image $IMAGE_PUBLIC → $(realpath "$IMAGE_PUBLIC")"
+        echo "Reusing existing cached image $IMAGE_PUBLIC → $(readlink -f "$IMAGE_PUBLIC")"
         if get_bool "$TEST_PARALLELIZE" || get_bool "$hook_defined"; then
-            cp -v -- "$(realpath "$IMAGE_PUBLIC")" "$IMAGE_PRIVATE"
+            cp -v -- "$(readlink -f "$IMAGE_PUBLIC")" "$IMAGE_PRIVATE"
         else
-            ln -sv -- "$(realpath "$IMAGE_PUBLIC")" "$IMAGE_PRIVATE"
+            ln -sv -- "$(readlink -f "$IMAGE_PUBLIC")" "$IMAGE_PRIVATE"
         fi
 
         mount_initdir
index 0420dbd..302629a 100755 (executable)
@@ -29,13 +29,13 @@ if [[ $# -lt 2 ]]; then
     exit 1
 fi
 
-binary=$(realpath "${1}")
+binary=$(readlink -f "${1}")
 if [[ "${1}" =~ systemd-boot([[:alnum:]]+).efi ]]; then
     target="systemd-boot"
-    symbols=$(realpath "${1%efi}elf")
+    symbols=$(readlink -f "${1%efi}elf")
 elif [[ "${1}" =~ linux([[:alnum:]]+).efi.stub ]]; then
     target="systemd-stub"
-    symbols=$(realpath "${1%efi.stub}elf.stub")
+    symbols=$(readlink -f "${1%efi.stub}elf.stub")
 else
     echo "Cannot detect EFI binary '${1}'."
     exit 1
index 79a79fc..791752e 100755 (executable)
@@ -6,11 +6,11 @@ set -eu
 # we look for subdirectories of the parent directory that look like ninja build dirs.
 
 if [ -n "${BUILD_DIR:=}" ]; then
-    realpath "$BUILD_DIR"
+    readlink -f "$BUILD_DIR"
     exit 0
 fi
 
-root="$(dirname "$(realpath "$0")")"
+root="$(dirname "$(readlink -f "$0")")"
 
 found=
 for i in "$root"/../*/build.ninja; do
@@ -30,4 +30,4 @@ if [ -z "$found" ]; then
     exit 1
 fi
 
-realpath "$found"
+readlink -f "$found"
diff --git a/tools/relative-path.sh b/tools/relative-path.sh
new file mode 100755 (executable)
index 0000000..da42936
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/sh
+# SPDX-License-Identifier: LGPL-2.1-or-later
+set -eu
+
+# relative-path.sh <RELATIVE_TO> <PATH>
+#
+# Print resolved <PATH> relative to <RELATIVE_TO>
+
+full_path=$(readlink -f ${2})/
+relative_path=$(readlink -f ${1})/
+
+while [ -n "${full_path}" -a \
+        -n "${relative_path}" -a \
+        "${full_path%%/*}" =  "${relative_path%%/*}" ]; do
+    full_path=${full_path#*/}
+    relative_path=${relative_path#*/}
+done
+
+dots=""
+
+full_path=${full_path%/}
+relative_path=${relative_path%/}
+
+relative_path=${relative_path:+/$relative_path}
+while [ -n "${relative_path}" ]; do
+    dots="../$dots"
+    relative_path=${relative_path%/*}
+done
+dots=${dots%/}
+if [ -z "$full_path" ]; then
+    echo ${dots}
+else
+    echo ${dots}/${full_path}
+fi