Patches for Yocto layers and components that do not have their own list
 help / color / mirror / Atom feed
From: Trevor Woerner <twoerner@gmail.com>
To: yocto-patches@lists.yoctoproject.org
Subject: [wic][PATCH 2/4] oe/path: don't glob-expand the destination in symlink(force=True)
Date: Thu,  9 Jul 2026 16:52:25 -0400	[thread overview]
Message-ID: <20260709205227.3470712-3-twoerner@gmail.com> (raw)
In-Reply-To: <20260709205227.3470712-1-twoerner@gmail.com>

symlink(source, destination, force=True) cleared an existing
destination by calling remove(destination). remove() is a rm -rf
helper that runs its argument through glob.glob() before unlinking, so
it treats the destination as a glob pattern rather than a literal path.

For ordinary names this is merely wasteful, but a destination that
contains glob metacharacters is actively dangerous. A name with a '['
may fail to match itself and silently leave the old link in place, and
a pattern that happens to match other entries could unlink files the
caller never named. remove() even warns about exactly this in its own
docstring.

Remove the literal destination directly instead: unlink it, fall back
to shutil.rmtree() when it turns out to be a directory (EISDIR), and
treat a missing destination (ENOENT) as success. This mirrors what
remove() does per matched name, minus the glob expansion.

AI-Generated: codex/claude-opus 4.8 (xhigh)
Signed-off-by: Trevor Woerner <twoerner@gmail.com>
---
 src/wic/oe/path.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/wic/oe/path.py b/src/wic/oe/path.py
index 862edc532ade..13c52b363694 100644
--- a/src/wic/oe/path.py
+++ b/src/wic/oe/path.py
@@ -168,7 +168,18 @@ def symlink(source, destination, force=False):
     """Create a symbolic link"""
     try:
         if force:
-            remove(destination)
+            # Remove the exact destination path. Do not route this through
+            # remove(), which treats its argument as a glob pattern: a
+            # destination containing glob metacharacters (for example a
+            # '[' in the name) could fail to match, or match and delete
+            # unrelated files.
+            try:
+                os.unlink(destination)
+            except OSError as exc:
+                if exc.errno == errno.EISDIR:
+                    shutil.rmtree(destination)
+                elif exc.errno != errno.ENOENT:
+                    raise
         os.symlink(source, destination)
     except OSError as e:
         if e.errno != errno.EEXIST or os.readlink(destination) != source:
-- 
2.50.0.173.g8b6f19ccfc3a



  parent reply	other threads:[~2026-07-09 20:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-09 20:52 [wic][PATCH 0/4] oe/path: three fixes plus unit coverage Trevor Woerner
2026-07-09 20:52 ` [wic][PATCH 1/4] oe/path: fix bare `false` NameError in __realpath's isdir guard Trevor Woerner
2026-07-09 20:52 ` Trevor Woerner [this message]
2026-07-09 20:52 ` [wic][PATCH 3/4] oe/path: canonicalize('') should return '' rather than the cwd Trevor Woerner
2026-07-09 20:52 ` [wic][PATCH 4/4] tests/unit/test_oe_path: cover oe/path's own path logic Trevor Woerner
2026-07-15 18:01 ` [yocto-patches] [wic][PATCH 0/4] oe/path: three fixes plus unit coverage Paul Barker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260709205227.3470712-3-twoerner@gmail.com \
    --to=twoerner@gmail.com \
    --cc=yocto-patches@lists.yoctoproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox