Skip to content

Commit

Permalink
fs: do not throw error on cpSync internals
Browse files Browse the repository at this point in the history
PR-URL: #50185
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
anonrig authored and UlisesGascon committed Dec 11, 2023
1 parent 8414fb4 commit 11412e8
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions lib/internal/fs/cp/cp-sync.js
Expand Up @@ -116,31 +116,22 @@ function checkPathsSync(src, dest, opts) {
}

function getStatsSync(src, dest, opts) {
let destStat;
const statFunc = opts.dereference ?
(file) => statSync(file, { bigint: true }) :
(file) => lstatSync(file, { bigint: true });
const srcStat = statFunc(src);
try {
destStat = statFunc(dest);
} catch (err) {
if (err.code === 'ENOENT') return { srcStat, destStat: null };
throw err;
}
const statFunc = opts.dereference ? statSync : lstatSync;
const srcStat = statFunc(src, { bigint: true, throwIfNoEntry: true });
const destStat = statFunc(dest, { bigint: true, throwIfNoEntry: false });
return { srcStat, destStat };
}

function checkParentPathsSync(src, srcStat, dest) {
const srcParent = resolve(dirname(src));
const destParent = resolve(dirname(dest));
if (destParent === srcParent || destParent === parse(destParent).root) return;
let destStat;
try {
destStat = statSync(destParent, { bigint: true });
} catch (err) {
if (err.code === 'ENOENT') return;
throw err;
const destStat = statSync(destParent, { bigint: true, throwIfNoEntry: false });

if (destStat === undefined) {
return;
}

if (areIdentical(srcStat, destStat)) {
throw new ERR_FS_CP_EINVAL({
message: `cannot copy ${src} to a subdirectory of self ${dest}`,
Expand Down

0 comments on commit 11412e8

Please sign in to comment.