Skip to content

Commit e8ec898

Browse files
committed
fs: use SyncCall in OpenFileHandle
PR-URL: #18871 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent fea5dda commit e8ec898

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/node_file.cc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,26 +1197,33 @@ static void Open(const FunctionCallbackInfo<Value>& args) {
11971197

11981198
static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
11991199
Environment* env = Environment::GetCurrent(args);
1200-
Local<Context> context = env->context();
12011200

1202-
CHECK_GE(args.Length(), 3);
1203-
CHECK(args[1]->IsInt32());
1204-
CHECK(args[2]->IsInt32());
1201+
const int argc = args.Length();
1202+
CHECK_GE(argc, 3);
12051203

12061204
BufferValue path(env->isolate(), args[0]);
12071205
CHECK_NE(*path, nullptr);
12081206

1209-
int flags = args[1]->Int32Value(context).ToChecked();
1210-
int mode = args[2]->Int32Value(context).ToChecked();
1207+
CHECK(args[1]->IsInt32());
1208+
const int flags = args[1].As<Int32>()->Value();
1209+
1210+
CHECK(args[2]->IsInt32());
1211+
const int mode = args[2].As<Int32>()->Value();
12111212

12121213
FSReqBase* req_wrap = GetReqWrap(env, args[3]);
1213-
if (req_wrap != nullptr) {
1214+
if (req_wrap != nullptr) { // openFileHandle(path, flags, mode, req)
12141215
AsyncCall(env, req_wrap, args, "open", UTF8, AfterOpenFileHandle,
12151216
uv_fs_open, *path, flags, mode);
1216-
} else {
1217-
SYNC_CALL(open, *path, *path, flags, mode)
1217+
} else { // openFileHandle(path, flags, mode, undefined, ctx)
1218+
CHECK_EQ(argc, 5);
1219+
fs_req_wrap req_wrap;
1220+
int result = SyncCall(env, args[4], &req_wrap, "open",
1221+
uv_fs_open, *path, flags, mode);
1222+
if (result < 0) {
1223+
return; // syscall failed, no need to continue, error info is in ctx
1224+
}
12181225
HandleScope scope(env->isolate());
1219-
FileHandle* fd = new FileHandle(env, SYNC_RESULT);
1226+
FileHandle* fd = new FileHandle(env, result);
12201227
args.GetReturnValue().Set(fd->object());
12211228
}
12221229
}

test/parallel/test-fs-filehandle.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33

44
const common = require('../common');
5+
const assert = require('assert');
56
const path = require('path');
67
const fs = process.binding('fs');
78
const { stringToFlags } = require('internal/fs');
@@ -11,8 +12,10 @@ const { stringToFlags } = require('internal/fs');
1112

1213
let fdnum;
1314
{
15+
const ctx = {};
1416
fdnum = fs.openFileHandle(path.toNamespacedPath(__filename),
15-
stringToFlags('r'), 0o666).fd;
17+
stringToFlags('r'), 0o666, undefined, ctx).fd;
18+
assert.strictEqual(ctx.errno, undefined);
1619
}
1720

1821
common.expectWarning(

0 commit comments

Comments
 (0)