Skip to content

Commit ee89c31

Browse files
jazellyjakecastelli
authored andcommitted
fs: respect dereference when copy symlink directory
Co-authored-by: Jake Yuesong Li <[email protected]> PR-URL: #54732 Fixes: #54730 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5e081a1 commit ee89c31

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/node_file.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3060,7 +3060,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
30603060

30613061
bool dest_exists = !error_code && dest_status.type() !=
30623062
std::filesystem::file_type::not_found;
3063-
bool src_is_dir = src_status.type() == std::filesystem::file_type::directory;
3063+
bool src_is_dir =
3064+
(src_status.type() == std::filesystem::file_type::directory) ||
3065+
(dereference && src_status.type() == std::filesystem::file_type::symlink);
30643066

30653067
if (!error_code) {
30663068
// Check if src and dest are identical.

test/parallel/test-fs-cp.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ function nextdir() {
119119
}
120120

121121

122+
// It overrides target directory with what symlink points to, when dereference is true.
123+
{
124+
const src = nextdir();
125+
const symlink = nextdir();
126+
const dest = nextdir();
127+
mkdirSync(src, mustNotMutateObjectDeep({ recursive: true }));
128+
writeFileSync(join(src, 'foo.js'), 'foo', 'utf8');
129+
symlinkSync(src, symlink);
130+
131+
mkdirSync(dest, mustNotMutateObjectDeep({ recursive: true }));
132+
133+
cpSync(symlink, dest, mustNotMutateObjectDeep({ dereference: true, recursive: true }));
134+
const destStat = lstatSync(dest);
135+
assert(!destStat.isSymbolicLink());
136+
assertDirEquivalent(src, dest);
137+
}
138+
122139
// It throws error when verbatimSymlinks is not a boolean.
123140
{
124141
const src = './test/fixtures/copy/kitchen-sink';

0 commit comments

Comments
 (0)