Skip to content

Commit

Permalink
fs: throw fs.utimesSync errors in JS land
Browse files Browse the repository at this point in the history
PR-URL: #18871
Refs: #18106
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
joyeecheung committed Feb 27, 2018
1 parent 8fb5a6c commit 82523d3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
6 changes: 4 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 13 additions & 8 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1561,22 +1561,27 @@ static void FChown(const FunctionCallbackInfo<Value>& args) {
static void UTimes(const FunctionCallbackInfo<Value>& 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<double>(args[1]->NumberValue());
const double mtime = static_cast<double>(args[2]->NumberValue());
CHECK(args[1]->IsNumber());
const double atime = args[1].As<Number>()->Value();

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) { // 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);
}
}

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

0 comments on commit 82523d3

Please sign in to comment.