From e4474af0a084065dcb4a53216e0df4daa3768ad6 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 28 Feb 2018 01:21:19 +0800 Subject: [PATCH 1/7] fs: throw readSync errors in JS --- lib/fs.js | 6 ++- src/node_file.cc | 51 ++++++++++++------------- test/parallel/test-fs-error-messages.js | 21 ++++++++++ 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 510369e44fd4af..95c32b4b71af35 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -577,7 +577,11 @@ fs.readSync = function(fd, buffer, offset, length, position) { if (!isUint32(position)) position = -1; - return binding.read(fd, buffer, offset, length, position); + const ctx = {}; + const result = binding.read(fd, buffer, offset, length, position, + undefined, ctx); + handleErrorFromBinding(ctx); + return result; }; // usage: diff --git a/src/node_file.cc b/src/node_file.cc index 00fe1f178bedc3..617264e096c568 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -110,7 +110,7 @@ using v8::Value; # define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif -#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1) +#define GET_OFFSET(a) ((a)->IsNumber() ? (a).As()->Value() : -1) // The FileHandle object wraps a file descriptor and will close it on garbage // collection if necessary. If that happens, a process warning will be @@ -1411,51 +1411,50 @@ static void WriteString(const FunctionCallbackInfo& args) { * * bytesRead = fs.read(fd, buffer, offset, length, position) * - * 0 fd integer. file descriptor + * 0 fd int32. file descriptor * 1 buffer instance of Buffer - * 2 offset integer. offset to start reading into inside buffer - * 3 length integer. length to read - * 4 position file position - null for current position - * + * 2 offset int32. offset to start reading into inside buffer + * 3 length int32. length to read + * 4 position int64. file position - -1 for current position */ static void Read(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - CHECK(args[0]->IsInt32()); - CHECK(Buffer::HasInstance(args[1])); - - int fd = args[0]->Int32Value(); - - Local req; - - size_t len; - int64_t pos; + const int argc = args.Length(); + CHECK_GE(argc, 4); - char * buf = nullptr; + CHECK(args[0]->IsInt32()); + const int fd = args[0].As()->Value(); + CHECK(Buffer::HasInstance(args[1])); Local buffer_obj = args[1].As(); - char *buffer_data = Buffer::Data(buffer_obj); + char* buffer_data = Buffer::Data(buffer_obj); size_t buffer_length = Buffer::Length(buffer_obj); - size_t off = args[2]->Int32Value(); + CHECK(args[2]->IsInt32()); + const size_t off = static_cast(args[2].As()->Value()); CHECK_LT(off, buffer_length); - len = args[3]->Int32Value(); + CHECK(args[3]->IsInt32()); + const size_t len = static_cast(args[3].As()->Value()); CHECK(Buffer::IsWithinBounds(off, len, buffer_length)); - pos = GET_OFFSET(args[4]); - - buf = buffer_data + off; + CHECK(args[4]->IsNumber()); + const int64_t pos = args[4].As()->Value(); + char* buf = buffer_data + off; uv_buf_t uvbuf = uv_buf_init(const_cast(buf), len); FSReqBase* req_wrap = GetReqWrap(env, args[5]); - if (req_wrap != nullptr) { + if (req_wrap != nullptr) { // read(fd, buffer, offset, len, pos, req) AsyncCall(env, req_wrap, args, "read", UTF8, AfterInteger, uv_fs_read, fd, &uvbuf, 1, pos); - } else { - SYNC_CALL(read, 0, fd, &uvbuf, 1, pos) - args.GetReturnValue().Set(SYNC_RESULT); + } else { // read(fd, buffer, offset, len, pos, undefined, ctx) + CHECK_EQ(argc, 7); + fs_req_wrap req_wrap; + const int bytesRead = SyncCall(env, args[6], &req_wrap, "read", + uv_fs_read, fd, &uvbuf, 1, pos); + args.GetReturnValue().Set(bytesRead); } } diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index 6718e6fe660803..dc925069ba01eb 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -708,3 +708,24 @@ if (!common.isAIX) { validateError ); } + +// read +{ + const validateError = (err) => { + assert.strictEqual(err.message, 'EBADF: bad file descriptor, read'); + assert.strictEqual(err.errno, uv.UV_EBADF); + assert.strictEqual(err.code, 'EBADF'); + assert.strictEqual(err.syscall, 'read'); + return true; + }; + + common.runWithInvalidFD((fd) => { + const buf = Buffer.alloc(5); + fs.read(fd, buf, 0, 1, 1, common.mustCall(validateError)); + + assert.throws( + () => fs.readSync(fd, buf, 0, 1, 1), + validateError + ); + }); +} From 5660ccf8bf8b5e792e14bc61a5d2bb2e76d01e1a Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 28 Feb 2018 01:33:31 +0800 Subject: [PATCH 2/7] fs: throw fchmodSync errors in JS --- lib/fs.js | 4 +++- src/node_file.cc | 18 ++++++++++++------ test/parallel/test-fs-error-messages.js | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 95c32b4b71af35..2cafcefc53d3af 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1004,7 +1004,9 @@ fs.fchmodSync = function(fd, mode) { validateUint32(mode, 'mode'); if (mode < 0 || mode > 0o777) throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode'); - return binding.fchmod(fd, mode); + const ctx = {}; + binding.fchmod(fd, mode, undefined, ctx); + handleErrorFromBinding(ctx); }; if (constants.O_SYMLINK !== undefined) { diff --git a/src/node_file.cc b/src/node_file.cc index 617264e096c568..bb38fc3878f71d 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1493,18 +1493,24 @@ static void Chmod(const FunctionCallbackInfo& args) { static void FChmod(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + const int argc = args.Length(); + CHECK_GE(argc, 2); + CHECK(args[0]->IsInt32()); - CHECK(args[1]->IsInt32()); + const int fd = args[0].As()->Value(); - int fd = args[0]->Int32Value(); - int mode = static_cast(args[1]->Int32Value()); + CHECK(args[1]->IsInt32()); + const int mode = args[1].As()->Value(); FSReqBase* req_wrap = GetReqWrap(env, args[2]); - if (req_wrap != nullptr) { + if (req_wrap != nullptr) { // fchmod(fd, mode, req) AsyncCall(env, req_wrap, args, "fchmod", UTF8, AfterNoArgs, uv_fs_fchmod, fd, mode); - } else { - SYNC_CALL(fchmod, 0, fd, mode); + } else { // fchmod(fd, mode, undefined, ctx) + CHECK_EQ(argc, 4); + fs_req_wrap req_wrap; + SyncCall(env, args[3], &req_wrap, "fchmod", + uv_fs_fchmod, fd, mode); } } diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index dc925069ba01eb..d14a5b8effcd45 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -729,3 +729,23 @@ if (!common.isAIX) { ); }); } + +// fchmod +{ + const validateError = (err) => { + assert.strictEqual(err.message, 'EBADF: bad file descriptor, fchmod'); + assert.strictEqual(err.errno, uv.UV_EBADF); + assert.strictEqual(err.code, 'EBADF'); + assert.strictEqual(err.syscall, 'fchmod'); + return true; + }; + + common.runWithInvalidFD((fd) => { + fs.fchmod(fd, 0o666, common.mustCall(validateError)); + + assert.throws( + () => fs.fchmodSync(fd, 0o666), + validateError + ); + }); +} From fbdd350a21e5f55bd87cc33634bee95ca69eb6f2 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 28 Feb 2018 01:45:17 +0800 Subject: [PATCH 3/7] fs: throw fchownSync errors in JS --- lib/fs.js | 4 +++- src/node_file.cc | 21 ++++++++++++++------- test/parallel/test-fs-error-messages.js | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 2cafcefc53d3af..0671a05ae00681 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1110,7 +1110,9 @@ fs.fchownSync = function(fd, uid, gid) { validateUint32(uid, 'uid'); validateUint32(gid, 'gid'); - return binding.fchown(fd, uid, gid); + const ctx = {}; + binding.fchown(fd, uid, gid, undefined, ctx); + handleErrorFromBinding(ctx); }; fs.chown = function(path, uid, gid, callback) { diff --git a/src/node_file.cc b/src/node_file.cc index bb38fc3878f71d..3a6b28907ab936 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1552,20 +1552,27 @@ static void Chown(const FunctionCallbackInfo& args) { static void FChown(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + const int argc = args.Length(); + CHECK_GE(argc, 3); + CHECK(args[0]->IsInt32()); + const int fd = args[0].As()->Value(); + CHECK(args[1]->IsUint32()); - CHECK(args[2]->IsUint32()); + const uv_uid_t uid = static_cast(args[1].As()->Value()); - int fd = args[0]->Int32Value(); - uv_uid_t uid = static_cast(args[1]->Uint32Value()); - uv_gid_t gid = static_cast(args[2]->Uint32Value()); + CHECK(args[2]->IsUint32()); + const uv_gid_t gid = static_cast(args[2].As()->Value()); FSReqBase* req_wrap = GetReqWrap(env, args[3]); - if (req_wrap != nullptr) { + if (req_wrap != nullptr) { // fchown(fd, uid, gid, req) AsyncCall(env, req_wrap, args, "fchown", UTF8, AfterNoArgs, uv_fs_fchown, fd, uid, gid); - } else { - SYNC_CALL(fchown, 0, fd, uid, gid); + } else { // fchown(fd, uid, gid, undefined, ctx) + CHECK_EQ(argc, 5); + fs_req_wrap req_wrap; + SyncCall(env, args[4], &req_wrap, "fchown", + uv_fs_fchown, fd, uid, gid); } } diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index d14a5b8effcd45..1aa4e1229499e6 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -749,3 +749,24 @@ if (!common.isAIX) { ); }); } + +// fchown +if (!common.isWindows) { + const validateError = (err) => { + assert.strictEqual(err.message, 'EBADF: bad file descriptor, fchown'); + assert.strictEqual(err.errno, uv.UV_EBADF); + assert.strictEqual(err.code, 'EBADF'); + assert.strictEqual(err.syscall, 'fchown'); + return true; + }; + + common.runWithInvalidFD((fd) => { + fs.fchown(fd, process.getuid(), process.getgid(), + common.mustCall(validateError)); + + assert.throws( + () => fs.fchownSync(fd, process.getuid(), process.getgid()), + validateError + ); + }); +} From fbc8679dca227fe966bbca6fe1fac597e3123ad7 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 28 Feb 2018 02:17:12 +0800 Subject: [PATCH 4/7] fs: throw writeSync errors in JS --- lib/fs.js | 19 +++++--- src/node_file.cc | 63 +++++++++++++++---------- test/parallel/test-fs-error-messages.js | 41 ++++++++++++++++ 3 files changed, 93 insertions(+), 30 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 0671a05ae00681..2c96b08144bc03 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -635,6 +635,8 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs, // fs.writeSync(fd, string[, position[, encoding]]); fs.writeSync = function(fd, buffer, offset, length, position) { validateUint32(fd, 'fd'); + const ctx = {}; + let result; if (isUint8Array(buffer)) { if (position === undefined) position = null; @@ -643,13 +645,18 @@ fs.writeSync = function(fd, buffer, offset, length, position) { if (typeof length !== 'number') length = buffer.length - offset; validateOffsetLengthWrite(offset, length, buffer.byteLength); - return binding.writeBuffer(fd, buffer, offset, length, position); + result = binding.writeBuffer(fd, buffer, offset, length, position, + undefined, ctx); + } else { + if (typeof buffer !== 'string') + buffer += ''; + if (offset === undefined) + offset = null; + result = binding.writeString(fd, buffer, offset, length, + undefined, ctx); } - if (typeof buffer !== 'string') - buffer += ''; - if (offset === undefined) - offset = null; - return binding.writeString(fd, buffer, offset, length, position); + handleErrorFromBinding(ctx); + return result; }; fs.rename = function(oldPath, newPath, callback) { diff --git a/src/node_file.cc b/src/node_file.cc index 3a6b28907ab936..b6879368cad6a4 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1270,35 +1270,43 @@ static void CopyFile(const FunctionCallbackInfo& args) { static void WriteBuffer(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + const int argc = args.Length(); + CHECK_GE(argc, 4); + CHECK(args[0]->IsInt32()); - CHECK(Buffer::HasInstance(args[1])); + const int fd = args[0].As()->Value(); - int fd = args[0]->Int32Value(); - Local obj = args[1].As(); - const char* buf = Buffer::Data(obj); - size_t buffer_length = Buffer::Length(obj); - size_t off = args[2]->Uint32Value(); - size_t len = args[3]->Uint32Value(); - int64_t pos = GET_OFFSET(args[4]); + CHECK(Buffer::HasInstance(args[1])); + Local buffer_obj = args[1].As(); + char* buffer_data = Buffer::Data(buffer_obj); + size_t buffer_length = Buffer::Length(buffer_obj); + CHECK(args[2]->IsInt32()); + const size_t off = static_cast(args[2].As()->Value()); CHECK_LE(off, buffer_length); + + CHECK(args[3]->IsInt32()); + const size_t len = static_cast(args[3].As()->Value()); + CHECK(Buffer::IsWithinBounds(off, len, buffer_length)); CHECK_LE(len, buffer_length); CHECK_GE(off + len, off); - CHECK(Buffer::IsWithinBounds(off, len, buffer_length)); - buf += off; + const int64_t pos = GET_OFFSET(args[4]); + char* buf = buffer_data + off; uv_buf_t uvbuf = uv_buf_init(const_cast(buf), len); FSReqBase* req_wrap = GetReqWrap(env, args[5]); - if (req_wrap != nullptr) { + if (req_wrap != nullptr) { // write(fd, buffer, off, len, pos, req) AsyncCall(env, req_wrap, args, "write", UTF8, AfterInteger, uv_fs_write, fd, &uvbuf, 1, pos); - return; + } else { // write(fd, buffer, off, len, pos, undefined, ctx) + CHECK_EQ(argc, 7); + fs_req_wrap req_wrap; + int bytesWritten = SyncCall(env, args[6], &req_wrap, "write", + uv_fs_write, fd, &uvbuf, 1, pos); + args.GetReturnValue().Set(bytesWritten); } - - SYNC_CALL(write, nullptr, fd, &uvbuf, 1, pos) - args.GetReturnValue().Set(SYNC_RESULT); } @@ -1350,19 +1358,23 @@ static void WriteBuffers(const FunctionCallbackInfo& args) { static void WriteString(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + const int argc = args.Length(); + CHECK_GE(argc, 4); + CHECK(args[0]->IsInt32()); + const int fd = args[0].As()->Value(); + + const int64_t pos = GET_OFFSET(args[2]); + + const auto enc = ParseEncoding(env->isolate(), args[3], UTF8); std::unique_ptr delete_on_return; - Local req; Local value = args[1]; - int fd = args[0]->Int32Value(); char* buf = nullptr; size_t len; - const int64_t pos = GET_OFFSET(args[2]); - const auto enc = ParseEncoding(env->isolate(), args[3], UTF8); FSReqBase* req_wrap = GetReqWrap(env, args[4]); - const auto is_async = req_wrap != nullptr; + const bool is_async = req_wrap != nullptr; // Avoid copying the string when it is externalized but only when: // 1. The target encoding is compatible with the string's encoding, and @@ -1396,12 +1408,15 @@ static void WriteString(const FunctionCallbackInfo& args) { uv_buf_t uvbuf = uv_buf_init(buf, len); - if (req_wrap != nullptr) { + if (is_async) { // write(fd, string, pos, enc, req) AsyncCall(env, req_wrap, args, "write", UTF8, AfterInteger, uv_fs_write, fd, &uvbuf, 1, pos); - } else { - SYNC_CALL(write, nullptr, fd, &uvbuf, 1, pos) - return args.GetReturnValue().Set(SYNC_RESULT); + } else { // write(fd, string, pos, enc, undefined, ctx) + CHECK_EQ(argc, 6); + fs_req_wrap req_wrap; + int bytesWritten = SyncCall(env, args[5], &req_wrap, "write", + uv_fs_write, fd, &uvbuf, 1, pos); + args.GetReturnValue().Set(bytesWritten); } } diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index 1aa4e1229499e6..27533f122fc5f5 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -770,3 +770,44 @@ if (!common.isWindows) { ); }); } + +// write buffer +{ + const validateError = (err) => { + assert.strictEqual(err.message, 'EBADF: bad file descriptor, write'); + assert.strictEqual(err.errno, uv.UV_EBADF); + assert.strictEqual(err.code, 'EBADF'); + assert.strictEqual(err.syscall, 'write'); + return true; + }; + + common.runWithInvalidFD((fd) => { + const buf = Buffer.alloc(5); + fs.write(fd, buf, 0, 1, 1, common.mustCall(validateError)); + + assert.throws( + () => fs.writeSync(fd, buf, 0, 1, 1), + validateError + ); + }); +} + +// write string +{ + const validateError = (err) => { + assert.strictEqual(err.message, 'EBADF: bad file descriptor, write'); + assert.strictEqual(err.errno, uv.UV_EBADF); + assert.strictEqual(err.code, 'EBADF'); + assert.strictEqual(err.syscall, 'write'); + return true; + }; + + common.runWithInvalidFD((fd) => { + fs.write(fd, 'test', 1, common.mustCall(validateError)); + + assert.throws( + () => fs.writeSync(fd, 'test', 1), + validateError + ); + }); +} From 4d80e225f8aaaf22a8c6f9f56cb2e311981c3afb Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 28 Feb 2018 02:25:02 +0800 Subject: [PATCH 5/7] fs: throw futimesSync errors in JS --- lib/fs.js | 4 +++- src/node_file.cc | 21 ++++++++++++++------- test/parallel/test-fs-error-messages.js | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 2c96b08144bc03..2dcfb25348fe8c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1183,7 +1183,9 @@ fs.futimesSync = function(fd, atime, mtime) { validateUint32(fd, 'fd'); atime = toUnixTimestamp(atime, 'atime'); mtime = toUnixTimestamp(mtime, 'mtime'); - binding.futimes(fd, atime, mtime); + const ctx = {}; + binding.futimes(fd, atime, mtime, undefined, ctx); + handleErrorFromBinding(ctx); }; function writeAll(fd, isUserFd, buffer, offset, length, position, callback) { diff --git a/src/node_file.cc b/src/node_file.cc index b6879368cad6a4..231ee7a40c7e58 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1622,20 +1622,27 @@ static void UTimes(const FunctionCallbackInfo& args) { static void FUTimes(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + const int argc = args.Length(); + CHECK_GE(argc, 3); + CHECK(args[0]->IsInt32()); + const int fd = args[0].As()->Value(); + CHECK(args[1]->IsNumber()); - CHECK(args[2]->IsNumber()); + const double atime = args[1].As()->Value(); - const int fd = args[0]->Int32Value(); - const double atime = static_cast(args[1]->NumberValue()); - const double mtime = static_cast(args[2]->NumberValue()); + CHECK(args[2]->IsNumber()); + const double mtime = args[2].As()->Value(); FSReqBase* req_wrap = GetReqWrap(env, args[3]); - if (req_wrap != nullptr) { + if (req_wrap != nullptr) { // futimes(fd, atime, mtime, req) AsyncCall(env, req_wrap, args, "futime", UTF8, AfterNoArgs, uv_fs_futime, fd, atime, mtime); - } else { - SYNC_CALL(futime, 0, fd, atime, mtime); + } else { // futimes(fd, atime, mtime, undefined, ctx) + CHECK_EQ(argc, 5); + fs_req_wrap req_wrap; + SyncCall(env, args[4], &req_wrap, "futime", + uv_fs_futime, fd, atime, mtime); } } diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index 27533f122fc5f5..61e51585a028c2 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -811,3 +811,24 @@ if (!common.isWindows) { ); }); } + + +// futimes +if (!common.isAIX) { + const validateError = (err) => { + assert.strictEqual(err.message, 'EBADF: bad file descriptor, futime'); + assert.strictEqual(err.errno, uv.UV_EBADF); + assert.strictEqual(err.code, 'EBADF'); + assert.strictEqual(err.syscall, 'futime'); + return true; + }; + + common.runWithInvalidFD((fd) => { + fs.futimes(fd, new Date(), new Date(), common.mustCall(validateError)); + + assert.throws( + () => fs.futimesSync(fd, new Date(), new Date()), + validateError + ); + }); +} From 787851c67fea8ebc92e13358041d2640ca98b47e Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 28 Feb 2018 02:36:43 +0800 Subject: [PATCH 6/7] fs: use SyncCall in WriteBuffers --- src/node_file.cc | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 231ee7a40c7e58..5577969443f45d 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1320,11 +1320,15 @@ static void WriteBuffer(const FunctionCallbackInfo& args) { static void WriteBuffers(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + const int argc = args.Length(); + CHECK_GE(argc, 3); + CHECK(args[0]->IsInt32()); - CHECK(args[1]->IsArray()); + const int fd = args[0].As()->Value(); - int fd = args[0]->Int32Value(); + CHECK(args[1]->IsArray()); Local chunks = args[1].As(); + int64_t pos = GET_OFFSET(args[2]); MaybeStackBuffer iovs(chunks->Length()); @@ -1336,14 +1340,16 @@ static void WriteBuffers(const FunctionCallbackInfo& args) { } FSReqBase* req_wrap = GetReqWrap(env, args[3]); - if (req_wrap != nullptr) { + if (req_wrap != nullptr) { // writeBuffers(fd, chunks, pos, req) AsyncCall(env, req_wrap, args, "write", UTF8, AfterInteger, uv_fs_write, fd, *iovs, iovs.length(), pos); - return; + } else { // writeBuffers(fd, chunks, pos, undefined, ctx) + CHECK_EQ(argc, 5); + fs_req_wrap req_wrap; + int bytesWritten = SyncCall(env, args[4], &req_wrap, "write", + uv_fs_write, fd, *iovs, iovs.length(), pos); + args.GetReturnValue().Set(bytesWritten); } - - SYNC_CALL(write, nullptr, fd, *iovs, iovs.length(), pos) - args.GetReturnValue().Set(SYNC_RESULT); } From 1e71351f79991f05e9655e1f692f992b662f7572 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 28 Feb 2018 02:41:27 +0800 Subject: [PATCH 7/7] fs: remove unused SYNC_* helpers --- src/node_file.cc | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 5577969443f45d..00c60ff5346268 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -575,24 +575,6 @@ inline int SyncCall(Environment* env, Local ctx, fs_req_wrap* req_wrap, return err; } -#define SYNC_DEST_CALL(func, path, dest, ...) \ - fs_req_wrap sync_wrap; \ - env->PrintSyncTrace(); \ - int err = uv_fs_ ## func(env->event_loop(), \ - &sync_wrap.req, \ - __VA_ARGS__, \ - nullptr); \ - if (err < 0) { \ - return env->ThrowUVException(err, #func, nullptr, path, dest); \ - } \ - -#define SYNC_CALL(func, path, ...) \ - SYNC_DEST_CALL(func, path, nullptr, __VA_ARGS__) \ - -#define SYNC_REQ sync_wrap.req - -#define SYNC_RESULT err - inline FSReqBase* GetReqWrap(Environment* env, Local value) { if (value->IsObject()) { return Unwrap(value.As());