fs: fix cpSync handle existing symlinks

PR-URL: https://github.com/nodejs/node/pull/58476
Fixes: https://github.com/nodejs/node/issues/58468
Reviewed-By: Dario Piotrowicz <dario.piotrowicz@gmail.com>
This commit is contained in:
Yuesong Jake Li 2025-05-27 12:16:11 +09:30 committed by Node.js GitHub Bot
parent 49679ddd98
commit 0e8ae91855
2 changed files with 15 additions and 2 deletions

View File

@ -196,7 +196,9 @@ function onLink(destStat, src, dest, verbatimSymlinks) {
if (!isAbsolute(resolvedDest)) { if (!isAbsolute(resolvedDest)) {
resolvedDest = resolve(dirname(dest), 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({ throw new ERR_FS_CP_EINVAL({
message: `cannot copy ${resolvedSrc} to a subdirectory of self ` + message: `cannot copy ${resolvedSrc} to a subdirectory of self ` +
`${resolvedDest}`, `${resolvedDest}`,

View File

@ -15,7 +15,7 @@ const {
writeFileSync, writeFileSync,
} = fs; } = fs;
import net from 'net'; import net from 'net';
import { join } from 'path'; import { join, resolve } from 'path';
import { pathToFileURL } from 'url'; import { pathToFileURL } from 'url';
import { setTimeout } from 'timers/promises'; import { setTimeout } from 'timers/promises';
@ -248,6 +248,17 @@ function nextdir(dirname) {
); );
} }
// It allows copying when is not 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 throws error if symlink in dest points to location in src. // It throws error if symlink in dest points to location in src.
{ {
const src = nextdir(); const src = nextdir();