src: consolidate C++ ReadFileSync/WriteFileSync utilities#61662
Open
joyeecheung wants to merge 2 commits intonodejs:mainfrom
Open
src: consolidate C++ ReadFileSync/WriteFileSync utilities#61662joyeecheung wants to merge 2 commits intonodejs:mainfrom
joyeecheung wants to merge 2 commits intonodejs:mainfrom
Conversation
This patch moves `ReadFileSync` and `WriteFileSync` from `src/util.cc` to `src/node_file_utils.cc`, consolidates the implementation to reuse code, and adds a few more enhancements: For `ReadFileSync`: - Use fstat-based pre-allocation to minimize buffer resizing and repeated reads for bigger files. - Handle various potential overflows in size conversions. - Handle fallback for 0-byte special files. For `WriteFileSync`: - Handle potential partial writes for big enough files and support non-seekable files (with -1 as offset). - Handle 0-byte writes correctly. In both cases, this now avoids hard aborts and return error code for the caller to handle as much as possible, except `std::vector<char> ReadFileSync(FILE* fp)` which is part of the embedder API. This patch uses the new `ReadFileSync` to address a TODO in node_sea.bin.cc.
Collaborator
|
Review requested:
|
Member
Author
|
cc @nodejs/cpp-reviewers |
RafaelGSS
approved these changes
Feb 3, 2026
Collaborator
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #61662 +/- ##
==========================================
- Coverage 89.74% 89.72% -0.03%
==========================================
Files 674 675 +1
Lines 204348 204472 +124
Branches 39271 39290 +19
==========================================
+ Hits 183396 183460 +64
- Misses 13262 13303 +41
- Partials 7690 7709 +19
🚀 New features to boost your workflow:
|
Qard
approved these changes
Feb 4, 2026
Member
Qard
left a comment
There was a problem hiding this comment.
LGTM. A few small code quality improvement ideas, but I'm fine landing as-is. These are just minor suggestions to simplify things slightly. 🙂
Co-authored-by: Stephen Belanger <[email protected]>
Member
Author
|
Thanks for the suggestion. @Qard @RafaelGSS Can you take a look again? |
addaleax
approved these changes
Feb 5, 2026
Collaborator
Qard
approved these changes
Feb 5, 2026
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.
This patch moves
ReadFileSyncandWriteFileSyncfromsrc/util.cctosrc/node_file_utils.cc, consolidates the implementation to reuse code, and adds a few more enhancements:For
ReadFileSync:For
WriteFileSync:In both cases, this now avoids hard aborts and return error code for the caller to handle as much as possible, except
std::vector<char> ReadFileSync(FILE* fp)which is part of the embedder API.This patch uses the new
ReadFileSyncto address a TODO in node_sea.bin.cc.Some numbers from
--build-sea(note that most of the time is spent on parsing the executable and building the SEA, but even so there's a measurable difference just from the file I/O because the binary is ~120MB).