Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: improve error performance of fchownSync #49920

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions benchmark/fs/bench-fchownSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

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

// Windows does not have `getuid` or `getgid`.
if (process.platform === 'win32') {
return;
}

const bench = common.createBenchmark(main, {
type: ['existing', 'non-existing'],
n: [1e3],
});

function main({ n, type }) {
const uid = process.getuid();
const gid = process.getgid();
let files;

switch (type) {
case 'existing':
files = [];

// Populate tmpdir with mock files
for (let i = 0; i < n; i++) {
const path = tmpdir.resolve(`fchmodsync-bench-file-${i}`);
fs.writeFileSync(path, 'bench');
files.push(path);
}
break;
case 'non-existing':
files = new Array(n).fill(tmpdir.resolve(`.non-existing-file-${Date.now()}`));
break;
default:
new Error('Invalid type');
}

const fds = files.map((x) => {
// Try to open, if not return likely invalid fd (1 << 30)
try {
return fs.openSync(x, 'r');
} catch {
return 1 << 30;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this only be part of the non-existing case?

}
});

bench.start();
for (let i = 0; i < n; i++) {
try {
fs.fchownSync(fds[i], uid, gid);
} catch {
// do nothing
}
}
bench.end(n);

for (const x of fds) {
try {
fs.closeSync(x);
} catch {
// do nothing
}
}
}
5 changes: 1 addition & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2059,10 +2059,7 @@
fd = getValidatedFd(fd);
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);

const ctx = {};
binding.fchown(fd, uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
return syncFs.fchown(fd, uid, gid);

Check failure on line 2062 in lib/fs.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'syncFs' is not defined
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2626,6 +2626,32 @@ static void FChown(const FunctionCallbackInfo<Value>& args) {
}


static void FChownSync(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

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

CHECK(args[0]->IsInt32());
const uv_file fd = static_cast<uv_file>(args[0].As<Int32>()->Value());

CHECK(args[1]->IsInt32());
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Int32>()->Value());

CHECK(args[2]->IsInt32());
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Int32>()->Value());

uv_fs_t req;
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
FS_SYNC_TRACE_BEGIN(fchown);
int err = uv_fs_fchown(nullptr, &req, fd, uid, gid, nullptr);
FS_SYNC_TRACE_END(fchown);
if (err < 0) {
return env->ThrowUVException(err, "fchown");
Copy link
Member

@joyeecheung joyeecheung Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need a new binding, it can just be done by switching SyncCall to SyncCallAndThrowOnError in the original FChownSync implementation.

}
}


static void LChown(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand Down Expand Up @@ -3224,6 +3250,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
SetMethod(isolate, target, "rmdir", RMDir);
SetMethod(isolate, target, "mkdir", MKDir);
SetMethod(isolate, target, "readdir", ReadDir);
SetMethod(isolate, target, "fchownSync", FChownSync);
SetMethod(isolate, target, "internalModuleReadJSON", InternalModuleReadJSON);
SetMethod(isolate, target, "internalModuleStat", InternalModuleStat);
SetMethod(isolate, target, "stat", Stat);
Expand Down Expand Up @@ -3364,6 +3391,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {

registry->Register(Chown);
registry->Register(FChown);
registry->Register(FChownSync);
registry->Register(LChown);

registry->Register(UTimes);
Expand Down
2 changes: 2 additions & 0 deletions typings/internalBinding/fs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ declare namespace InternalFSBinding {
function chown(path: string, uid: number, gid: number, req: FSReqCallback): void;
function chown(path: string, uid: number, gid: number, req: undefined, ctx: FSSyncContext): void;
function chown(path: string, uid: number, gid: number, usePromises: typeof kUsePromises): Promise<void>;
function fchownSync(path: number, uid: number, gid: number): void;

function close(fd: number, req: FSReqCallback): void;
function close(fd: number, req: undefined, ctx: FSSyncContext): void;
Expand Down Expand Up @@ -240,6 +241,7 @@ export interface FsBinding {
copyFile: typeof InternalFSBinding.copyFile;
fchmod: typeof InternalFSBinding.fchmod;
fchown: typeof InternalFSBinding.fchown;
fchownSync: typeof InternalFSBinding.fchownSync;
fdatasync: typeof InternalFSBinding.fdatasync;
fstat: typeof InternalFSBinding.fstat;
fsync: typeof InternalFSBinding.fsync;
Expand Down
Loading