Skip to content

Commit 82523d3

Browse files
committed
fs: throw fs.utimesSync errors in JS land
PR-URL: #18871 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 8fb5a6c commit 82523d3

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

lib/fs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,9 +1148,11 @@ fs.utimes = function(path, atime, mtime, callback) {
11481148
fs.utimesSync = function(path, atime, mtime) {
11491149
path = getPathFromURL(path);
11501150
validatePath(path);
1151+
const ctx = { path };
11511152
binding.utimes(pathModule.toNamespacedPath(path),
1152-
toUnixTimestamp(atime),
1153-
toUnixTimestamp(mtime));
1153+
toUnixTimestamp(atime), toUnixTimestamp(mtime),
1154+
undefined, ctx);
1155+
handleErrorFromBinding(ctx);
11541156
};
11551157

11561158
fs.futimes = function(fd, atime, mtime, callback) {

src/node_file.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,22 +1561,27 @@ static void FChown(const FunctionCallbackInfo<Value>& args) {
15611561
static void UTimes(const FunctionCallbackInfo<Value>& args) {
15621562
Environment* env = Environment::GetCurrent(args);
15631563

1564-
CHECK_GE(args.Length(), 3);
1565-
CHECK(args[1]->IsNumber());
1566-
CHECK(args[2]->IsNumber());
1564+
const int argc = args.Length();
1565+
CHECK_GE(argc, 3);
15671566

15681567
BufferValue path(env->isolate(), args[0]);
15691568
CHECK_NE(*path, nullptr);
15701569

1571-
const double atime = static_cast<double>(args[1]->NumberValue());
1572-
const double mtime = static_cast<double>(args[2]->NumberValue());
1570+
CHECK(args[1]->IsNumber());
1571+
const double atime = args[1].As<Number>()->Value();
1572+
1573+
CHECK(args[2]->IsNumber());
1574+
const double mtime = args[2].As<Number>()->Value();
15731575

15741576
FSReqBase* req_wrap = GetReqWrap(env, args[3]);
1575-
if (req_wrap != nullptr) {
1577+
if (req_wrap != nullptr) { // utimes(path, atime, mtime, req)
15761578
AsyncCall(env, req_wrap, args, "utime", UTF8, AfterNoArgs,
15771579
uv_fs_utime, *path, atime, mtime);
1578-
} else {
1579-
SYNC_CALL(utime, *path, *path, atime, mtime);
1580+
} else { // utimes(path, atime, mtime, undefined, ctx)
1581+
CHECK_EQ(argc, 5);
1582+
fs_req_wrap req_wrap;
1583+
SyncCall(env, args[4], &req_wrap, "utime",
1584+
uv_fs_utime, *path, atime, mtime);
15801585
}
15811586
}
15821587

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,25 @@ if (!common.isWindows) {
585585
validateError
586586
);
587587
}
588+
589+
// utimes
590+
if (!common.isAIX) {
591+
const validateError = (err) => {
592+
assert.strictEqual(nonexistentFile, err.path);
593+
assert.strictEqual(
594+
err.message,
595+
`ENOENT: no such file or directory, utime '${nonexistentFile}'`);
596+
assert.strictEqual(err.errno, uv.UV_ENOENT);
597+
assert.strictEqual(err.code, 'ENOENT');
598+
assert.strictEqual(err.syscall, 'utime');
599+
return true;
600+
};
601+
602+
fs.utimes(nonexistentFile, new Date(), new Date(),
603+
common.mustCall(validateError));
604+
605+
assert.throws(
606+
() => fs.utimesSync(nonexistentFile, new Date(), new Date()),
607+
validateError
608+
);
609+
}

0 commit comments

Comments
 (0)