Skip to content

Commit

Permalink
fs: reimplement accessSync
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 10, 2023
1 parent 80c845b commit 10cb7e5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
29 changes: 29 additions & 0 deletions benchmark/fs/bench-accessSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();

const paths = [
__filename,
tmpdir.resolve(`.non-existing-file-${process.pid}`),
];

const bench = common.createBenchmark(main, {
n: [1e5],
});

function main({ n }) {
bench.start();
for (let i = 0; i < n; i++) {
for (let j = 0; j < paths.length; j++) {
try {
fs.accessSync(paths[j]);
} catch {
// do nothing
}
}
}
bench.end(n);
}
7 changes: 1 addition & 6 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,7 @@ function access(path, mode, callback) {
* @returns {void}
*/
function accessSync(path, mode) {
path = getValidatedPath(path);
mode = getValidMode(mode, 'access');

const ctx = { path };
binding.access(pathModule.toNamespacedPath(path), mode, undefined, ctx);
handleErrorFromBinding(ctx);
syncFs.access(path, mode);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion lib/internal/fs/sync.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const pathModule = require('path');
const { handleErrorFromBinding, getValidatedPath, stringToFlags } = require('internal/fs/utils');
const { handleErrorFromBinding, getValidatedPath, stringToFlags, getValidMode } = require('internal/fs/utils');

const syncBinding = internalBinding('fs_sync');

Expand Down Expand Up @@ -32,7 +32,19 @@ function exists(path) {
return syncBinding.exists(pathModule.toNamespacedPath(path));
}

function access(path, mode) {
path = getValidatedPath(path);
mode = getValidMode(mode, 'access');

const errno = syncBinding.access(pathModule.toNamespacedPath(path), mode);

if (errno !== undefined) {
handleErrorFromBinding({ errno, syscall: 'access', path });
}
}

module.exports = {
readFileUtf8,
exists,
access,
};
28 changes: 28 additions & 0 deletions src/node_file_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ void BindingData::Deserialize(v8::Local<v8::Context> context,
CHECK_NOT_NULL(binding);
}

void BindingData::Access(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();

const int argc = args.Length();
CHECK_GE(argc, 2);

CHECK(args[1]->IsInt32());
int mode = args[1].As<Int32>()->Value();

BufferValue path(isolate, args[0]);
CHECK_NOT_NULL(*path);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());

uv_fs_t req;
auto make = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
FS_SYNC_TRACE_BEGIN(access);
uv_fs_access(nullptr, &req, *path, mode, nullptr);
FS_SYNC_TRACE_END(access);

if (req.result < 0) {
return args.GetReturnValue().Set(int(req.result));
}
}

bool BindingData::ExistsInternal(const std::string_view path) {
uv_fs_t req;
auto make = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
Expand Down Expand Up @@ -237,6 +263,7 @@ void BindingData::ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->isolate();
SetMethodNoSideEffect(isolate, target, "access", Access);
SetFastMethodNoSideEffect(isolate, target, "exists", Exists, &fast_exists_);
SetMethodNoSideEffect(isolate, target, "readFileUtf8", ReadFileUtf8);
}
Expand All @@ -251,6 +278,7 @@ void BindingData::CreatePerContextProperties(Local<Object> target,

void BindingData::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(Access);
registry->Register(Exists);
registry->Register(FastExists);
registry->Register(fast_exists_.GetTypeInfo());
Expand Down
1 change: 1 addition & 0 deletions src/node_file_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class BindingData : public SnapshotableObject {
static void Exists(const v8::FunctionCallbackInfo<v8::Value>& args);
static bool FastExists(v8::Local<v8::Value> receiver, const v8::FastOneByteString& path);

static void Access(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ReadFileUtf8(const v8::FunctionCallbackInfo<v8::Value>& args);

static void CreatePerIsolateProperties(IsolateData* isolate_data,
Expand Down

0 comments on commit 10cb7e5

Please sign in to comment.