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 <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
anonrig authored and targos committed Nov 11, 2023
1 parent 541bdf1 commit 3e7226a
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions lib/internal/fs/cp/cp-sync.js
Original file line number Diff line number Diff line change
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 3e7226a

Please sign in to comment.