Skip to content

fix(ln,cp): normalize symlink link text to backslash separators - #1102

Merged
caomengxuan666 merged 2 commits into
mainfrom
fix/ln-symlink-target-separators
Sep 16, 2026
Merged

caomengxuan666 merged 2 commits into
mainfrom
fix/ln-symlink-target-separators

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1101. Also addresses the ln -s breakage reported in unixwin/niubash#109.

Root cause

NT resolves reparse-point link text with the Win32 path parser, which only accepts backslash separators. A symlink created with a forward-slash target, e.g.

ln -s ./bds ./q

stores the literal string ./bds in the reparse point. The reparse point is created successfully (rc=0), but native resolution of q fails with ERROR_INVALID_NAME (123) — Explorer reports "The filename, directory name, or volume label syntax is incorrect" — even though MSYS/POSIX-style readers that resolve link text themselves work fine.

Verified matrix on this machine (before fix, native CreateFileW resolution of the link):

stored target native resolution
bds OK
.\bds OK
t1\f OK
..\<path> OK
./bds FAIL 123
.\? no — t1/f FAIL 123
t1/./f FAIL 123
../ln-test/bds FAIL 123

i.e. any forward slash in the stored target breaks native resolution; dot segments with backslashes are fine.

Fix

  • utils:win32 gains win32_normalize_symlink_target() which translates / → \ in the link text.
  • ln -s, cp -s and cp --dereference symlink recreation normalize the target before CreateSymbolicLinkW. The user-visible link text (verbose output, messages) is unchanged.
  • mv fallback and wpm were inspected and already store canonical backslash targets.

Verification

  • New regression tests: ln.ln_symlink_dot_slash_target_resolves_natively, ln.ln_symlink_forward_slash_path_target_resolves_natively, cp.cp_symbolic_forward_slash_source_resolves_natively — all assert the stored target contains no / and that CreateFileW on the plain path resolves.
  • Release build via scripts/build-with-vs.ps1 (Release, -DENABLE_TESTS_USER_VALUE=ON): ln.* + new cp test → 16 passed, 0 failed.
  • Manual end-to-end: ln -s ./bds q now stores .\bds and resolves natively (was FAIL 123 before).
  • No new user-visible strings; i18n extract/validate against winuxcmd-i18n zh-CN is clean (3559/3559 keys, no drift).

NT resolves reparse-point link targets with the Win32 path parser, which
only accepts backslash separators. A target stored with forward slashes
(e.g. 'ln -s ./bds q' stored './bds') fails native resolution with
ERROR_INVALID_NAME - Explorer and other Win32 callers report 'The
filename, directory name, or volume label syntax is incorrect' - even
though the reparse point itself is created successfully.

Translate forward slashes to backslashes in the link text handed to
CreateSymbolicLinkW in ln -s, cp -s and cp --dereference symlink
recreation. mv/wpm paths already store canonical backslash targets.

Regression tests assert the stored target contains no '/' and that the
link resolves through CreateFileW on the plain path.
The previous push was based on a stale local checkout; it clobbered
win32_account_matches added to this module on main (used by chgrp,
chown and find), breaking the CI unity build. Restore the current
main version and re-apply win32_normalize_symlink_target on top.
@caomengxuan666
caomengxuan666 merged commit d39a14d into main Sep 16, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/ln-symlink-target-separators branch September 21, 2026 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ln -s: "./"-prefixed relative targets stored verbatim are unresolvable by native Windows consumers (+ false-negative ENOENT on relative targets)

1 participant