Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: respect dereference when copy symlink directory #54732

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3155,7 +3155,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {

bool dest_exists = !error_code && dest_status.type() !=
std::filesystem::file_type::not_found;
bool src_is_dir = src_status.type() == std::filesystem::file_type::directory;
bool src_is_dir =
(src_status.type() == std::filesystem::file_type::directory) ||
(dereference && src_status.type() == std::filesystem::file_type::symlink);

if (!error_code) {
// Check if src and dest are identical.
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
Loading