From 11412e863a6d49f8d65672dd60596e8a9ee5c47c Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 23 Oct 2023 13:22:28 -0400 Subject: [PATCH] fs: do not throw error on cpSync internals PR-URL: https://github.com/nodejs/node/pull/50185 Reviewed-By: Geoffrey Booth Reviewed-By: Antoine du Hamel --- lib/internal/fs/cp/cp-sync.js | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/lib/internal/fs/cp/cp-sync.js b/lib/internal/fs/cp/cp-sync.js index 348d45adcd7c4e..fe342f2dda19ec 100644 --- a/lib/internal/fs/cp/cp-sync.js +++ b/lib/internal/fs/cp/cp-sync.js @@ -116,17 +116,9 @@ 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 }; } @@ -134,13 +126,12 @@ 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}`,