diff mbox series

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

Message ID 20261203-fix-copyhardlinktree-dangling-symlink-v1-1-46c4802937ae@wabtec.com
State Under Review
Headers show
Series oe/path: don't follow symlinks in copyhardlinktree()'s hardlink test | expand

Commit Message

Florian Leferenz via B4 Relay Dec. 3, 2026, 10:18 a.m. UTC
From: Florian Leferenz <florian.leferenz@wabtec.com>

copyhardlinktree() hardlinks the first file os.walk() returns to decide
between hardlinking and a full tar copy. os.link() follows symlinks by
default, so if that entry is a dangling symlink (e.g. var/lock ->
/run/lock in a rootfs) the test fails with ENOENT and the whole tree is
copied with tar. Which entry comes first depends on the directory order
of the host filesystem, so the result differs between build hosts. Link
the entry itself instead and add an oe-selftest case for it.

The standalone wic carries a copy of this code and gets the same fix.

AI-Generated: Uses GitHub Copilot
Signed-off-by: Florian Leferenz <florian.leferenz@wabtec.com>
---
oe/path: fix copyhardlinktree() hardlink test with dangling symlinks
---
 meta/lib/oe/path.py                   |  2 +-
 meta/lib/oeqa/selftest/cases/liboe.py | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)


---
base-commit: 73216413e23c280ce9accd5e740134b4084c2851
change-id: 20261203-fix-copyhardlinktree-dangling-symlink-3bb2fea1ee89

Best regards,
diff mbox series

Patch

diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py
index 5122fcb08e..69199c87da 100644
--- a/meta/lib/oe/path.py
+++ b/meta/lib/oe/path.py
@@ -156,7 +156,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/meta/lib/oeqa/selftest/cases/liboe.py b/meta/lib/oeqa/selftest/cases/liboe.py
index 96969da161..3930a31c6d 100644
--- a/meta/lib/oeqa/selftest/cases/liboe.py
+++ b/meta/lib/oeqa/selftest/cases/liboe.py
@@ -106,6 +106,28 @@  class CopyTreeTests(OESelftestTestCase):
 
         oe.path.remove(testloc)
 
+    def test_copy_hardlink_tree_dangling_symlink(self):
+        """
+        Summary:    oe.path.copyhardlinktree() should still hardlink when the
+                    first entry os.walk() returns is a dangling symlink
+        Expected:   the regular file in dst is a hardlink of the one in src
+        Product:    OE-Core
+        """
+        with tempfile.TemporaryDirectory(prefix="oeqa-copyhardlinktree-") as testloc:
+            src = oe.path.join(testloc, 'src')
+            dst = oe.path.join(testloc, 'dst')
+            bb.utils.mkdirhier(oe.path.join(src, 'sub'))
+            # Like var/lock -> /run/lock in a target rootfs
+            os.symlink('/nonexistent/target', oe.path.join(src, 'dangling'))
+            open(oe.path.join(src, 'sub', 'file'), 'w+b').close()
+
+            oe.path.copyhardlinktree(src, dst)
+
+            self.assertTrue(os.path.samefile(oe.path.join(src, 'sub', 'file'),
+                                             oe.path.join(dst, 'sub', 'file')),
+                            "File in dst was copied instead of hardlinked")
+            self.assertEqual(os.readlink(oe.path.join(dst, 'dangling')), '/nonexistent/target')
+
 class SubprocessTests(OESelftestTestCase):
 
     def test_subprocess_tweak(self):