Skip to content

Commit f766c04

Browse files
anonrigtargos
authored andcommitted
fs: improve error performance of chownSync
PR-URL: #49962 Refs: nodejs/performance#106 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent 610036c commit f766c04

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

benchmark/fs/bench-chownSync.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const assert = require('assert');
6+
const tmpdir = require('../../test/common/tmpdir');
7+
8+
if (process.platform === 'win32') {
9+
console.log('Skipping: Windows does not have `getuid` or `getgid`');
10+
process.exit(0);
11+
}
12+
13+
const bench = common.createBenchmark(main, {
14+
type: ['existing', 'non-existing'],
15+
method: ['chownSync', 'lchownSync'],
16+
n: [1e4],
17+
});
18+
19+
function main({ n, type, method }) {
20+
const uid = process.getuid();
21+
const gid = process.getgid();
22+
const fsMethod = fs[method];
23+
24+
switch (type) {
25+
case 'existing': {
26+
tmpdir.refresh();
27+
const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
28+
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');
29+
bench.start();
30+
for (let i = 0; i < n; i++) {
31+
fsMethod(tmpfile, uid, gid);
32+
}
33+
bench.end(n);
34+
break;
35+
}
36+
case 'non-existing': {
37+
const path = tmpdir.resolve(`.non-existing-file-${Date.now()}`);
38+
let hasError = false;
39+
bench.start();
40+
for (let i = 0; i < n; i++) {
41+
try {
42+
fs[method](path, uid, gid);
43+
} catch {
44+
hasError = true;
45+
}
46+
}
47+
bench.end(n);
48+
assert(hasError);
49+
break;
50+
}
51+
default:
52+
new Error('Invalid type');
53+
}
54+
}

lib/fs.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2089,9 +2089,11 @@ function chownSync(path, uid, gid) {
20892089
path = getValidatedPath(path);
20902090
validateInteger(uid, 'uid', -1, kMaxUserId);
20912091
validateInteger(gid, 'gid', -1, kMaxUserId);
2092-
const ctx = { path };
2093-
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
2094-
handleErrorFromBinding(ctx);
2092+
binding.chown(
2093+
pathModule.toNamespacedPath(path),
2094+
uid,
2095+
gid,
2096+
);
20952097
}
20962098

20972099
/**

src/node_file.cc

+5-7
Original file line numberDiff line numberDiff line change
@@ -2572,18 +2572,16 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
25722572
CHECK(IsSafeJsInt(args[2]));
25732573
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Integer>()->Value());
25742574

2575-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2576-
if (req_wrap_async != nullptr) { // chown(path, uid, gid, req)
2575+
if (argc > 3) { // chown(path, uid, gid, req)
2576+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
25772577
FS_ASYNC_TRACE_BEGIN1(
25782578
UV_FS_CHOWN, req_wrap_async, "path", TRACE_STR_COPY(*path))
25792579
AsyncCall(env, req_wrap_async, args, "chown", UTF8, AfterNoArgs,
25802580
uv_fs_chown, *path, uid, gid);
2581-
} else { // chown(path, uid, gid, undefined, ctx)
2582-
CHECK_EQ(argc, 5);
2583-
FSReqWrapSync req_wrap_sync;
2581+
} else { // chown(path, uid, gid)
2582+
FSReqWrapSync req_wrap_sync("chown", *path);
25842583
FS_SYNC_TRACE_BEGIN(chown);
2585-
SyncCall(env, args[4], &req_wrap_sync, "chown",
2586-
uv_fs_chown, *path, uid, gid);
2584+
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_chown, *path, uid, gid);
25872585
FS_SYNC_TRACE_END(chown);
25882586
}
25892587
}

typings/internalBinding/fs.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ declare namespace InternalFSBinding {
6767
function chown(path: string, uid: number, gid: number, req: FSReqCallback): void;
6868
function chown(path: string, uid: number, gid: number, req: undefined, ctx: FSSyncContext): void;
6969
function chown(path: string, uid: number, gid: number, usePromises: typeof kUsePromises): Promise<void>;
70+
function chown(path: string, uid: number, gid: number): void;
7071

7172
function close(fd: number, req: FSReqCallback): void;
7273
function close(fd: number, req: undefined, ctx: FSSyncContext): void;

0 commit comments

Comments
 (0)