Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: fix opts.filter issue in cpSync #45143

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions lib/internal/fs/cp/cp-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,20 @@ function cpSyncFn(src, dest, opts) {
'node is not recommended';
process.emitWarning(warning, 'TimestampPrecisionWarning');
}
const { srcStat, destStat } = checkPathsSync(src, dest, opts);
const { srcStat, destStat, skipped } = checkPathsSync(src, dest, opts);
if (skipped) return;
checkParentPathsSync(src, srcStat, dest);
return handleFilterAndCopy(destStat, src, dest, opts);
return checkParentDir(destStat, src, dest, opts);
}

function checkPathsSync(src, dest, opts) {
if (opts.filter) {
const shouldCopy = opts.filter(src, dest);
if (isPromise(shouldCopy)) {
throw new ERR_INVALID_RETURN_VALUE('boolean', 'filter', shouldCopy);
}
if (!shouldCopy) return { __proto__: null, skipped: true };
}
const { srcStat, destStat } = getStatsSync(src, dest, opts);

if (destStat) {
Expand Down Expand Up @@ -104,7 +112,7 @@ function checkPathsSync(src, dest, opts) {
code: 'EINVAL',
});
}
return { srcStat, destStat };
return { __proto__: null, srcStat, destStat, skipped: false };
}

function getStatsSync(src, dest, opts) {
Expand Down Expand Up @@ -145,24 +153,12 @@ function checkParentPathsSync(src, srcStat, dest) {
return checkParentPathsSync(src, srcStat, destParent);
}

function handleFilterAndCopy(destStat, src, dest, opts) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the name to make it consistent with cp-async

if (opts.filter) {
const shouldCopy = opts.filter(src, dest);
if (isPromise(shouldCopy)) {
throw new ERR_INVALID_RETURN_VALUE('boolean', 'filter', shouldCopy);
}
if (!shouldCopy) return;
}
function checkParentDir(destStat, src, dest, opts) {
const destParent = dirname(dest);
if (!existsSync(destParent)) mkdirSync(destParent, { recursive: true });
return getStats(destStat, src, dest, opts);
}

function startCopy(destStat, src, dest, opts) {
if (opts.filter && !opts.filter(src, dest)) return;
return getStats(destStat, src, dest, opts);
}

function getStats(destStat, src, dest, opts) {
const statSyncFn = opts.dereference ? statSync : lstatSync;
const srcStat = statSyncFn(src);
Expand Down Expand Up @@ -284,9 +280,8 @@ function copyDir(src, dest, opts) {
const { name } = dirent;
const srcItem = join(src, name);
const destItem = join(dest, name);
const { destStat } = checkPathsSync(srcItem, destItem, opts);

startCopy(destStat, srcItem, destItem, opts);
const { destStat, skipped } = checkPathsSync(srcItem, destItem, opts);
if (!skipped) getStats(destStat, srcItem, destItem, opts);
}
} finally {
dir.closeSync();
Expand Down
22 changes: 13 additions & 9 deletions test/parallel/test-fs-cp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -746,24 +746,26 @@ if (!isWindows) {
}));
}

// Copy async should not throw exception if child folder is filtered out.
// Copy should not throw exception if child folder is filtered out.
{
const src = nextdir();
mkdirSync(join(src, 'test-cp-async'), mustNotMutateObjectDeep({ recursive: true }));
mkdirSync(join(src, 'test-cp'), mustNotMutateObjectDeep({ recursive: true }));

const dest = nextdir();
mkdirSync(dest, mustNotMutateObjectDeep({ recursive: true }));
writeFileSync(join(dest, 'test-cp-async'), 'test-content', mustNotMutateObjectDeep({ mode: 0o444 }));
writeFileSync(join(dest, 'test-cp'), 'test-content', mustNotMutateObjectDeep({ mode: 0o444 }));

cp(src, dest, {
filter: (path) => !path.includes('test-cp-async'),
const opts = {
filter: (path) => !path.includes('test-cp'),
recursive: true,
}, mustCall((err) => {
};
cp(src, dest, opts, mustCall((err) => {
assert.strictEqual(err, null);
}));
cpSync(src, dest, opts);
}

// Copy async should not throw exception if dest is invalid but filtered out.
// Copy should not throw exception if dest is invalid but filtered out.
{
// Create dest as a file.
// Expect: cp skips the copy logic entirely and won't throw any exception in path validation process.
Expand All @@ -775,12 +777,14 @@ if (!isWindows) {
mkdirSync(destParent, mustNotMutateObjectDeep({ recursive: true }));
writeFileSync(dest, 'test-content', mustNotMutateObjectDeep({ mode: 0o444 }));

cp(src, dest, {
const opts = {
filter: (path) => !path.includes('bar'),
recursive: true,
}, mustCall((err) => {
};
cp(src, dest, opts, mustCall((err) => {
assert.strictEqual(err, null);
}));
cpSync(src, dest, opts);
}

// It throws if options is not object.
Expand Down