mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 18:25:13 +08:00
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>
40 lines
1.1 KiB
C++
40 lines
1.1 KiB
C++
#ifndef SRC_NODE_FILE_UTILS_H_
|
|
#define SRC_NODE_FILE_UTILS_H_
|
|
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
#include <cstdio>
|
|
#include <functional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "uv.h"
|
|
#include "v8.h"
|
|
|
|
namespace node {
|
|
|
|
// Synchronously writes to a file. If the file exists, it is replaced
|
|
// (truncated).
|
|
int WriteFileSync(const char* path, uv_buf_t buf);
|
|
int WriteFileSync(const char* path, uv_buf_t* bufs, size_t buf_count);
|
|
int WriteFileSync(v8::Isolate* isolate,
|
|
const char* path,
|
|
v8::Local<v8::String> string);
|
|
|
|
// Synchronously reads the entire contents of a file.
|
|
int ReadFileSync(const char* path, std::string* result);
|
|
int ReadFileSync(const char* path, std::vector<uint8_t>* result);
|
|
|
|
// Legacy interface. TODO(joyeecheung): update the callers to pass path first,
|
|
// output parameters second.
|
|
int ReadFileSync(std::string* result, const char* path);
|
|
|
|
// This is currently only used by embedder APIs that take a FILE*.
|
|
std::vector<char> ReadFileSync(FILE* fp);
|
|
|
|
} // namespace node
|
|
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
#endif // SRC_NODE_FILE_UTILS_H_
|