Skip to content

Commit 49dd809

Browse files
committed
fs: throw futimesSync errors in JS
PR-URL: #19041 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 994320b commit 49dd809

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

lib/fs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,9 @@ fs.futimesSync = function(fd, atime, mtime) {
11831183
validateUint32(fd, 'fd');
11841184
atime = toUnixTimestamp(atime, 'atime');
11851185
mtime = toUnixTimestamp(mtime, 'mtime');
1186-
binding.futimes(fd, atime, mtime);
1186+
const ctx = {};
1187+
binding.futimes(fd, atime, mtime, undefined, ctx);
1188+
handleErrorFromBinding(ctx);
11871189
};
11881190

11891191
function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {

src/node_file.cc

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,20 +1622,27 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
16221622
static void FUTimes(const FunctionCallbackInfo<Value>& args) {
16231623
Environment* env = Environment::GetCurrent(args);
16241624

1625+
const int argc = args.Length();
1626+
CHECK_GE(argc, 3);
1627+
16251628
CHECK(args[0]->IsInt32());
1629+
const int fd = args[0].As<Int32>()->Value();
1630+
16261631
CHECK(args[1]->IsNumber());
1627-
CHECK(args[2]->IsNumber());
1632+
const double atime = args[1].As<Number>()->Value();
16281633

1629-
const int fd = args[0]->Int32Value();
1630-
const double atime = static_cast<double>(args[1]->NumberValue());
1631-
const double mtime = static_cast<double>(args[2]->NumberValue());
1634+
CHECK(args[2]->IsNumber());
1635+
const double mtime = args[2].As<Number>()->Value();
16321636

16331637
FSReqBase* req_wrap = GetReqWrap(env, args[3]);
1634-
if (req_wrap != nullptr) {
1638+
if (req_wrap != nullptr) { // futimes(fd, atime, mtime, req)
16351639
AsyncCall(env, req_wrap, args, "futime", UTF8, AfterNoArgs,
16361640
uv_fs_futime, fd, atime, mtime);
1637-
} else {
1638-
SYNC_CALL(futime, 0, fd, atime, mtime);
1641+
} else { // futimes(fd, atime, mtime, undefined, ctx)
1642+
CHECK_EQ(argc, 5);
1643+
fs_req_wrap req_wrap;
1644+
SyncCall(env, args[4], &req_wrap, "futime",
1645+
uv_fs_futime, fd, atime, mtime);
16391646
}
16401647
}
16411648

test/parallel/test-fs-error-messages.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,24 @@ if (!common.isWindows) {
811811
);
812812
});
813813
}
814+
815+
816+
// futimes
817+
if (!common.isAIX) {
818+
const validateError = (err) => {
819+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, futime');
820+
assert.strictEqual(err.errno, uv.UV_EBADF);
821+
assert.strictEqual(err.code, 'EBADF');
822+
assert.strictEqual(err.syscall, 'futime');
823+
return true;
824+
};
825+
826+
common.runWithInvalidFD((fd) => {
827+
fs.futimes(fd, new Date(), new Date(), common.mustCall(validateError));
828+
829+
assert.throws(
830+
() => fs.futimesSync(fd, new Date(), new Date()),
831+
validateError
832+
);
833+
});
834+
}

0 commit comments

Comments
 (0)