Skip to content
Closed
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
5 changes: 1 addition & 4 deletions benchmark/fs/bench-cpSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ const tmpdir = require('../../test/common/tmpdir');
const bench = common.createBenchmark(main, {
n: [1, 100, 10_000],
dereference: ['true', 'false'],
// When `force` is `true` the `cpSync` function is called twice the second
// time however an `ERR_FS_CP_EINVAL` is thrown, so skip `true` for the time being
// TODO: allow `force` to also be `true` once https://github.com/nodejs/node/issues/58468 is addressed
force: ['false'],
force: ['true', 'false'],
});

function prepareTestDirectory() {
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/fs/cp/cp-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ function onLink(destStat, src, dest, verbatimSymlinks) {
if (!isAbsolute(resolvedDest)) {
resolvedDest = resolve(dirname(dest), resolvedDest);
}
if (isSrcSubdir(resolvedSrc, resolvedDest)) {
const srcIsDir = fsBinding.internalModuleStat(src) === 1;

if (srcIsDir && isSrcSubdir(resolvedSrc, resolvedDest)) {
throw new ERR_FS_CP_EINVAL({
message: `cannot copy ${resolvedSrc} to a subdirectory of self ` +
`${resolvedDest}`,
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/fs/cp/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const {
resolve,
sep,
} = require('path');
const fsBinding = internalBinding('fs');

async function cpFn(src, dest, opts) {
// Warn about using preserveTimestamps on 32-bit node
Expand Down Expand Up @@ -344,7 +345,10 @@ async function onLink(destStat, src, dest, opts) {
if (!isAbsolute(resolvedDest)) {
resolvedDest = resolve(dirname(dest), resolvedDest);
}
if (isSrcSubdir(resolvedSrc, resolvedDest)) {

const srcIsDir = fsBinding.internalModuleStat(src) === 1;

if (srcIsDir && isSrcSubdir(resolvedSrc, resolvedDest)) {
throw new ERR_FS_CP_EINVAL({
message: `cannot copy ${resolvedSrc} to a subdirectory of self ` +
`${resolvedDest}`,
Expand Down
30 changes: 29 additions & 1 deletion test/parallel/test-fs-cp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
writeFileSync,
} = fs;
import net from 'net';
import { join } from 'path';
import { join, resolve } from 'path';
import { pathToFileURL } from 'url';
import { setTimeout } from 'timers/promises';

Expand Down Expand Up @@ -248,6 +248,34 @@ function nextdir(dirname) {
);
}

// It allows cpSync copying symlinks in src to locations in dest with existing synlinks not pointing to a directory.
{
const src = nextdir();
const dest = nextdir();
mkdirSync(src, mustNotMutateObjectDeep({ recursive: true }));
writeFileSync(`${src}/test.txt`, 'test');
symlinkSync(resolve(`${src}/test.txt`), join(src, 'link.txt'));
cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true }));
cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true }));
}

// It allows cp copying symlinks in src to locations in dest with existing synlinks not pointing to a directory.
{
const src = nextdir();
const dest = nextdir();
mkdirSync(src, mustNotMutateObjectDeep({ recursive: true }));
writeFileSync(`${src}/test.txt`, 'test');
symlinkSync(resolve(`${src}/test.txt`), join(src, 'link.txt'));
cp(src, dest, { recursive: true },
mustCall((err) => {
assert.strictEqual(err, null);

cp(src, dest, { recursive: true }, mustCall((err) => {
assert.strictEqual(err, null);
}));
}));
}

// It throws error if symlink in dest points to location in src.
{
const src = nextdir();
Expand Down
Loading