Skip to content

Commit

Permalink
fixup! fs: improve readFileSync with file descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 24, 2023
1 parent 28cf7ff commit a415654
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2551,7 +2551,6 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {

uv_file file;
uv_fs_t req;
auto defer_req_cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });

bool is_fd = args[0]->IsInt32();

Expand All @@ -2567,17 +2566,19 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
file = uv_fs_open(nullptr, &req, *path, flags, O_RDONLY, nullptr);
FS_SYNC_TRACE_END(open);
if (req.result < 0) {
uv_fs_req_cleanup(&req);
// req will be cleaned up by scope leave.
return env->ThrowUVException(req.result, "open", nullptr, path.out());
}
}

auto defer_close = OnScopeLeave([file, is_fd]() {
auto defer_close = OnScopeLeave([file, is_fd, &req]() {
if (!is_fd) {
uv_fs_t close_req;
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr));
uv_fs_req_cleanup(&close_req);
FS_SYNC_TRACE_BEGIN(close);
CHECK_EQ(0, uv_fs_close(nullptr, &req, file, nullptr));
FS_SYNC_TRACE_END(close);
}
uv_fs_req_cleanup(&req);
});

std::string result{};
Expand Down

0 comments on commit a415654

Please sign in to comment.