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>