From e8ec898a7d8169bed73378b5cd4e677adbec16c8 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 14 Feb 2018 11:29:05 -0800 Subject: [PATCH] fs: use SyncCall in OpenFileHandle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/18871 Refs: https://github.com/nodejs/node/issues/18106 Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- src/node_file.cc | 27 +++++++++++++++++---------- test/parallel/test-fs-filehandle.js | 5 ++++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 8f9b71a238e..5e8f9ee168b 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1197,26 +1197,33 @@ static void Open(const FunctionCallbackInfo& args) { static void OpenFileHandle(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - Local context = env->context(); - CHECK_GE(args.Length(), 3); - CHECK(args[1]->IsInt32()); - CHECK(args[2]->IsInt32()); + const int argc = args.Length(); + CHECK_GE(argc, 3); BufferValue path(env->isolate(), args[0]); CHECK_NE(*path, nullptr); - int flags = args[1]->Int32Value(context).ToChecked(); - int mode = args[2]->Int32Value(context).ToChecked(); + CHECK(args[1]->IsInt32()); + const int flags = args[1].As()->Value(); + + CHECK(args[2]->IsInt32()); + const int mode = args[2].As()->Value(); FSReqBase* req_wrap = GetReqWrap(env, args[3]); - if (req_wrap != nullptr) { + if (req_wrap != nullptr) { // openFileHandle(path, flags, mode, req) AsyncCall(env, req_wrap, args, "open", UTF8, AfterOpenFileHandle, uv_fs_open, *path, flags, mode); - } else { - SYNC_CALL(open, *path, *path, flags, mode) + } else { // openFileHandle(path, flags, mode, undefined, ctx) + CHECK_EQ(argc, 5); + fs_req_wrap req_wrap; + int result = SyncCall(env, args[4], &req_wrap, "open", + uv_fs_open, *path, flags, mode); + if (result < 0) { + return; // syscall failed, no need to continue, error info is in ctx + } HandleScope scope(env->isolate()); - FileHandle* fd = new FileHandle(env, SYNC_RESULT); + FileHandle* fd = new FileHandle(env, result); args.GetReturnValue().Set(fd->object()); } } diff --git a/test/parallel/test-fs-filehandle.js b/test/parallel/test-fs-filehandle.js index 8b7de1a35b3..761193b6d29 100644 --- a/test/parallel/test-fs-filehandle.js +++ b/test/parallel/test-fs-filehandle.js @@ -2,6 +2,7 @@ 'use strict'; const common = require('../common'); +const assert = require('assert'); const path = require('path'); const fs = process.binding('fs'); const { stringToFlags } = require('internal/fs'); @@ -11,8 +12,10 @@ const { stringToFlags } = require('internal/fs'); let fdnum; { + const ctx = {}; fdnum = fs.openFileHandle(path.toNamespacedPath(__filename), - stringToFlags('r'), 0o666).fd; + stringToFlags('r'), 0o666, undefined, ctx).fd; + assert.strictEqual(ctx.errno, undefined); } common.expectWarning(