From 82523d3b6eb7905fc59f12a0d38026af996152fa Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 20 Feb 2018 04:23:47 +0800 Subject: [PATCH] fs: throw fs.utimesSync errors in JS land MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/18871 Refs: https://github.com/nodejs/node/issues/18106 Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/fs.js | 6 ++++-- src/node_file.cc | 21 +++++++++++++-------- test/parallel/test-fs-error-messages.js | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index e86360fc1a2158..49b4d434e8181c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1148,9 +1148,11 @@ fs.utimes = function(path, atime, mtime, callback) { fs.utimesSync = function(path, atime, mtime) { path = getPathFromURL(path); validatePath(path); + const ctx = { path }; binding.utimes(pathModule.toNamespacedPath(path), - toUnixTimestamp(atime), - toUnixTimestamp(mtime)); + toUnixTimestamp(atime), toUnixTimestamp(mtime), + undefined, ctx); + handleErrorFromBinding(ctx); }; fs.futimes = function(fd, atime, mtime, callback) { diff --git a/src/node_file.cc b/src/node_file.cc index 6e23846d471256..3152d2067c110b 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1561,22 +1561,27 @@ static void FChown(const FunctionCallbackInfo& args) { static void UTimes(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - CHECK_GE(args.Length(), 3); - CHECK(args[1]->IsNumber()); - CHECK(args[2]->IsNumber()); + const int argc = args.Length(); + CHECK_GE(argc, 3); BufferValue path(env->isolate(), args[0]); CHECK_NE(*path, nullptr); - const double atime = static_cast(args[1]->NumberValue()); - const double mtime = static_cast(args[2]->NumberValue()); + CHECK(args[1]->IsNumber()); + const double atime = args[1].As()->Value(); + + 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) { // utimes(path, atime, mtime, req) AsyncCall(env, req_wrap, args, "utime", UTF8, AfterNoArgs, uv_fs_utime, *path, atime, mtime); - } else { - SYNC_CALL(utime, *path, *path, atime, mtime); + } else { // utimes(path, atime, mtime, undefined, ctx) + CHECK_EQ(argc, 5); + fs_req_wrap req_wrap; + SyncCall(env, args[4], &req_wrap, "utime", + uv_fs_utime, *path, atime, mtime); } } diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index b91d65538464a9..5ded7d234054d4 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -585,3 +585,25 @@ if (!common.isWindows) { validateError ); } + +// utimes +if (!common.isAIX) { + const validateError = (err) => { + assert.strictEqual(nonexistentFile, err.path); + assert.strictEqual( + err.message, + `ENOENT: no such file or directory, utime '${nonexistentFile}'`); + assert.strictEqual(err.errno, uv.UV_ENOENT); + assert.strictEqual(err.code, 'ENOENT'); + assert.strictEqual(err.syscall, 'utime'); + return true; + }; + + fs.utimes(nonexistentFile, new Date(), new Date(), + common.mustCall(validateError)); + + assert.throws( + () => fs.utimesSync(nonexistentFile, new Date(), new Date()), + validateError + ); +}