diff --git a/lib/fs.js b/lib/fs.js index 510369e44fd4af..2dcfb25348fe8c 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: @@ -631,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; @@ -639,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) { @@ -1000,7 +1011,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) { @@ -1104,7 +1117,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) { @@ -1168,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 00fe1f178bedc3..00c60ff5346268 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 @@ -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()); @@ -1270,35 +1252,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); } @@ -1312,11 +1302,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()); @@ -1328,14 +1322,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); } @@ -1350,19 +1346,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 +1396,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); } } @@ -1411,51 +1414,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); } } @@ -1494,18 +1496,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); } } @@ -1547,20 +1555,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); } } @@ -1595,20 +1610,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 6718e6fe660803..61e51585a028c2 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -708,3 +708,127 @@ 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 + ); + }); +} + +// 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 + ); + }); +} + +// 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 + ); + }); +} + +// 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 + ); + }); +} + + +// 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 + ); + }); +}