Skip to content

fix(mv): refuse directory onto non-directory without backup (Refs #1649) - #1661

Closed
caomengxuan666 wants to merge 2 commits into
mainfrom
fix/1649-mv-dir-over-nondir
Closed

caomengxuan666 wants to merge 2 commits into
mainfrom
fix/1649-mv-dir-over-nondir

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

mv dir a.txt (dest is a regular file): GNU refuses with mv: cannot overwrite non-directory 'a.txt' with directory 'dir', rc=1. WinuxCmd 1.2.1 returned rc=0 and replaced the file with the directory (data loss).

Fix: type-mismatch overwrite guard in move_single_path (src/commands/mv.cpp, +34), placed after the backup step, before the rename — GNU copy.c copy() arms: dir→non-dir copy.c:2448-2463, mirror copy.c:2489-2500. Skipped when a backup was just made (--backup allowed, dest moved aside); --backup=none still refuses. Symlinks are followed via GetFileAttributesW, like GNU's stat.

Verification

  • GNU coreutils 9.4 oracle matrix (local, LC_ALL=C): 8/8 shapes as expected — refuse / --backup allows / --backup=none refuses / -T mirror refuses / move-into-dir locks pass / symlink-to-file refuses naming the link.
  • New pins: 7 cells in tests/unit/mv/mv_unit_test.cpp (issue shape, --backup allow, --backup=none refuse, -T mirror, two move-into locks, symlink-to-file).
  • git diff --check clean. i18n extract on the head tree: 3619 messages, zero containing cannot overwrite — the new messages are concatenations, invisible to the extractor, so no drift and no i18n-repo sync is triggered.
  • Not run locally (Windows-only binary; this host cannot build/run WinuxCmd): compile + 7 new cells on head + fail-first on base. Review round must run these on X299/CI.

Refs #1649

GNU copy.c copy(): moving a directory onto an existing non-directory
is refused with 'cannot overwrite non-directory %s with directory %s'
(copy.c:2448-2463) unless backups are in effect; the mirror arm is
copy.c:2489-2500. MoveFileExW would otherwise silently replace the
file (data loss, rc=0). Gate runs after the backup step and is skipped
when a backup was made; --backup=none still refuses.

Refs #1649
@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review: FAIL — one finding (rework)

Reviewed head 89cd293d (base 8be156e3). No merge until the finding below is fixed and re-verified.

F2 — symlink-to-dir src onto a file is falsely refused (regression)

mv lnkdir a.txt where lnkdir -> realdir:

  • GNU coreutils 9.4: rc=0, the link itself is renamed onto the file (a.txt -> realdir).
  • Base 35f48641 (X299): rc=0, link renamed — GNU-matching.
  • Head 89cd293d (X299): rc=1, mv: cannot overwrite non-directory 'a.txt' with directory 'lnkdir', both operands untouched.

The new gate (mv.cpp:900–918) reads the src type via follow-symlinks GetFileAttributesW, sees a directory through the link, and over-fires. The PR breaks a previously-working shape in the exact face it fixes.

Rework: the gate's src-type query needs lstat-equivalent semantics (e.g. FILE_FLAG_OPEN_REPARSE_POINT / FindFirstFileW, as the same-file guard in the same function already does): a src counts as a directory only when DIRECTORY is set without REPARSE_POINT. Add 1 pin cell: symlink-to-dir src onto file (rc=0, link renamed). Re-verify on the reworked binary (head cells incl. the new pin + F2 live probe).

Verified green (banked)

  • Pins 7/7 on head; fail-first confirmed by live-probing the exact 7 cell shapes against both built binaries: cells 1/3/4/7 fail on base (rc=0 replace/move-in), cells 2/5/6 pass on base (locks); all 7 pass on head. Oracle matrix 8/8 vs GNU 9.4. CI ctest: tests #2035–2041 all Passed, full suite 3884/3884. X299 gtest: 7 mv.*issue1649* tests passed.
  • Mechanism: gate placement/order matches GNU in effect; --backup carve-out works (made_backup ⟺ backup actually made); messages byte-identical to GNU (copy.c:2460, copy.c:2496); the unreachable second GNU gate (copy.c:2510) correctly not implemented.
  • Intersection vs live tip 8be156e3: ∅ (interval touches only xargs.rs, not the PR's files).
  • i18n: extractor on head tree 3619 messages, zero containing cannot overwrite; head/base catalogs identical — nothing to sync.
  • Hygiene: git diff --check 35f48641..89cd293d clean; closing scan Refs-only (Refs #1649).

CI — one red, not a product verdict

Runs 43577219204 (GNU differential) success; 43577219310 (CMake Windows) failure with only rust2-gate red; all other jobs green (rust-tests, 4× build-windows, i18n-drift, rust2-surface-check). I pulled the failed log myself: GNU-gated cases: pass=0 fail=81, all 81 cases g_rc=MISSING — the WSL/GNU side produced no output at all. Identical red on unrelated PR #1651's run 43577222551 in the same 09:22 window. This is a shared CI infrastructure failure (gate/WSL side), disjoint from this PR (C++ mv + its test vs gate's csplit/pathchk/ptx) — not a rework item, flagged here for whoever owns the gate.

Merge notes

@caomengxuan666

Copy link
Copy Markdown
Member Author

Superseded: #1649 fix merged via #1700.

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.

1 participant