Skip to content

Commit 2aa3762

Browse files
jasnellnodejs-github-bot
authored andcommitted
fs: use GetAsyncReqWrap() in the remaining async/sync bindings
Replace the repeated `if (argc > N) { req = GetReqWrap(args, N); CHECK_NOT_NULL(req); ...` prologue of the fs bindings that select the asynchronous path by argument count with GetAsyncReqWrap(). Assisted-by: Claude Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #67497 Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 72d04a6 commit 2aa3762

1 file changed

Lines changed: 43 additions & 63 deletions

File tree

‎src/node_file.cc‎

Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,8 @@ void Access(const FunctionCallbackInfo<Value>& args) {
10661066
CHECK_NOT_NULL(*path);
10671067
ToNamespacedPath(env, &path);
10681068

1069-
if (argc > 2) { // access(path, mode, req)
1070-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1071-
CHECK_NOT_NULL(req_wrap_async);
1069+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
1070+
if (req_wrap_async != nullptr) { // access(path, mode, req)
10721071
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
10731072
env,
10741073
req_wrap_async,
@@ -1107,9 +1106,8 @@ void Close(const FunctionCallbackInfo<Value>& args) {
11071106
}
11081107
env->RemoveUnmanagedFd(fd);
11091108

1110-
if (argc > 1) { // close(fd, req)
1111-
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
1112-
CHECK_NOT_NULL(req_wrap_async);
1109+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1);
1110+
if (req_wrap_async != nullptr) { // close(fd, req)
11131111
FS_ASYNC_TRACE_BEGIN0(UV_FS_CLOSE, req_wrap_async)
11141112
AsyncCall(
11151113
env, req_wrap_async, args, "close", UTF8, AfterNoArgs, uv_fs_close, fd);
@@ -1421,9 +1419,8 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
14211419
CHECK(args[2]->IsInt32());
14221420
int flags = args[2].As<Int32>()->Value();
14231421

1424-
if (argc > 3) { // symlink(target, path, flags, req)
1425-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1426-
CHECK_NOT_NULL(req_wrap_async);
1422+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
1423+
if (req_wrap_async != nullptr) { // symlink(target, path, flags, req)
14271424
FS_ASYNC_TRACE_BEGIN2(UV_FS_SYMLINK,
14281425
req_wrap_async,
14291426
"target",
@@ -1529,9 +1526,8 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
15291526

15301527
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
15311528

1532-
if (argc > 2) { // readlink(path, encoding, req)
1533-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1534-
CHECK_NOT_NULL(req_wrap_async);
1529+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
1530+
if (req_wrap_async != nullptr) { // readlink(path, encoding, req)
15351531
FS_ASYNC_TRACE_BEGIN1(
15361532
UV_FS_READLINK, req_wrap_async, "path", TRACE_STR_COPY(*path))
15371533
AsyncCall(env,
@@ -1633,9 +1629,8 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
16331629
CHECK(IsSafeJsInt(args[1]));
16341630
const int64_t len = args[1].As<Integer>()->Value();
16351631

1636-
if (argc > 2) { // ftruncate(fd, len, req)
1637-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1638-
CHECK_NOT_NULL(req_wrap_async);
1632+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
1633+
if (req_wrap_async != nullptr) { // ftruncate(fd, len, req)
16391634
FS_ASYNC_TRACE_BEGIN0(UV_FS_FTRUNCATE, req_wrap_async)
16401635
AsyncCall(env,
16411636
req_wrap_async,
@@ -1665,9 +1660,8 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
16651660
return;
16661661
}
16671662

1668-
if (argc > 1) { // fdatasync(fd, req)
1669-
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
1670-
CHECK_NOT_NULL(req_wrap_async);
1663+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1);
1664+
if (req_wrap_async != nullptr) { // fdatasync(fd, req)
16711665
FS_ASYNC_TRACE_BEGIN0(UV_FS_FDATASYNC, req_wrap_async)
16721666
AsyncCall(env,
16731667
req_wrap_async,
@@ -1696,9 +1690,8 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) {
16961690
return;
16971691
}
16981692

1699-
if (argc > 1) {
1700-
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
1701-
CHECK_NOT_NULL(req_wrap_async);
1693+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1);
1694+
if (req_wrap_async != nullptr) {
17021695
FS_ASYNC_TRACE_BEGIN0(UV_FS_FSYNC, req_wrap_async)
17031696
AsyncCall(
17041697
env, req_wrap_async, args, "fsync", UTF8, AfterNoArgs, uv_fs_fsync, fd);
@@ -1757,9 +1750,8 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
17571750
THROW_IF_INSUFFICIENT_PERMISSIONS(
17581751
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
17591752

1760-
if (argc > 1) {
1761-
FSReqBase* req_wrap_async = GetReqWrap(args, 1); // rmdir(path, req)
1762-
CHECK_NOT_NULL(req_wrap_async);
1753+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1);
1754+
if (req_wrap_async != nullptr) { // rmdir(path, req)
17631755
FS_ASYNC_TRACE_BEGIN1(
17641756
UV_FS_RMDIR, req_wrap_async, "path", TRACE_STR_COPY(*path))
17651757
AsyncCall(env,
@@ -2141,9 +2133,8 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
21412133
CHECK(args[2]->IsBoolean());
21422134
bool mkdirp = args[2]->IsTrue();
21432135

2144-
if (argc > 3) { // mkdir(path, mode, recursive, req)
2145-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2146-
CHECK_NOT_NULL(req_wrap_async);
2136+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
2137+
if (req_wrap_async != nullptr) { // mkdir(path, mode, recursive, req)
21472138
FS_ASYNC_TRACE_BEGIN1(
21482139
UV_FS_UNLINK, req_wrap_async, "path", TRACE_STR_COPY(*path))
21492140
AsyncCall(env,
@@ -2771,9 +2762,9 @@ static void ReadDirRecursive(const FunctionCallbackInfo<Value>& args) {
27712762
// can do those: lib walks the tree in JS when the permission model is on.
27722763
CHECK(!env->permission()->enabled());
27732764

2774-
if (argc > 3) { // readdirRecursive(path, encoding, withTypes, req)
2775-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2776-
CHECK_NOT_NULL(req_wrap_async);
2765+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
2766+
if (req_wrap_async !=
2767+
nullptr) { // readdirRecursive(path, encoding, withTypes, req)
27772768
req_wrap_async->Init("scandir", nullptr, 0, encoding);
27782769
FS_ASYNC_TRACE_BEGIN1(
27792770
UV_FS_SCANDIR, req_wrap_async, "path", TRACE_STR_COPY(*path))
@@ -2886,9 +2877,8 @@ static void Open(const FunctionCallbackInfo<Value>& args) {
28862877
CHECK(args[2]->IsInt32());
28872878
const int mode = args[2].As<Int32>()->Value();
28882879

2889-
if (argc > 3) { // open(path, flags, mode, req)
2890-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2891-
CHECK_NOT_NULL(req_wrap_async);
2880+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
2881+
if (req_wrap_async != nullptr) { // open(path, flags, mode, req)
28922882
if (AsyncCheckOpenPermissions(env, req_wrap_async, path, flags).IsNothing())
28932883
return;
28942884
req_wrap_async->set_is_plain_open(true);
@@ -3148,9 +3138,8 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
31483138
iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk));
31493139
}
31503140

3151-
if (argc > 3) { // writeBuffers(fd, chunks, pos, req)
3152-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
3153-
CHECK_NOT_NULL(req_wrap_async);
3141+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
3142+
if (req_wrap_async != nullptr) { // writeBuffers(fd, chunks, pos, req)
31543143
FS_ASYNC_TRACE_BEGIN0(UV_FS_WRITE, req_wrap_async)
31553144
AsyncCall(env,
31563145
req_wrap_async,
@@ -3406,9 +3395,8 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
34063395
char* buf = buffer_data + off;
34073396
uv_buf_t uvbuf = uv_buf_init(buf, len);
34083397

3409-
if (argc > 5) { // read(fd, buffer, offset, len, pos, req)
3410-
FSReqBase* req_wrap_async = GetReqWrap(args, 5);
3411-
CHECK_NOT_NULL(req_wrap_async);
3398+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 5);
3399+
if (req_wrap_async != nullptr) { // read(fd, buffer, offset, len, pos, req)
34123400
FS_ASYNC_TRACE_BEGIN0(UV_FS_READ, req_wrap_async)
34133401
AsyncCall(env,
34143402
req_wrap_async,
@@ -4038,9 +4026,8 @@ static void ReadBuffers(const FunctionCallbackInfo<Value>& args) {
40384026
iovs[i] = uv_buf_init(Buffer::Data(buffer), Buffer::Length(buffer));
40394027
}
40404028

4041-
if (argc > 3) { // readBuffers(fd, buffers, pos, req)
4042-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
4043-
CHECK_NOT_NULL(req_wrap_async);
4029+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
4030+
if (req_wrap_async != nullptr) { // readBuffers(fd, buffers, pos, req)
40444031
FS_ASYNC_TRACE_BEGIN0(UV_FS_READ, req_wrap_async)
40454032
AsyncCall(env,
40464033
req_wrap_async,
@@ -4084,9 +4071,8 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
40844071
CHECK(args[1]->IsInt32());
40854072
int mode = args[1].As<Int32>()->Value();
40864073

4087-
if (argc > 2) { // chmod(path, mode, req)
4088-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
4089-
CHECK_NOT_NULL(req_wrap_async);
4074+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
4075+
if (req_wrap_async != nullptr) { // chmod(path, mode, req)
40904076
FS_ASYNC_TRACE_BEGIN1(
40914077
UV_FS_CHMOD, req_wrap_async, "path", TRACE_STR_COPY(*path))
40924078
AsyncCall(env,
@@ -4123,9 +4109,8 @@ static void FChmod(const FunctionCallbackInfo<Value>& args) {
41234109
CHECK(args[1]->IsInt32());
41244110
const int mode = args[1].As<Int32>()->Value();
41254111

4126-
if (argc > 2) { // fchmod(fd, mode, req)
4127-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
4128-
CHECK_NOT_NULL(req_wrap_async);
4112+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
4113+
if (req_wrap_async != nullptr) { // fchmod(fd, mode, req)
41294114
FS_ASYNC_TRACE_BEGIN0(UV_FS_FCHMOD, req_wrap_async)
41304115
AsyncCall(env,
41314116
req_wrap_async,
@@ -4210,9 +4195,8 @@ static void FChown(const FunctionCallbackInfo<Value>& args) {
42104195
CHECK(IsSafeJsInt(args[2]));
42114196
const auto gid = FromV8Value<uv_gid_t, true>(args[2]);
42124197

4213-
if (argc > 3) { // fchown(fd, uid, gid, req)
4214-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
4215-
CHECK_NOT_NULL(req_wrap_async);
4198+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
4199+
if (req_wrap_async != nullptr) { // fchown(fd, uid, gid, req)
42164200
FS_ASYNC_TRACE_BEGIN0(UV_FS_FCHOWN, req_wrap_async)
42174201
AsyncCall(env,
42184202
req_wrap_async,
@@ -4293,9 +4277,8 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
42934277
CHECK(args[2]->IsNumber());
42944278
const double mtime = args[2].As<Number>()->Value();
42954279

4296-
if (argc > 3) { // utimes(path, atime, mtime, req)
4297-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
4298-
CHECK_NOT_NULL(req_wrap_async);
4280+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
4281+
if (req_wrap_async != nullptr) { // utimes(path, atime, mtime, req)
42994282
FS_ASYNC_TRACE_BEGIN1(
43004283
UV_FS_UTIME, req_wrap_async, "path", TRACE_STR_COPY(*path))
43014284
AsyncCall(env,
@@ -4334,9 +4317,8 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
43344317
CHECK(args[2]->IsNumber());
43354318
const double mtime = args[2].As<Number>()->Value();
43364319

4337-
if (argc > 3) { // futimes(fd, atime, mtime, req)
4338-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
4339-
CHECK_NOT_NULL(req_wrap_async);
4320+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
4321+
if (req_wrap_async != nullptr) { // futimes(fd, atime, mtime, req)
43404322
FS_ASYNC_TRACE_BEGIN0(UV_FS_FUTIME, req_wrap_async)
43414323
AsyncCall(env,
43424324
req_wrap_async,
@@ -4375,9 +4357,8 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) {
43754357
CHECK(args[2]->IsNumber());
43764358
const double mtime = args[2].As<Number>()->Value();
43774359

4378-
if (argc > 3) { // lutimes(path, atime, mtime, req)
4379-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
4380-
CHECK_NOT_NULL(req_wrap_async);
4360+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3);
4361+
if (req_wrap_async != nullptr) { // lutimes(path, atime, mtime, req)
43814362
FS_ASYNC_TRACE_BEGIN1(
43824363
UV_FS_LUTIME, req_wrap_async, "path", TRACE_STR_COPY(*path))
43834364
AsyncCall(env,
@@ -4474,9 +4455,8 @@ static void Mkstemp(const FunctionCallbackInfo<Value>& args) {
44744455

44754456
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
44764457

4477-
if (argc > 2) { // mkstemp(tmpl, encoding, req)
4478-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
4479-
CHECK_NOT_NULL(req_wrap_async);
4458+
FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2);
4459+
if (req_wrap_async != nullptr) { // mkstemp(tmpl, encoding, req)
44804460
if (AsyncCheckOpenPermissions(env, req_wrap_async, tmpl, kMkstempFlags)
44814461
.IsNothing()) {
44824462
return;

0 commit comments

Comments
 (0)