Skip to content

Commit ea7902d

Browse files
anonrigtargos
authored andcommitted
fs: improve error performance of chownSync
PR-URL: #49962 Refs: nodejs/performance#106 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent 39f31a3 commit ea7902d

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
@@ -2087,9 +2087,11 @@ function chownSync(path, uid, gid) {
20872087
path = getValidatedPath(path);
20882088
validateInteger(uid, 'uid', -1, kMaxUserId);
20892089
validateInteger(gid, 'gid', -1, kMaxUserId);
2090-
const ctx = { path };
2091-
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
2092-
handleErrorFromBinding(ctx);
2090+
binding.chown(
2091+
pathModule.toNamespacedPath(path),
2092+
uid,
2093+
gid,
2094+
);
20932095
}
20942096

20952097
/**

src/node_file.cc

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

2610-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2611-
if (req_wrap_async != nullptr) { // chown(path, uid, gid, req)
2610+
if (argc > 3) { // chown(path, uid, gid, req)
2611+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
26122612
FS_ASYNC_TRACE_BEGIN1(
26132613
UV_FS_CHOWN, req_wrap_async, "path", TRACE_STR_COPY(*path))
26142614
AsyncCall(env, req_wrap_async, args, "chown", UTF8, AfterNoArgs,
26152615
uv_fs_chown, *path, uid, gid);
2616-
} else { // chown(path, uid, gid, undefined, ctx)
2617-
CHECK_EQ(argc, 5);
2618-
FSReqWrapSync req_wrap_sync;
2616+
} else { // chown(path, uid, gid)
2617+
FSReqWrapSync req_wrap_sync("chown", *path);
26192618
FS_SYNC_TRACE_BEGIN(chown);
2620-
SyncCall(env, args[4], &req_wrap_sync, "chown",
2621-
uv_fs_chown, *path, uid, gid);
2619+
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_chown, *path, uid, gid);
26222620
FS_SYNC_TRACE_END(chown);
26232621
}
26242622
}

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)