From ff9f2d620f160b137a6aeab2b8ddcd5c5e36e1d1 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Tue, 15 Aug 2017 14:08:48 +0000 Subject: [PATCH] Fix #271: properly resolve symlinks in all paths (#329) I've replaced all instances in the code of `os.path.abspath` with `os.path.realpath`, as this does the same as `abspath` plus resolving symlinks. See also: https://stackoverflow.com/a/40311142 --- pmb/build/other.py | 6 +++--- pmb/challenge/build.py | 2 +- pmb/chroot/zap.py | 2 +- pmb/helpers/file.py | 2 +- pmb/helpers/mount.py | 4 ++-- test/test_apk_static.py | 2 +- test/test_aport_in_sync_with_git.py | 4 ++-- test/test_aportgen.py | 2 +- test/test_build.py | 2 +- test/test_build_is_necessary.py | 4 ++-- test/test_challenge_apk.py | 2 +- test/test_challenge_apkindex.py | 2 +- test/test_challenge_build.py | 2 +- test/test_chroot_interactive_shell.py | 2 +- test/test_keys.py | 2 +- test/test_mount.py | 2 +- test/test_repo.py | 2 +- test/test_shell_escape.py | 2 +- test/test_version.py | 2 +- 19 files changed, 24 insertions(+), 24 deletions(-) diff --git a/pmb/build/other.py b/pmb/build/other.py index df168d29..b482aa7b 100644 --- a/pmb/build/other.py +++ b/pmb/build/other.py @@ -103,7 +103,7 @@ def aports_files_out_of_sync_with_git(args, package=None): # Filter out a specific package if package: ret = [] - prefix = os.path.abspath( + prefix = os.path.realpath( pmb.build.other.find_aport( args, package)) + "/" for file in aports_files_out_of_sync_with_git(args): @@ -137,10 +137,10 @@ def aports_files_out_of_sync_with_git(args, package=None): working_dir=git_root, return_stdout=True) # Set absolute path, filter out aports files - aports_absolute = os.path.abspath(args.aports) + aports_absolute = os.path.realpath(args.aports) files = tracked.rstrip().split("\n") + untracked.rstrip().split("\n") for file in files: - file = os.path.abspath(git_root + "/" + file) + file = os.path.realpath(git_root + "/" + file) if file.startswith(aports_absolute): ret.append(file) else: diff --git a/pmb/challenge/build.py b/pmb/challenge/build.py index b4c5586b..693a486d 100644 --- a/pmb/challenge/build.py +++ b/pmb/challenge/build.py @@ -54,7 +54,7 @@ def build(args, apk_path): repo_diff = pmb.helpers.repo.diff(args, repo_before) # Diff the apk contents - staging_path = os.path.abspath(os.path.dirname(apk_path) + "/../") + staging_path = os.path.realpath(os.path.dirname(apk_path) + "/../") for file in repo_diff: file_staging = staging_path + "/" + file file_work = args.work + "/packages/" + file diff --git a/pmb/chroot/zap.py b/pmb/chroot/zap.py index 2237f39b..bb9f2b0a 100644 --- a/pmb/chroot/zap.py +++ b/pmb/chroot/zap.py @@ -39,7 +39,7 @@ def zap(args): patterns += ["cache_http"] for pattern in patterns: - pattern = os.path.abspath(args.work + "/" + pattern) + pattern = os.path.realpath(args.work + "/" + pattern) matches = glob.glob(pattern) for match in matches: if pmb.helpers.cli.confirm(args, "Remove " + match + "?"): diff --git a/pmb/helpers/file.py b/pmb/helpers/file.py index 3efe5463..3e828c53 100644 --- a/pmb/helpers/file.py +++ b/pmb/helpers/file.py @@ -66,7 +66,7 @@ def symlink(args, file, link): """ if os.path.exists(link): if (os.path.islink(link) and - os.path.abspath(os.readlink(link)) == os.path.abspath(file)): + os.path.realpath(os.readlink(link)) == os.path.realpath(file)): return raise RuntimeError("File exists: " + link) elif os.path.islink(link): diff --git a/pmb/helpers/mount.py b/pmb/helpers/mount.py index 8a53a942..bea29fa4 100644 --- a/pmb/helpers/mount.py +++ b/pmb/helpers/mount.py @@ -25,7 +25,7 @@ def ismount(folder): Ismount() implementation, that works for mount --bind. Workaround for: https://bugs.python.org/issue29707 """ - folder = os.path.abspath(os.path.realpath(folder)) + folder = os.path.realpath(os.path.realpath(folder)) with open("/proc/mounts", "r") as handle: for line in handle: words = line.split() @@ -86,7 +86,7 @@ def umount_all_list(prefix, source="/proc/mounts"): :returns: a list of folders, that need to be umounted """ ret = [] - prefix = os.path.abspath(prefix) + prefix = os.path.realpath(prefix) with open(source, "r") as handle: for line in handle: words = line.split() diff --git a/test/test_apk_static.py b/test/test_apk_static.py index 8e5e6dc7..d200669f 100644 --- a/test/test_apk_static.py +++ b/test/test_apk_static.py @@ -23,7 +23,7 @@ import glob import pytest # Import from parent directory -pmb_src = os.path.abspath(os.path.join(os.path.dirname(__file__) + "/..")) +pmb_src = os.path.realpath(os.path.join(os.path.dirname(__file__) + "/..")) sys.path.append(pmb_src) import pmb.chroot.apk_static import pmb.parse.apkindex diff --git a/test/test_aport_in_sync_with_git.py b/test/test_aport_in_sync_with_git.py index 4ef791a1..35e763f9 100644 --- a/test/test_aport_in_sync_with_git.py +++ b/test/test_aport_in_sync_with_git.py @@ -21,7 +21,7 @@ import sys import pytest # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.build.other import pmb.chroot.apk @@ -84,7 +84,7 @@ def out_of_sync_files(args): def test_aport_in_sync_with_git(args): aports = temp_aports_repo(args) ret_in_sync = [] - ret_out_of_sync = [args.aports + "/main/alpine-base/APKBUILD"] + ret_out_of_sync = [os.path.realpath(args.aports + "/main/alpine-base/APKBUILD")] # In sync (no files changed) assert out_of_sync_files(args) == ret_in_sync diff --git a/test/test_aportgen.py b/test/test_aportgen.py index 24e52426..8aa01ad3 100644 --- a/test/test_aportgen.py +++ b/test/test_aportgen.py @@ -22,7 +22,7 @@ import pytest import filecmp # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.aportgen import pmb.config diff --git a/test/test_build.py b/test/test_build.py index df4394f2..30cd0c5d 100644 --- a/test/test_build.py +++ b/test/test_build.py @@ -21,7 +21,7 @@ import sys import pytest # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.aportgen import pmb.config diff --git a/test/test_build_is_necessary.py b/test/test_build_is_necessary.py index 24f1d9ea..dc67f3f8 100644 --- a/test/test_build_is_necessary.py +++ b/test/test_build_is_necessary.py @@ -21,7 +21,7 @@ import sys import pytest # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.build.other import pmb.helpers.logging @@ -73,7 +73,7 @@ def cache_files_out_of_sync(args, is_out_of_sync): new = [] if is_out_of_sync: aport = pmb.build.other.find_aport(args, "hello-world") - new = [os.path.abspath(aport + "/APKBUILD")] + new = [os.path.realpath(aport + "/APKBUILD")] args.cache["aports_files_out_of_sync_with_git"] = new diff --git a/test/test_challenge_apk.py b/test/test_challenge_apk.py index d72e0a47..27941c19 100644 --- a/test/test_challenge_apk.py +++ b/test/test_challenge_apk.py @@ -22,7 +22,7 @@ import pytest import tarfile # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.challenge.apk_file import pmb.config diff --git a/test/test_challenge_apkindex.py b/test/test_challenge_apkindex.py index 2afef4f4..2192f3d5 100644 --- a/test/test_challenge_apkindex.py +++ b/test/test_challenge_apkindex.py @@ -21,7 +21,7 @@ import sys import pytest # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.challenge.apkindex import pmb.config diff --git a/test/test_challenge_build.py b/test/test_challenge_build.py index 08700e3a..3171e9cf 100644 --- a/test/test_challenge_build.py +++ b/test/test_challenge_build.py @@ -21,7 +21,7 @@ import sys import pytest # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.build.package import pmb.challenge.build diff --git a/test/test_chroot_interactive_shell.py b/test/test_chroot_interactive_shell.py index ea3a6884..645e1614 100644 --- a/test/test_chroot_interactive_shell.py +++ b/test/test_chroot_interactive_shell.py @@ -24,7 +24,7 @@ def test_chroot_interactive_shell(): """ Open a shell with 'pmbootstrap chroot' and pass 'echo hello_world\n' as stdin. """ - pmb_src = os.path.abspath(os.path.join(os.path.dirname(__file__) + "/..")) + pmb_src = os.path.realpath(os.path.join(os.path.dirname(__file__) + "/..")) os.chdir(pmb_src) ret = subprocess.check_output(["./pmbootstrap.py", "-q", "chroot"], timeout=300, input="echo hello_world\n", universal_newlines=True, diff --git a/test/test_keys.py b/test/test_keys.py index 13fd2742..4dc6b170 100644 --- a/test/test_keys.py +++ b/test/test_keys.py @@ -23,7 +23,7 @@ import glob import filecmp # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.parse.apkindex import pmb.helpers.git diff --git a/test/test_mount.py b/test/test_mount.py index 20910091..dee6f9de 100644 --- a/test/test_mount.py +++ b/test/test_mount.py @@ -20,7 +20,7 @@ import os import sys # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.helpers.mount diff --git a/test/test_repo.py b/test/test_repo.py index 389e8c0b..b0ef6cdd 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -24,7 +24,7 @@ import time import logging # Import from parent directory -pmb_src = os.path.abspath(os.path.join(os.path.dirname(__file__) + "/..")) +pmb_src = os.path.realpath(os.path.join(os.path.dirname(__file__) + "/..")) sys.path.append(pmb_src) import pmb.build.package import pmb.helpers.logging diff --git a/test/test_shell_escape.py b/test/test_shell_escape.py index 881ccffa..a9a67dd5 100644 --- a/test/test_shell_escape.py +++ b/test/test_shell_escape.py @@ -21,7 +21,7 @@ import sys import pytest # Import from parent directory -pmb_src = os.path.abspath(os.path.join(os.path.dirname(__file__) + "/..")) +pmb_src = os.path.realpath(os.path.join(os.path.dirname(__file__) + "/..")) sys.path.append(pmb_src) import pmb.chroot.root import pmb.chroot.user diff --git a/test/test_version.py b/test/test_version.py index 51b24403..86bbf2dd 100644 --- a/test/test_version.py +++ b/test/test_version.py @@ -21,7 +21,7 @@ import sys import pytest # Import from parent directory -sys.path.append(os.path.abspath( +sys.path.append(os.path.realpath( os.path.join(os.path.dirname(__file__) + "/.."))) import pmb.helpers.git import pmb.helpers.logging