Skip to content

Commit

Permalink
fs: improve ExistsSync performance
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jun 24, 2024
1 parent d335487 commit a4b05d2
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1003,24 +1003,14 @@ static void ExistsSync(const FunctionCallbackInfo<Value>& args) {
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());

uv_fs_t req;
auto make = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
std::error_code error{};
FS_SYNC_TRACE_BEGIN(access);
int err = uv_fs_access(nullptr, &req, path.out(), 0, nullptr);
bool exists = std::filesystem::exists(path.ToStringView(), error);
FS_SYNC_TRACE_END(access);

#ifdef _WIN32
// In case of an invalid symlink, `uv_fs_access` on win32
// will **not** return an error and is therefore not enough.
// Double check with `uv_fs_stat()`.
if (err == 0) {
FS_SYNC_TRACE_BEGIN(stat);
err = uv_fs_stat(nullptr, &req, path.out(), nullptr);
FS_SYNC_TRACE_END(stat);
if (error) {
exists = false;
}
#endif // _WIN32

args.GetReturnValue().Set(err == 0);
args.GetReturnValue().Set(exists);
}

// Used to speed up module loading. Returns 0 if the path refers to
Expand Down

0 comments on commit a4b05d2

Please sign in to comment.