From d5196d38cecb2acadb6e204141f39e29e7135a44 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 24 Jan 2018 09:37:52 +0800 Subject: [PATCH] fs: throw errors from fs.fdatasyncSync in JS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/18348 Refs: https://github.com/nodejs/node/issues/18106 Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell --- lib/fs.js | 6 +++++- src/node_file.cc | 17 +++++++++++------ test/parallel/test-fs-error-messages.js | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 8eaba7984cca16..49e550fe3d794a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1029,7 +1029,11 @@ fs.fdatasync = function(fd, callback) { fs.fdatasyncSync = function(fd) { validateUint32(fd, 'fd'); - return binding.fdatasync(fd); + const ctx = {}; + binding.fdatasync(fd, undefined, ctx); + if (ctx.errno !== undefined) { + throw new errors.uvException(ctx); + } }; fs.fsync = function(fd, callback) { diff --git a/src/node_file.cc b/src/node_file.cc index 10149b403e29b2..6188da24488fd9 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -735,16 +735,21 @@ static void FTruncate(const FunctionCallbackInfo& args) { static void Fdatasync(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - CHECK(args[0]->IsInt32()); + const int argc = args.Length(); + CHECK_GE(argc, 2); - int fd = args[0]->Int32Value(); + CHECK(args[0]->IsInt32()); + const int fd = args[0]->As()->Value(); - if (args[1]->IsObject()) { - CHECK_EQ(args.Length(), 2); + if (args[1]->IsObject()) { // fdatasync(fd, req) + CHECK_EQ(argc, 2); AsyncCall(env, args, "fdatasync", UTF8, AfterNoArgs, uv_fs_fdatasync, fd); - } else { - SYNC_CALL(fdatasync, 0, fd) + } else { // fdatasync(fd, undefined, ctx) + CHECK_EQ(argc, 3); + fs_req_wrap req_wrap; + SyncCall(env, args[2], &req_wrap, "fdatasync", + uv_fs_fdatasync, fd); } } diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index b7e7ab6a2cc089..116c8537242e73 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -482,3 +482,24 @@ function re(literals, ...values) { validateError ); } + +// fdatasync +{ + const validateError = (err) => { + assert.strictEqual(err.message, 'EBADF: bad file descriptor, fdatasync'); + assert.strictEqual(err.errno, uv.UV_EBADF); + assert.strictEqual(err.code, 'EBADF'); + assert.strictEqual(err.syscall, 'fdatasync'); + return true; + }; + + const fd = fs.openSync(existingFile, 'r'); + fs.closeSync(fd); + + fs.fdatasync(fd, common.mustCall(validateError)); + + assert.throws( + () => fs.fdatasyncSync(fd), + validateError + ); +}