fs: implement copyFileSync in C++ - #21
Open
anonrig wants to merge 3 commits into
Open
Conversation
Move path validation, file URL conversion, NUL checks, mode validation, permission checks, and the copy into a dedicated C++ binding so fs.copyFileSync() no longer goes through getValidatedPath() in JavaScript. Signed-off-by: Yagiz Nizipli <yagiz-twitter@nizipli.com> Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com> Signed-off-by: Yagiz Nizipli <yagiz-twitter@nizipli.com>
Exercise the C++ copyFileSync validator with Buffer paths, file: URLs, and a non-file URL scheme. Signed-off-by: Yagiz Nizipli <yagiz-twitter@nizipli.com> Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com>
The C++ FileURLToPath helper aborted on file URLs with a hostname because the ERR_INVALID_FILE_URL_HOST format string was missing %s. copyFileSync now converts URLs in C++, so that path was user-visible. Also match JS fileURLToPath's ERR_INVALID_URL_SCHEME message and cover Uint8Array paths, encoded slashes, and file URL hosts. Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com>
anonrig
marked this pull request as ready for review
August 18, 2026 02:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Rewrite
fs.copyFileSync()so the entire implementation lives in C++.JavaScript now only does VFS dispatch. Everything else — path type checks (
string/Buffer/Uint8Array/URL), file URL conversion, embedded-NUL rejection, mode validation, permission-model checks, and the copy — runs in a dedicatedbinding.copyFileSync()function.The copy itself still uses
uv_fs_copyfile, so flags (COPYFILE_EXCL,COPYFILE_FICLONE,COPYFILE_FICLONE_FORCE), mode/timestamp preservation, same-file handling, and UV error shapes (syscall: 'copyfile',path/dest) stay identical.Also fixes a crash in the C++
FileURLToPathhelper:ERR_INVALID_FILE_URL_HOSTused a format string without%s, which aborted when converting a file URL with a hostname (now reachable fromcopyFileSync).Why
copyFileSyncwas paying for twogetValidatedPath()trips in JS on every call. Moving that work next to the existing C++ permission checks anduv_fs_copyfileremoves that overhead without changing behavior.Testing
All of the following passed against
./out/Release/node:test/parallel/test-fs-copyfile.js(Buffer,Uint8Array,file:URL, non-file URL, encoded slashes, file URL host)test/parallel/test-fs-copyfile-respect-permissions.jstest/parallel/test-fs-null-bytes.jstest/parallel/test-fs-error-messages.jstest/parallel/test-fs-whatwg-url.jstest/parallel/test-url-fileurltopath.jstest-vfs-fs-copyFileSync.js,test-vfs-copyfile-mode.js,test-vfs-mount-errors.js, real/memory providerstest-permission-fs-read.js,test-permission-fs-write.jscopyFileSynccallers:test-fs-lchown.js,test-fs-fsync.js,test-fs-open-flags.js,test-trace-events-fs-sync.js,test-require-symlink.jsCompared C++
copyFileSyncvs the old JSgetValidatedPath+binding.copyFilepath (n=1e4):Official
benchmark/fs/bench-copyFileSync.jsalso ran (type=valid/type=invalid,n=10000).