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.cpSync fails when copying to a directory with a name starting with the source directory name #54285

Closed
ikemo3 opened this issue Aug 9, 2024 · 3 comments · Fixed by #55033
Labels
confirmed-bug Issues with confirmed bugs. fs Issues and PRs related to the fs subsystem / file system.

Comments

@ikemo3
Copy link

ikemo3 commented Aug 9, 2024

Version

v22.6.0

Platform

Darwin ikemoto-macmini.local 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:21 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T8103 arm64

Subsystem

fs module

What steps will reproduce the bug?

fs = require("fs");
fs.cpSync("./foo", "./foobar", { recursive: true });

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

This bug reproduces consistently in Node.js v22.6.0. It works correctly in v22.5.1.

What is the expected behavior? Why is that the expected behavior?

The expected behavior, as seen in v22.5.1, is that the ./foo directory should be copied to ./foobar. This is the normal operation of the fs.cpSync function and aligns with the documented behavior.

What do you see instead?

In v22.6.0, the following error is thrown:

node:internal/fs/cp/cp-sync:56
  fsBinding.cpSyncCheckPaths(src, dest, opts.dereference, opts.recursive);
            ^

Error: Cannot copy ./foo to a subdirectory of self ./foobar
    at cpSyncFn (node:internal/fs/cp/cp-sync:56:13)
    at Object.cpSync (node:fs:3044:3)
    at Object.<anonymous> (/Users/ikemo/tmp/main.js:2:4)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)
    at Module._load (node:internal/modules/cjs/loader:1127:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:166:5) {
  code: 'ERR_FS_CP_EINVAL'
}

Additional information

The issue is caused by the misuse of the starts_with function in a newly added check. This check incorrectly prevents copying to directories whose names start with the source directory name. For details, see:
https://github.com/nodejs/node/pull/53614/files#diff-70e3325bd2115867617ae2a16321c5de53c070ff2841976fe5b849675a5111e6R3186-R3190

@avivkeller avivkeller added the fs Issues and PRs related to the fs subsystem / file system. label Aug 9, 2024
@avivkeller
Copy link
Member

Thanks for the report! Seeing as you seem to know the cause, if you'd like, you can submit a patch PR.

@avivkeller
Copy link
Member

I've opened a PR patching this in #54288. Thanks again :-)

@blackram
Copy link

present in node v22.7.0

`PS C:\temp\drtest> node run.js
node:internal/fs/cp/cp-sync:56
fsBinding.cpSyncCheckPaths(src, dest, opts.dereference, opts.recursive);
^

Error: Cannot copy \?\C:\temp\drtest\foo to a subdirectory of self \?\C:\temp\drtest\foobar
at cpSyncFn (node:internal/fs/cp/cp-sync:56:13)
at Object.cpSync (node:fs:3046:3)
at Object. (C:\temp\drtest\run.js:2:4)
at Module._compile (node:internal/modules/cjs/loader:1546:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
at Module.load (node:internal/modules/cjs/loader:1317:32)
at Module._load (node:internal/modules/cjs/loader:1127:12)
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:166:5) {
code: 'ERR_FS_CP_EINVAL'
}

Node.js v22.7.0
PS C:\temp\drtest>
`

drtest.zip

nodejs-github-bot pushed a commit that referenced this issue Sep 28, 2024
PR-URL: #55033
Fixes: #54285
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
targos pushed a commit that referenced this issue Oct 4, 2024
PR-URL: #55033
Fixes: #54285
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
targos pushed a commit that referenced this issue Oct 4, 2024
PR-URL: #55033
Fixes: #54285
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
louwers pushed a commit to louwers/node that referenced this issue Nov 2, 2024
PR-URL: nodejs#55033
Fixes: nodejs#54285
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
tpoisseau pushed a commit to tpoisseau/node that referenced this issue Nov 21, 2024
PR-URL: nodejs#55033
Fixes: nodejs#54285
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
4 participants