fs: fix repeated copy of directory with symlinks - #65411
Open
haramj wants to merge 1 commit into
Open
Conversation
Repeatedly copying a directory that contains a symlink to an unrelated directory fails on the second copy with ERR_FS_CP_EINVAL because the symlink target is mistaken for a self-referential copy. Compare symlink targets using canonicalized paths, treating identical targets as self-copies only when the target is the destination root or one of its ancestors. Fixes: nodejs#65097 Signed-off-by: haramjeong <04harams77@gmail.com>
haramj
force-pushed
the
fix/65097-cp-repeated-symlink-copy
branch
from
August 20, 2026 01:48
f3190d6 to
b91d82d
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65411 +/- ##
==========================================
- Coverage 90.13% 90.12% -0.02%
==========================================
Files 752 752
Lines 251864 252233 +369
Branches 47354 47452 +98
==========================================
+ Hits 227021 227313 +292
- Misses 16180 16216 +36
- Partials 8663 8704 +41
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes: #65097
Repeatedly copying a directory that contains a symlink to an unrelated
directory fails on the second copy with
ERR_FS_CP_EINVALbecause thesymlink target is mistaken for a self-referential copy.
Root cause
Both the JS (
isSrcSubdirinlib/internal/fs/cp/cp.js) and the C++(
isInsideDirinsrc/node_file.cc) guards are prefix-inclusive, so whenthe source symlink and the already-copied destination symlink resolve to
the same directory, the "copy to a subdirectory of self" check fires on
the identical target.
Fix
the destination root or one of its ancestors; identical targets that are
unrelated to the destination root are allowed to be copied again.
weakly_canonical) in C++so relative-vs-absolute representations and macOS
/varvs/private/varare handled correctly.the internal options object.
Unlike the earlier attempt in #65099, the existing self-referential
protections are kept: a symlink pointing to the destination root or an
ancestor of it still fails with
ERR_FS_CP_EINVAL.Tests
New regression tests (
test-fs-cp-sync-symlink-repeated-copyandtest-fs-cp-async-symlink-repeated-copy) cover both the C++ (no filter)and JS (filter) paths with absolute and relative targets, each copied
twice, and verify the copied symlink still resolves to the original
target. Existing
*-points-to-desttests are unchanged and keep passing.Validation
makebuilds successfully.test/parallel/test-fs-*.{mjs,js}(351 tests) pass, including thenew regression tests and all
*-points-to-desttests.git-clang-formatreports no formatting changes.