Skip to content

Commit 996fdb0

Browse files
jakecastellitargos
authored andcommitted
fs: fix cp handle existing symlinks
PR-URL: #58476Fixes: #58468 Reviewed-By: Dario Piotrowicz <dario.piotrowicz@gmail.com>
1 parent d2931e5 commit 996fdb0

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎lib/internal/fs/cp/cp.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const {
5454
resolve,
5555
sep,
5656
}=require('path');
57+
constfsBinding=internalBinding('fs');
5758

5859
asyncfunctioncpFn(src,dest,opts){
5960
// Warn about using preserveTimestamps on 32-bit node
@@ -344,7 +345,10 @@ async function onLink(destStat, src, dest, opts) {
344345
if(!isAbsolute(resolvedDest)){
345346
resolvedDest=resolve(dirname(dest),resolvedDest);
346347
}
347-
if(isSrcSubdir(resolvedSrc,resolvedDest)){
348+
349+
constsrcIsDir=fsBinding.internalModuleStat(src)===1;
350+
351+
if(srcIsDir&&isSrcSubdir(resolvedSrc,resolvedDest)){
348352
thrownewERR_FS_CP_EINVAL({
349353
message: `cannot copy ${resolvedSrc} to a subdirectory of self `+
350354
`${resolvedDest}`,

‎test/parallel/test-fs-cp.mjs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ function nextdir(dirname) {
248248
);
249249
}
250250

251-
// It allows copying when is not a directory
251+
// It allows cpSync copying symlinks in src to locations in dest with existing synlinks not pointing to a directory.
252252
{
253253
constsrc=nextdir();
254254
constdest=nextdir();
@@ -259,6 +259,23 @@ function nextdir(dirname) {
259259
cpSync(src,dest,mustNotMutateObjectDeep({recursive: true}));
260260
}
261261

262+
// It allows cp copying symlinks in src to locations in dest with existing synlinks not pointing to a directory.
263+
{
264+
constsrc=nextdir();
265+
constdest=nextdir();
266+
mkdirSync(src,mustNotMutateObjectDeep({recursive: true}));
267+
writeFileSync(`${src}/test.txt`,'test');
268+
symlinkSync(resolve(`${src}/test.txt`),join(src,'link.txt'));
269+
cp(src,dest,{recursive: true},
270+
mustCall((err)=>{
271+
assert.strictEqual(err,null);
272+
273+
cp(src,dest,{recursive: true},mustCall((err)=>{
274+
assert.strictEqual(err,null);
275+
}));
276+
}));
277+
}
278+
262279
// It throws error if symlink in dest points to location in src.
263280
{
264281
constsrc=nextdir();

0 commit comments

Comments
 (0)