Skip to content

fs: implement copyFileSync in C++ - #21

Open
anonrig wants to merge 3 commits into
mainfrom
cursor/copyfilesync-cpp-317c
Open

fs: implement copyFileSync in C++#21
anonrig wants to merge 3 commits into
mainfrom
cursor/copyfilesync-cpp-317c

Conversation

@anonrig

@anonrig anonrig commented Aug 18, 2026

Copy link
Copy Markdown
Owner

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 dedicated binding.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++ FileURLToPath helper: ERR_INVALID_FILE_URL_HOST used a format string without %s, which aborted when converting a file URL with a hostname (now reachable from copyFileSync).

Why

copyFileSync was paying for two getValidatedPath() trips in JS on every call. Moving that work next to the existing C++ permission checks and uv_fs_copyfile removes 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.js
  • test/parallel/test-fs-null-bytes.js
  • test/parallel/test-fs-error-messages.js
  • test/parallel/test-fs-whatwg-url.js
  • test/parallel/test-url-fileurltopath.js
  • VFS: test-vfs-fs-copyFileSync.js, test-vfs-copyfile-mode.js, test-vfs-mount-errors.js, real/memory providers
  • Permission model: test-permission-fs-read.js, test-permission-fs-write.js
  • Other copyFileSync callers: test-fs-lchown.js, test-fs-fsync.js, test-fs-open-flags.js, test-trace-events-fs-sync.js, test-require-symlink.js

Compared C++ copyFileSync vs the old JS getValidatedPath + binding.copyFile path (n=1e4):

case vs old
valid copy ~6% faster
missing src (ENOENT) ~7% faster
invalid type ~68% faster
file URL ~1% faster

Official benchmark/fs/bench-copyFileSync.js also ran (type=valid / type=invalid, n=10000).

Open in Web Open in Cursor 

Yagiz Nizipli and others added 3 commits August 18, 2026 00:35
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
anonrig marked this pull request as ready for review August 18, 2026 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant