fs: throw fs.utimesSync errors in JS land

PR-URL: https://github.com/nodejs/node/pull/18871
Refs: https://github.com/nodejs/node/issues/18106
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Joyee Cheung 2018-02-20 04:23:47 +08:00
parent 8fb5a6cd81
commit 82523d3b6e
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
3 changed files with 39 additions and 10 deletions

View File

@ -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) {

View File

@ -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);
}
}

View File

@ -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
);
}