Commit Graph
1 Commits
Author SHA1 Message Date
Joyee Cheung 3ab9dd86e7 src: consolidate C++ ReadFileSync/WriteFileSync utilities
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.

PR-URL: https://github.com/nodejs/node/pull/61662
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-02-07 02:38:56 +00:00