Skip to content

Commit

Permalink
fs: throw futimesSync errors in JS
Browse files Browse the repository at this point in the history
PR-URL: #19041
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
joyeecheung committed Mar 2, 2018
1 parent 994320b commit 49dd809
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 14 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1622,20 +1622,27 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
static void FUTimes(const FunctionCallbackInfo<Value>& 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<Int32>()->Value();

CHECK(args[1]->IsNumber());
CHECK(args[2]->IsNumber());
const double atime = args[1].As<Number>()->Value();

const int fd = args[0]->Int32Value();
const double atime = static_cast<double>(args[1]->NumberValue());
const double mtime = static_cast<double>(args[2]->NumberValue());
CHECK(args[2]->IsNumber());
const double mtime = args[2].As<Number>()->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);
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
}

0 comments on commit 49dd809

Please sign in to comment.