Skip to contentSkip to conversation

utils/check-package: support catching broken symlinks

PATCH New Thomas Perale via buildroot 1 participant 2 messages Oct 11

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 utils/check-package: support catching broken symlinks

In a number of cases, such as [1] backporting a patch to an LTS would create a broken symlink.

Those symlinks are hard to spot as the symlink and its target live in completely different directories.

This commit introduces a make check-package check to verify that a symlink is not pointing to a non-existent location.

Since broken symlinks don't have a pattern in their filename the get_lib_from_filename is responsible for doing the check and assigning the proper lib: lib_symlink.BrokenSymlink.
Then BrokenSymlink doesn't perform any check and just returns the error message instead. Assigning the BrokenSymlink means that the file is broken and doesn't need any check.

Having such a lib makes it possible to ignore files that are rightfully pointing to nothing since they will be installed on the target to point to an actual directory.

To achieve this, the FileNotFoundError exception no longer creates a warning for symlink, since they are checked in the lib instead. Files listed in .checkpackageignore are also no longer reported as missing when they are broken symlinks.

[1] 176fed6cc0 package/gcc-bare-metal: fix symlink to gcc 14.3.0

---
Hi,

Not sure if it's possible but I think that through introspection lib that don't have a "check_line" method defined should instead not be opened.

This would avoid us to have to check that the file is a symlink again in the FileNotFoundError exception once again.
But this is pretty minimal change compared to change how the 'check_line' is called so for now I'm sending this implementation.

Regards.

---
 .checkpackageignore                  | 25 +++++++++++++++++++++++++
 utils/check-package                  | 15 ++++++++++++---
 utils/checkpackagelib/lib_ignore.py  |  2 +-
 utils/checkpackagelib/lib_symlink.py | 13 +++++++++++++
 4 files changed, 51 insertions(+), 4 deletions(-)
 create mode 100644 utils/checkpackagelib/lib_symlink.py
.checkpackageignore +25 -0
index edd5ddb3f2..6adbb9587b 100644
@@ -809,7 +809,26 @@ package/setserial/0001-build-system-fix.patch lib_patch.Upstream
 package/shairport-sync/S99shairport-sync Shellcheck lib_sysv.Indent lib_sysv.Variables
 package/shared-mime-info/0001-Remove-incorrect-dependency-from-install-data-hook.patch lib_patch.Upstream
 package/shellinabox/0001-Makefile-disable-always-building-statically.patch lib_patch.Upstream
+package/skeleton-init-openrc/skeleton/var/tmp lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/dev/log lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/etc/runlevels/default/sysv-rcs lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/var/cache lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/var/lib/misc lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/var/lock lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/var/log lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/var/run lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/var/spool lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/var/tmp lib_symlink.BrokenSymlink
+package/skeleton-init-openrc/skeleton/var/tmp lib_symlink.BrokenSymlink
 package/skeleton-init-systemd/fakeroot_tmpfiles.sh Shellcheck
+package/skeleton-init-sysv/skeleton/dev/log lib_symlink.BrokenSymlink
+package/skeleton-init-sysv/skeleton/var/cache lib_symlink.BrokenSymlink
+package/skeleton-init-sysv/skeleton/var/lib/misc lib_symlink.BrokenSymlink
+package/skeleton-init-sysv/skeleton/var/lock lib_symlink.BrokenSymlink
+package/skeleton-init-sysv/skeleton/var/log lib_symlink.BrokenSymlink
+package/skeleton-init-sysv/skeleton/var/run lib_symlink.BrokenSymlink
+package/skeleton-init-sysv/skeleton/var/spool lib_symlink.BrokenSymlink
+package/skeleton-init-sysv/skeleton/var/tmp lib_symlink.BrokenSymlink
 package/slang/0001-slsh-libs.patch lib_patch.Upstream
 package/smcroute/S41smcroute NotExecutable lib_sysv.Indent lib_sysv.Variables
 package/smstools3/0001-fix-Makefile.patch lib_patch.Upstream
@@ -993,3 +1012,9 @@ support/scripts/setlocalversion Shellcheck
 support/testing/tests/core/post-build.sh Shellcheck
 support/testing/tests/package/test_opkg/post-build.sh Shellcheck
 support/testing/tests/utils/test_get_developers/0001-package-binutils-change-.mk.patch lib_patch.NumberedSubject lib_patch.Upstream
+system/skeleton/dev/fd lib_symlink.BrokenSymlink
+system/skeleton/dev/stderr lib_symlink.BrokenSymlink
+system/skeleton/dev/stdin lib_symlink.BrokenSymlink
+system/skeleton/dev/stdout lib_symlink.BrokenSymlink
+system/skeleton/etc/mtab lib_symlink.BrokenSymlink
+system/skeleton/etc/resolv.conf lib_symlink.BrokenSymlink
utils/check-package +12 -3
index aa10280f4d..3c8c54ec5e 100755
@@ -26,6 +26,7 @@ import checkpackagelib.lib_patch
 import checkpackagelib.lib_python
 import checkpackagelib.lib_shellscript
 import checkpackagelib.lib_sysv
+import checkpackagelib.lib_symlink
 
 VERBOSE_LEVEL_TO_SHOW_IGNORED_FILES = 3
 flags = None  # Command line arguments.
@@ -170,6 +171,8 @@ def get_lib_from_filename(fname):
         if os.path.basename(fname) == "external.mk" and \
            os.path.exists(fname[:-2] + "desc"):
             return None
+    if os.path.islink(fname) and not os.path.exists(fname):
+        return checkpackagelib.lib_symlink
     if fname == ".checkpackageignore":
         return checkpackagelib.lib_ignore
     if CONFIG_IN_FILENAME.search(fname):
@@ -271,9 +274,15 @@ def check_file_using_lib(fname):
                     nwarnings += warn
                 lastline = text
     except FileNotFoundError:
-        print(f"{fname}: missing; unstaged file removal?")
-        nwarnings += 1
-        return nwarnings, nlines
+        # Since all the filename that get a lib assigned are always
+        # unconditionnally opened, a broken symlinks will trigger this
+        # exception.
+        # The proper warning case is already handled by BrokenSymlink which has
+        # the additional benefits to be working with '.checkpackageignore'
+        if not os.path.islink(fname):
+            print(f"{fname}: missing; unstaged file removal?")
+            nwarnings += 1
+            return nwarnings, nlines
 
     for name, cf in objects:
         if cf.disable.search(lastline):
utils/checkpackagelib/lib_ignore.py +1 -1
index f3698fc3b4..9615adf1db 100644
@@ -8,7 +8,7 @@ from checkpackagelib.base import _CheckFunction
 class IgnoreMissingFile(_CheckFunction):
     def check_line(self, lineno, text):
         fields = text.split()
-        if not os.path.exists(fields[0]):
+        if not os.path.lexists(fields[0]):
             return ["{}:{}: ignored file {} is missing"
                     .format(self.filename, lineno, fields[0]),
                     text]
utils/checkpackagelib/lib_symlink.py new file +13 -0
new file mode 100644
index 0000000000..237b049a21
@@ -0,0 +1,13 @@
+import os
+
+from checkpackagelib.base import _CheckFunction
+
+class BrokenSymlink(_CheckFunction):
+    def before(self):
+        # The BrokenSymlink class doesn't do any checks.
+        # The actual check is happening in
+        # 'utils/check-package:get_lib_from_filename'.
+        # This lib is only assigned if the symlink is broken while allowing
+        # filters from checkpackageignore
+        return ["{}:0: broken symlink to '{}'"
+           .format(self.filename, os.readlink(self.filename))]
Signature · 6 lines
2.55.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw

PATCH 2/2 utils/checkpackagelib: add test for broken symlink

Test that symlinks pointing to non-existent location trigger a warning from the lib_symlink.py lib.

Assisted-by LLM
---
 utils/checkpackagelib/test_lib_symlink.py | 36 +++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 utils/checkpackagelib/test_lib_symlink.py
utils/checkpackagelib/test_lib_symlink.py new file +36 -0
new file mode 100644
index 0000000000..cfa9fcd4ee
@@ -0,0 +1,36 @@
+import os
+import pytest
+import re
+import tempfile
+import checkpackagelib.lib_symlink as m
+
+workdir_regex = re.compile(r'/tmp/tmp[^/]*-checkpackagelib-test-symlink')
+
+
+def check_symlink(check_function, filename, target):
+    with tempfile.TemporaryDirectory(suffix='-checkpackagelib-test-symlink') as workdir:
+        link = os.path.join(workdir, filename)
+        os.symlink(target, link)
+        obj = check_function(link, 'url')
+        result = obj.before()
+        if result is None:
+            return []
+        return [workdir_regex.sub('dir', r) for r in result]
+
+
+BrokenSymlink = [
+    ('relative target',
+     'log',
+     '../tmp/log',
+     ["dir/log:0: broken symlink to '../tmp/log'"]),
+    ('absolute target',
+     'mtab',
+     '/proc/self/mounts',
+     ["dir/mtab:0: broken symlink to '/proc/self/mounts'"]),
+    ]
+
+
[email protected]('testname,filename,target,expected', BrokenSymlink)
+def test_BrokenSymlink(testname, filename, target, expected):
+    warnings = check_symlink(m.BrokenSymlink, filename, target)
+    assert warnings == expected
Signature · 6 lines
2.55.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot