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 operation not permitted #44786

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 2 additions & 7 deletions lib/internal/fs/cp/cp-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,6 @@ function handleFilterAndCopy(destStat, src, dest, opts) {
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 +279,9 @@ function copyDir(src, dest, opts) {
const { name } = dirent;
const srcItem = join(src, name);
const destItem = join(dest, name);
if (opts.filter && !opts.filter(srcItem, destItem)) continue;
const { destStat } = checkPathsSync(srcItem, destItem, opts);

startCopy(destStat, srcItem, destItem, opts);
getStats(destStat, srcItem, destItem, opts);
}
} finally {
dir.closeSync();
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-fs-cp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,24 @@ if (!isWindows) {
}, { code: 'ERR_INVALID_RETURN_VALUE' });
}

// It should not throw exception if child folder
// is filtered out.
{
// Create a file in dest with the same name as a child folder in src
// expect: this shouldn't throw error since filtered out by filter function
const src = nextdir();
mkdirSync(join(src, 'foo'), mustNotMutateObjectDeep({ recursive: true }));

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

cpSync(src, dest, {
filter: (path) => !path.includes('foo'),
recursive: true,
});
}
Copy link
Contributor Author

@thoqbk thoqbk Oct 1, 2022

Choose a reason for hiding this comment

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

Before fixing, when I run this test it throws the following error because checkPathsSync(srcItem, destItem, opts) gets called even though the srcItem shouldn't be copied:

    SystemError [ERR_FS_CP_DIR_TO_NON_DIR]: Cannot overwrite directory with non-directory: cp returned EISDIR (cannot overwrite directory /Users/xxx/dev/node/test/.tmp.0/copy_28/foo with non-directory /Users/xxx/dev/node/test/.tmp.0/copy_29/foo) /Users/xxx/dev/node/test/.tmp.0/copy_29/foo
        at new SystemError (node:internal/errors:244:5)
        at new NodeError (node:internal/errors:355:7)
        at checkPathsSync (node:internal/fs/cp/cp-sync:77:13)
        at copyDir (node:internal/fs/cp/cp-sync:287:28)
        at onDir (node:internal/fs/cp/cp-sync:268:10)
        at getStats (node:internal/fs/cp/cp-sync:171:12)
        at handleFilterAndCopy (node:internal/fs/cp/cp-sync:158:10)
        at cpSyncFn (node:internal/fs/cp/cp-sync:60:10)
        at cpSync (node:fs:2899:3)
        at file:///Users/xxx/dev/node/test/parallel/test-fs-cp.mjs:353:3 {
      code: 'ERR_FS_CP_DIR_TO_NON_DIR',
      info: {
        message: 'cannot overwrite directory /Users/xxx/dev/node/test/.tmp.0/copy_28/foo with non-directory /Users/xxx/dev/node/test/.tmp.0/copy_29/foo',
        path: '/Users/xxx/dev/node/test/.tmp.0/copy_29/foo',
        syscall: 'cp',
        errno: 21,
        code: 'EISDIR'
      },
      errno: [Getter/Setter],
      syscall: [Getter/Setter],
      path: [Getter/Setter]
    }


// It throws error if errorOnExist is true, force is false, and file or folder
// copied over.
{
Expand Down