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.cp] fails with EPERM despite filter #44720

Closed
c-vetter opened this issue Sep 19, 2022 · 2 comments · Fixed by #44922 or #45143
Closed

[fs.cp] fails with EPERM despite filter #44720

c-vetter opened this issue Sep 19, 2022 · 2 comments · Fixed by #44922 or #45143
Labels
fs Issues and PRs related to the fs subsystem / file system. good first issue Issues that are suitable for first-time contributors.

Comments

@c-vetter
Copy link

Version

v18.9.0

Platform

Microsoft Windows NT 10.0.19044.0 x64

Subsystem

No response

What steps will reproduce the bug?

$ node
Welcome to Node.js v18.9.0.
Type ".help" for more information.
> require(`fs`).cpSync(`B:`, `A:\\test`, { recursive: true, filter:(fp)=>(!fp.includes("System")) })
Uncaught Error: EPERM: operation not permitted, lstat '\\?\B:\System Volume Information'
    at lstatSync (node:fs:1574:3)
    at statFunc (node:internal/fs/cp/cp-sync:114:15)
    at getStatsSync (node:internal/fs/cp/cp-sync:115:19)
    at checkPathsSync (node:internal/fs/cp/cp-sync:64:33)
    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 Object.cpSync (node:fs:2904:3) {
  errno: -4048,
  syscall: 'lstat',
  code: 'EPERM',
  path: '\\\\?\\B:\\System Volume Information'
}

How often does it reproduce? Is there a required condition?

Happens every time. Both drives are external, connected via USB.
Source can also be a subdirectory, analogous error.

I generally use Powershell Core, not the built-in Powershell.
But the same happens there.

What is the expected behavior?

The copy operation proceeds without trying to access the filtered-out directory.

What do you see instead?

Uncaught Error: EPERM: operation not permitted, lstat '\\?\B:\System Volume Information'

Additional information

This was triggered by jprichardson/node-fs-extra#965 (comment)

@tniessen
Copy link
Member

I assume it is because copyDir calls checkPathsSync(), which invokes lstat(), before it calls startCopy(), which applies the filter. Given that the filter function only receives the paths and not the result of lstat(), it should be relatively simple to change that.

@tniessen tniessen added the good first issue Issues that are suitable for first-time contributors. label Sep 24, 2022
@thoqbk
Copy link
Contributor

thoqbk commented Sep 25, 2022

@tniessen am I right if we should add this check before calling checkPathsSync()?

if (opts.filter && !opts.filter(srcItem, destItem)) continue;

And since the startCopy has 2 lines and one of them is the filter call, should we move its main logic to copyDir and remove this function? Overall, the copyDir will look like this:

function copyDir(src, dest, opts) {
  const dir = opendirSync(src);

  try {
    let dirent;

    while ((dirent = dir.readSync()) !== null) {
      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);
      getStats(destStat, srcItem, destItem, opts);
    }
  } finally {
    dir.closeSync();
  }
}

thoqbk added a commit to thoqbk/node that referenced this issue Sep 25, 2022
fix: nodejs#44720

issue:
- [copyDir](https://github.com/nodejs/node/blob/7e0097d8a33fa7adbc1f298cbf647f6d2fd403e8/lib/internal/fs/cp/cp-sync.js#L287-L289) calls checkPathsSync(), which invokes lstat() which causes error because not checking the opts.filter

changes:
- check opts.filter before calling `checkPathsSync` and invoking copy logic
- cleanup `startCopy` function
thoqbk added a commit to thoqbk/node that referenced this issue Sep 25, 2022
fix: nodejs#44720

issue:
- `copyDir()` calls `checkPathsSync()`, which invokes `lstat()`
which causes error because of not checking the opts.filter

changes:
- check opts.filter before calling `checkPathsSync` and copy logic
- cleanup `startCopy` function
nodejs-github-bot pushed a commit that referenced this issue Oct 10, 2022
PR-URL: #44922
Fixes: #44720
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
danielleadams pushed a commit that referenced this issue Oct 11, 2022
PR-URL: #44922
Fixes: #44720
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
nodejs-github-bot pushed a commit that referenced this issue Nov 1, 2022
PR-URL: #45143
Fixes: #44720
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
RafaelGSS pushed a commit that referenced this issue Nov 1, 2022
PR-URL: #45143
Fixes: #44720
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
lucshi pushed a commit to lucshi/node that referenced this issue Nov 9, 2022
PR-URL: nodejs#45143
Fixes: nodejs#44720
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
RafaelGSS pushed a commit that referenced this issue Nov 10, 2022
PR-URL: #45143
Fixes: #44720
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
danielleadams pushed a commit that referenced this issue Dec 30, 2022
PR-URL: #45143
Fixes: #44720
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
danielleadams pushed a commit that referenced this issue Dec 30, 2022
PR-URL: #45143
Fixes: #44720
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
danielleadams pushed a commit that referenced this issue Jan 3, 2023
PR-URL: #45143
Fixes: #44720
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. good first issue Issues that are suitable for first-time contributors.
Projects
None yet
4 participants