Skip to content

Commit

Permalink
fs: respect dereference when copy symlink directory
Browse files Browse the repository at this point in the history
Co-authored-by: Jake Yuesong Li <[email protected]>
  • Loading branch information
jazelly and jakecastelli committed Sep 3, 2024
1 parent 298dea0 commit d3cd38b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3175,7 +3175,8 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
return THROW_ERR_FS_CP_DIR_TO_NON_DIR(env, message.c_str());
}

if (!src_is_dir && dest_is_dir) {
if (src_status.type() != std::filesystem::file_type::symlink
&& !src_is_dir && dest_is_dir) {
std::string message = "Cannot overwrite directory " + dest_path.string() +
" with non-directory " + src_path.string();
return THROW_ERR_FS_CP_NON_DIR_TO_DIR(env, message.c_str());
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-fs-cp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ function nextdir() {
}


// It overrides target directory with what symlink points to, when dereference is true.
{
const src = nextdir();
const symlink = nextdir();
const dest = nextdir();
mkdirSync(src, mustNotMutateObjectDeep({ recursive: true }));
writeFileSync(join(src, 'foo.js'), 'foo', 'utf8');
symlinkSync(src, symlink);

mkdirSync(dest, mustNotMutateObjectDeep({ recursive: true }));

cpSync(symlink, dest, mustNotMutateObjectDeep({ dereference: true, recursive: true }));
const destStat = lstatSync(dest);
assert(!destStat.isSymbolicLink());
assertDirEquivalent(src, dest);
}

// It throws error when verbatimSymlinks is not a boolean.
{
const src = './test/fixtures/copy/kitchen-sink';
Expand Down

0 comments on commit d3cd38b

Please sign in to comment.