Skip to content

fix(mkdir,ln): fold POSIX drive-dialect operands before any write (#1233, #1234) - #1243

Merged
caomengxuan666 merged 4 commits into
mainfrom
fix/1233-mkdir-ln-drive-dialect
Oct 19, 2026
Merged

caomengxuan666 merged 4 commits into
mainfrom
fix/1233-mkdir-ln-drive-dialect

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #1233. Closes #1234. Both found by the #1230 all-command sweep.

What

mkdir and ln accepted POSIX drive-dialect operands (/e/Data, /cygdrive/e/Data, /mnt/e/Data) and handed the raw text to Win32. A leading /e resolves against the CURRENT drive, so every resolution drifted with cwd:

How

Per the ticket direction: operand entry folds once through the shared normalize_api_operand boundary - no error-report-layer patches.

  • mkdir.cpp: create_directory folds the operand on entry (creation, -p recursion, -m, existence/verbose helpers consume the folded form); GNU-style messages keep printing what the user typed.
  • ln.cpp: sources and targets fold once per job and in the -t / 2-operand / implicit-target-directory directory-detection probes; -r computes the relative form from the folded pair; create-stage error text quotes the operand spelling as typed (display strings ride alongside the API forms - found during probe round 1, where the folded form leaked into failed to create hard link 'E:\Data\hard-w2' while MSYS prints the operand spelling).
  • Messages and symlink/hardlink stored text semantics unchanged otherwise.

Verification

Unit: full ctest 2993/2993 passed (local, Debug). New regressions synthesize the dialect spelling from the TempDir's own drive, so they hold on any host: land-on-operand-drive, cwd-on-other-drive shadow negatives, bare-drive-root -p no-op (the mkdir -p /e -> C:\e case), verbose operand form, -t/dir-target join, symlink landing (privilege-skip honored), message-spelling pin.

Live write-side probes (PowerShell native argv only; MSYS oracle = D:/Git/usr/bin/{mkdir,ln}.exe; landing witnessed by Test-Path on E: and shadow absence on C:/D:):

probe cwd=C:\ cwd=D:\repo MSYS oracle
mkdir -p /e/Data/probe-w2 exit 0, landed E:\Data\probe-w2, no C:\e exit 0, landed E:\Data\probe-w2, no D:\e exit 0, creates on E:
mkdir /e/Data/probe-w2 (after -p) File exists, exit 1, no shadow same File exists, exit 1
mkdir /e/Data (existing, ticket repro) File exists (was ENOENT), no shadow same File exists
ln -s /e/Data /e/Data/link-w2 privilege-blocked; error text identical to oracle (Operation not permitted); no C:\e\Data\link-w2 same same error (host lacks the privilege)
ln /e/Data/probe-w2 /e/Data/hard-w2 (dir) fails at create stage (source resolved on E:), raw operand spelling in message, no link, no shadow same hard link not allowed for directory
ln /e/Data/probe-w2/marker.txt /e/Data/hard-w2 (positive control) exit 0, landed E:\Data\hard-w2, no C:\e\Data\hard-w2 same exit 0

Notes: the symlink landing row is privilege-bound on this host for both our build and the oracle (identical error text = parity); the positive landing witness for the same per-job fold machinery is the hard-link row, plus the unit test ln.ln_symlink_drive_dialect_lands_on_operand_drive on privileged hosts. The dir-hardlink message text (Permission denied vs GNU's hard link not allowed for directory) is pre-existing Win32 error mapping, out of scope here.

Cleanup: every probe product deleted after each round; post-cleanup Test-Path = False for E:\Data, E:\Data\probe-w2, E:\Data\link-w2, E:\Data\hard-w2, C:\e, C:\d, C:\e\Data, D:\e, D:\e\Data - zero residue (probe baseline also started clean).

Provenance

Branch continues the rescued WIP of the previous agent (worktree winuxcmd-wt-1233, diff preserved at worktree-rescue-20261019b/winuxcmd-wt-1233.diff). Inherited as-is after line-by-line review (covers all write/probe call sites; matches both tickets' stated direction), with two corrections added on top: the create-stage message-plumbing fix above, and its regression pin.

)

mkdir handed the raw operand ('/e/Data', '/cygdrive/e/Data', '/mnt/e/Data')
to CreateDirectoryW / create_directories, and Win32 resolves a leading '/e'
against the CURRENT drive: from a C: cwd 'mkdir -p /e/Data' really created
a shadow C:\e\Data tree - the only audited misparse that writes to the
wrong place (#1230 sweep). An existing E:\Data also reported ENOENT
instead of 'File exists', and -m/-v answered for the shadow path.

The operand now folds once through the shared normalize_api_operand
boundary on entry to create_directory; the -m and existence helpers fold
likewise. GNU-style messages keep printing what the user typed, like MSYS.

Regressions synthesize the dialect spelling from the TempDir's own drive
(land-on-operand-drive, cwd-on-other-drive shadow negatives, bare-drive
root no-op, verbose operand form) and pass on any host.
ln resolved '/e/...' sources and targets against the cwd drive:
std::filesystem::absolute spliced a rooted no-drive operand onto the cwd
(msvc semantics), and GetFileAttributesW / FindFirstFileW /
CreateHardLinkW / CreateSymbolicLinkW all answered for '<cwd>:\e\...'.
From a C: cwd the hard-link source probe failed with "failed to access",
-t DIR rejected a real directory, and a privileged 'ln -s /e/Data
/e/Data/link' wrote the link beside the cwd instead of on E:.

Sources and targets now fold once per job (and in the -t / 2-operand /
implicit-target-directory probes) through the shared normalize_api_operand
boundary before any Win32 call; -r computes the relative form from the
folded pair. Create-stage error text quotes the operand spelling as typed
(display strings ride alongside the API forms), matching MSYS.

Regressions synthesize the dialect spelling from the TempDir's own drive
(hard-link landing + cwd-on-other-drive shadow negative, directory-target
join, -t directory, symlink landing, message-spelling pin) and pass on
any host; the symlink case skips on hosts without the privilege.
@caomengxuan666

Copy link
Copy Markdown
Member Author

Independent review: APPROVE-WITH-NITS

Reviewer: independent CR agent for #1243. All checks below were re-executed from scratch (own worktree at d640dbb, own MSVC builds of both base e991ba8 and PR head), not taken from the PR description.

Verified points

  1. Fold completeness (core audit). Walked every Win32/filesystem write+probe call site in both files against the diff:
    • mkdir.cpp: create_directories_w (via folded wpath), CreateDirectoryW (folded), Get/SetFileAttributesW in the -m helper (folds internally), directory_exists_utf8 / path_exists_not_directory (via make_api_path_operand). No uncovered call site found; no residual write gap.
    • ln.cpp: FindFirstFileW source probe (source_api), all four GetFileAttributesW probes (-t, 2-operand, implicit-target-dir, per-job target), symlink_status (operand.normalized), std::filesystem::canonical for --logical (source_api), relative_symlink_target (folded pair), backup MoveFileW/DeleteFileW/RemoveDirectoryW and remove_existing (target_api), CreateHardLinkW/CreateSymbolicLinkW (folded API forms, raw spelling carried alongside for messages). The WSL/cygdrive/MSYS spellings all share the same normalize_api_operand_w fold.
  2. Pre-fix reproduction (base e991ba8, built from scratch). PS-native argv: mkdir -p /e/probe-cr1243/pre from C:\ cwd → exit 0 and shadow C:\e\probe-cr1243\pre created, nothing on E:; same from D:\ → D:\e\... shadow; clean-machine mkdir /e/probe-cr1243/deep/leaf (no -p, E: parent exists) → No such file or directory (the ENOENT variant the CHANGELOG describes); ln/ln -t/2-op-dir via dialect → failed to access '/e/...'. Ticket root cause confirmed exactly as described.
  3. Post-fix matrix (PR head, PS-native argv, own probe area). 15+ rows: mkdir -p /e/... from C: and D: cwds lands on E: every time; /cygdrive/e/... and /mnt/e/... fold too; mkdir -p /e silent no-op; no--p re-run → File exists quoting the operand spelling (MSYS-oracle parity modulo program prefix/quote style); -m 555 sets ReadOnly on the E: target; -pv reports only created components in operand spelling; ln hard link exit 0 lands E: from C: and D: cwds; dir-join and -t rows land inside the directory; -f/-fv/-b behave; ln -s is privilege-blocked on this host (identical to oracle) and its landing is covered by the unit test on privileged hosts. Shadow witnesses C:\e, C:\d, D:\e, C:\cygdrive, C:\mnt were False before, during and after every row.
  4. Display/fold separation. create_hardlink/create_symlink take folded API forms + raw display params; their only call site passes the raw job operands. Interactive prompt, backup/cannot-backup, "File exists", "failed to access" and verbose output all print the spelling as typed. mkdir error paths and -v likewise keep the raw operand.
  5. New tests. All 11 new regression tests pass locally. ln.ln_symlink_drive_dialect_lands_on_operand_drive correctly skips on this non-privileged host (keyed on the Operation not permitted mapping, so a dialect misparse would still fail); bare-drive-root and message-spelling pins verified directly.
  6. Full ctest: 2993/2993 passed, 0 failed (local, Release + tests; matches the PR's Debug claim).
  7. CI: x64 + arm64 builds, oracle, compare — all SUCCESS on the head commit.
  8. realpath/readlink -f 裸盘符根 /x 形态静默拼接 cwd——canonicalize_path_w 共享入口缺盘符折叠 #1231 boundary relationship. This PR folds at command entry via normalize_api_operand (returns native form); realpath/readlink -f 裸盘符根 /x 形态静默拼接 cwd——canonicalize_path_w 共享入口缺盘符折叠 #1231's canonicalize_path_w is a separate shared layer with its own shell-tree mapping. No file overlap between the two branches (empty comm of the diff file sets); mkdir/ln never call canonicalize_path_w; normalize_api_operand_w is idempotent on already-native forms. No double-fold or conflict; the two can land in either order.
  9. Provenance claim verified. Applied worktree-rescue-20261019b/winuxcmd-wt-1233.diff (exactly the 4 ticket files) onto clean e991ba8: mkdir.cpp and mkdir_unit_test.cpp are byte-identical to the PR; ln.cpp differs only by the display-parameter plumbing (the claimed create-stage leak fix, WIP passed folded forms into messages); ln_unit_test.cpp adds only the utf8 helper + ln_drive_dialect_create_failure_message_keeps_operand_spelling. Claim accurate.
  10. Commit hygiene. 3 commits, each file set matches its message (mkdir+test / ln+test / CHANGELOG); no stray files, no process-killing or out-of-scope edits; no new i18n literals (catalog sync correctly skipped). CHANGELOG entry matches observed behavior, including the ENOENT variant reproduced on base.

Non-blocking notes

  • nit (message fidelity, the one real finding): remove_existing's failure message now prints the folded form — ln -fT /e/x /e/nonempty → failed to remove 'E:\probe-cr1243\nonempty': The directory is not empty. Pre-PR this path quoted the raw spelling, so the PR's "messages stay verbatim" invariant has a small residual hole on the -f removal-failure path (the create-stage leak got the display-param treatment; this sibling didn't). Error path only, unambiguous, cosmetic — happy to see it folded into a follow-up.
  • nit (pre-existing, now reachable): -t/implicit-dir join messages mix separators ('/e/probe-cr1243\probe.txt'), and the backup verbose line mixes dialects ('/e/x' -> 'E:\x~'). Both come from the pre-existing join_target_path/backup_name construction, not this PR's fold; cosmetic only.
  • The dir-hardlink error text (Permission denied vs GNU's hard link not allowed for directory) remains pre-existing Win32 error mapping, as the PR itself notes — out of scope.

BLOCKERS: none. Merge as-is or with nit (a) addressed at the author's discretion.

…nit)

The force-removal helper received the folded target and embedded it in
'failed to remove ...', so 'ln -fT /e/x /e/nonempty' printed the
'E:\...' API form where every other message prints what the user typed -
a violation of the verbatim-message invariant this branch declares.
remove_existing now takes a display string alongside the folded API path,
same plumbing as create_hardlink/create_symlink.

Regression: ln_fT_drive_dialect_remove_failure_message_keeps_operand_
spelling forces the deterministic non-empty-directory refusal through
dialect operands and pins the raw spelling. Live witness (PS native argv,
cwd=C:\): 'ln -fT /e/Data/src-w2.txt /e/Data/nonempty-w2' -> "failed to
remove '/e/Data/nonempty-w2'", no shadow, zero residue after cleanup.
@caomengxuan666

Copy link
Copy Markdown
Member Author

NIT addressed on this branch (head 23f9957), same display-plumbing pattern as create_hardlink/create_symlink:

  • remove_existing now takes (path_api, path_display); the force branch passes the raw operand for the message. ln -fT /e/Data/src-w2.txt /e/Data/nonempty-w2 from a C: cwd now prints failed to remove '/e/Data/nonempty-w2': The directory is not empty. — operand spelling, no folded E:\... leak, no shadow, zero residue after cleanup (PS native argv witness).
  • New regression pins it: ln.ln_fT_drive_dialect_remove_failure_message_keeps_operand_spelling (deterministic non-empty-directory refusal via dialect operands).
  • CHANGELOG wording updated: "create/removal error text quotes the operand spelling as typed".
  • Full local ctest on the new head: 2994/2994 passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant