Skip to content

Commit

Permalink
fs: use SyncCall in OpenFileHandle
Browse files Browse the repository at this point in the history
PR-URL: #18871
Refs: #18106
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
joyeecheung committed Feb 27, 2018
1 parent fea5dda commit e8ec898
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
27 changes: 17 additions & 10 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1197,26 +1197,33 @@ static void Open(const FunctionCallbackInfo<Value>& args) {

static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Context> 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<Int32>()->Value();

CHECK(args[2]->IsInt32());
const int mode = args[2].As<Int32>()->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());
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-fs-filehandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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(
Expand Down

0 comments on commit e8ec898

Please sign in to comment.