diff mbox series

[wic] oe/path: don't follow symlinks in copyhardlinktree()'s hardlink test

Message ID 20261203-fix-copyhardlinktree-dangling-symlink-v1-1-17e089e290a3@wabtec.com
State New
Headers show
Series [wic] oe/path: don't follow symlinks in copyhardlinktree()'s hardlink test | expand

Commit Message

Leferenz, Florian Dec. 3, 2026, 8:32 a.m. UTC
From: Florian Leferenz <florian.leferenz@wabtec.com>

copyhardlinktree() decides between hardlinking and a full tar copy by
hardlinking the first file os.walk() returns. os.link() follows symlinks
by default, so when that entry is a symlink whose target does not exist
on the build host (e.g. var/lock -> /run/lock in a target rootfs) the
test fails with ENOENT and the whole tree is copied with tar instead.
Which entry comes first depends on the directory order of the host
filesystem, so the same rootfs is hardlinked on one build host and fully
copied on another.

Link the entry itself instead of its target and add a regression test.

Signed-off-by: Florian Leferenz <florian.leferenz@wabtec.com>
---
wic: fix copyhardlinktree() hardlink test with dangling symlinks
---
 src/wic/oe/path.py         |  2 +-
 tests/unit/test_oe_path.py | 29 +++++++++++++++++++++++++++--
 2 files changed, 28 insertions(+), 3 deletions(-)


---
base-commit: 610a61a25e5fad0b62abd19d60f0b0e210b1df34
change-id: 20261203-fix-copyhardlinktree-dangling-symlink-81576ddd1b24

Best regards,

Comments

Ross Burton Dec. 3, 2026, 9:52 a.m. UTC | #1
Hi Florian,

Well that’s a fun bug!  Could you also send the same patch to oe-core, as wic’s path.py is a copy of oe-core’s meta/lib/oe/path.py.

Cheers,
Ross

> On 3 Dec 2026, at 08:32, Florian Leferenz via B4 Relay via yocto-patches <florian.leferenz=wabtec.com@lists.yoctoproject.org> wrote:
>
> From: Florian Leferenz <florian.leferenz@wabtec.com>
>
> copyhardlinktree() decides between hardlinking and a full tar copy by
> hardlinking the first file os.walk() returns. os.link() follows symlinks
> by default, so when that entry is a symlink whose target does not exist
> on the build host (e.g. var/lock -> /run/lock in a target rootfs) the
> test fails with ENOENT and the whole tree is copied with tar instead.
> Which entry comes first depends on the directory order of the host
> filesystem, so the same rootfs is hardlinked on one build host and fully
> copied on another.
>
> Link the entry itself instead of its target and add a regression test.
>
> Signed-off-by: Florian Leferenz <florian.leferenz@wabtec.com>
> ---
> wic: fix copyhardlinktree() hardlink test with dangling symlinks
> ---
> src/wic/oe/path.py         |  2 +-
> tests/unit/test_oe_path.py | 29 +++++++++++++++++++++++++++--
> 2 files changed, 28 insertions(+), 3 deletions(-)
>
> diff --git a/src/wic/oe/path.py b/src/wic/oe/path.py
> index 95bb964..6d61a6c 100644
> --- a/src/wic/oe/path.py
> +++ b/src/wic/oe/path.py
> @@ -112,7 +112,7 @@ def copyhardlinktree(src, dst):
>
>     if testfile is not None:
>         try:
> -            os.link(testfile, os.path.join(dst, 'testfile'))
> +            os.link(testfile, os.path.join(dst, 'testfile'), follow_symlinks=False)
>             os.unlink(os.path.join(dst, 'testfile'))
>             canhard = True
>         except Exception as e:
> diff --git a/tests/unit/test_oe_path.py b/tests/unit/test_oe_path.py
> index 3abe1f0..181128b 100644
> --- a/tests/unit/test_oe_path.py
> +++ b/tests/unit/test_oe_path.py
> @@ -1,8 +1,9 @@
> """
> Unit tests for wic/oe/path.py, covering wic's own path logic: join,
> is_path_parent, symlink, make_relative_symlink, canonicalize, which_wild,
> -and realpath. Thin standard-library wrappers (relative -> os.path.relpath,
> -find -> os.walk) are left to the standard library.
> +realpath and copyhardlinktree. Thin standard-library wrappers
> +(relative -> os.path.relpath, find -> os.walk) are left to the standard
> +library.
> """
> import errno
> import os
> @@ -23,6 +24,7 @@ from wic.oe.path import (
>     canonicalize,
>     which_wild,
>     realpath,
> +    copyhardlinktree,
> )
>
>
> @@ -376,3 +378,26 @@ class TestRealpath:
>
>         monkeypatch.setattr(os.path, "isdir", boom)
>         assert realpath(str(sub), str(tmp_path), use_physdir=False) == str(sub)
> +
> +
> +class TestCopyhardlinktree:
> +    def test_hardlinks_files(self, tmp_path):
> +        src = tmp_path / "src"
> +        (src / "sub").mkdir(parents=True)
> +        (src / "sub" / "file").write_text("data")
> +        dst = tmp_path / "dst"
> +        copyhardlinktree(str(src), str(dst))
> +        assert os.path.samefile(src / "sub" / "file", dst / "sub" / "file")
> +
> +    def test_dangling_symlink_as_first_entry_still_hardlinks(self, tmp_path):
> +        # The hardlink test uses the first file os.walk() returns. A rootfs
> +        # symlink such as var/lock -> /run/lock dangles on the build host and
> +        # must not make the test fail and force a full tar copy.
> +        src = tmp_path / "src"
> +        (src / "sub").mkdir(parents=True)
> +        os.symlink("/nonexistent/target", str(src / "dangling"))
> +        (src / "sub" / "file").write_text("data")
> +        dst = tmp_path / "dst"
> +        copyhardlinktree(str(src), str(dst))
> +        assert os.path.samefile(src / "sub" / "file", dst / "sub" / "file")
> +        assert os.readlink(dst / "dangling") == "/nonexistent/target"
>
> ---
> base-commit: 610a61a25e5fad0b62abd19d60f0b0e210b1df34
> change-id: 20261203-fix-copyhardlinktree-dangling-symlink-81576ddd1b24
>
> Best regards,
> --
> Florian Leferenz <florian.leferenz@wabtec.com>
>
>
>
>
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#5654): https://lists.yoctoproject.org/g/yocto-patches/message/5654
> Mute This Topic: https://lists.yoctoproject.org/mt/122570680/6875888
> Group Owner: yocto-patches+owner@lists.yoctoproject.org
> Unsubscribe: https://lists.yoctoproject.org/g/yocto-patches/leave/13162211/6875888/133974524/xyzzy [ross.burton@arm.com]
> -=-=-=-=-=-=-=-=-=-=-=-
>
>

IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Leferenz, Florian Dec. 3, 2026, 10:19 a.m. UTC | #2
3 - Highly Confidential

Hi Ross,

Thanks for having a look. I've now also sent same patch to oe-core.

Regards,
Florian
diff mbox series

Patch

diff --git a/src/wic/oe/path.py b/src/wic/oe/path.py
index 95bb964..6d61a6c 100644
--- a/src/wic/oe/path.py
+++ b/src/wic/oe/path.py
@@ -112,7 +112,7 @@  def copyhardlinktree(src, dst):
 
     if testfile is not None:
         try:
-            os.link(testfile, os.path.join(dst, 'testfile'))
+            os.link(testfile, os.path.join(dst, 'testfile'), follow_symlinks=False)
             os.unlink(os.path.join(dst, 'testfile'))
             canhard = True
         except Exception as e:
diff --git a/tests/unit/test_oe_path.py b/tests/unit/test_oe_path.py
index 3abe1f0..181128b 100644
--- a/tests/unit/test_oe_path.py
+++ b/tests/unit/test_oe_path.py
@@ -1,8 +1,9 @@ 
 """
 Unit tests for wic/oe/path.py, covering wic's own path logic: join,
 is_path_parent, symlink, make_relative_symlink, canonicalize, which_wild,
-and realpath. Thin standard-library wrappers (relative -> os.path.relpath,
-find -> os.walk) are left to the standard library.
+realpath and copyhardlinktree. Thin standard-library wrappers
+(relative -> os.path.relpath, find -> os.walk) are left to the standard
+library.
 """
 import errno
 import os
@@ -23,6 +24,7 @@  from wic.oe.path import (
     canonicalize,
     which_wild,
     realpath,
+    copyhardlinktree,
 )
 
 
@@ -376,3 +378,26 @@  class TestRealpath:
 
         monkeypatch.setattr(os.path, "isdir", boom)
         assert realpath(str(sub), str(tmp_path), use_physdir=False) == str(sub)
+
+
+class TestCopyhardlinktree:
+    def test_hardlinks_files(self, tmp_path):
+        src = tmp_path / "src"
+        (src / "sub").mkdir(parents=True)
+        (src / "sub" / "file").write_text("data")
+        dst = tmp_path / "dst"
+        copyhardlinktree(str(src), str(dst))
+        assert os.path.samefile(src / "sub" / "file", dst / "sub" / "file")
+
+    def test_dangling_symlink_as_first_entry_still_hardlinks(self, tmp_path):
+        # The hardlink test uses the first file os.walk() returns. A rootfs
+        # symlink such as var/lock -> /run/lock dangles on the build host and
+        # must not make the test fail and force a full tar copy.
+        src = tmp_path / "src"
+        (src / "sub").mkdir(parents=True)
+        os.symlink("/nonexistent/target", str(src / "dangling"))
+        (src / "sub" / "file").write_text("data")
+        dst = tmp_path / "dst"
+        copyhardlinktree(str(src), str(dst))
+        assert os.path.samefile(src / "sub" / "file", dst / "sub" / "file")
+        assert os.readlink(dst / "dangling") == "/nonexistent/target"