diff --git a/lib/fs.js b/lib/fs.js index 8222405d6fba2a..de2576d6bf6515 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1045,7 +1045,11 @@ fs.fsync = function(fd, callback) { fs.fsyncSync = function(fd) { validateUint32(fd, 'fd'); - return binding.fsync(fd); + const ctx = {}; + binding.fsync(fd, undefined, ctx); + if (ctx.errno !== undefined) { + throw new errors.uvException(ctx); + } }; fs.mkdir = function(path, mode, callback) { diff --git a/src/node_file.cc b/src/node_file.cc index 6188da24488fd9..3ffec4225b216c 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -739,7 +739,7 @@ static void Fdatasync(const FunctionCallbackInfo& args) { CHECK_GE(argc, 2); CHECK(args[0]->IsInt32()); - const int fd = args[0]->As()->Value(); + const int fd = args[0].As()->Value(); if (args[1]->IsObject()) { // fdatasync(fd, req) CHECK_EQ(argc, 2); @@ -756,16 +756,21 @@ static void Fdatasync(const FunctionCallbackInfo& args) { static void Fsync(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()) { // fsync(fd, req) + CHECK_EQ(argc, 2); AsyncCall(env, args, "fsync", UTF8, AfterNoArgs, uv_fs_fsync, fd); - } else { - SYNC_CALL(fsync, 0, fd) + } else { // fsync(fd, undefined, ctx) + CHECK_EQ(argc, 3); + fs_req_wrap req_wrap; + SyncCall(env, args[2], &req_wrap, "fsync", + uv_fs_fsync, fd); } } diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index 2da0c1a31257eb..d44101218f3d3f 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -497,3 +497,24 @@ function re(literals, ...values) { validateError ); } + +// fsync +{ + const validateError = (err) => { + assert.strictEqual(err.message, 'EBADF: bad file descriptor, fsync'); + assert.strictEqual(err.errno, uv.UV_EBADF); + assert.strictEqual(err.code, 'EBADF'); + assert.strictEqual(err.syscall, 'fsync'); + return true; + }; + + const fd = fs.openSync(existingFile, 'r'); + fs.closeSync(fd); + + fs.fsync(fd, common.mustCall(validateError)); + + assert.throws( + () => fs.fsyncSync(fd), + validateError + ); +}