fs: move type checking in fs.futimes to js

PR-URL: https://github.com/nodejs/node/pull/17334
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
James M Snell 2017-11-26 17:05:26 -08:00
parent 82eb459e3f
commit 448ec0b5aa
3 changed files with 42 additions and 36 deletions

View File

@ -1301,7 +1301,7 @@ fs.chownSync = function(path, uid, gid) {
}; };
// converts Date or number to a fractional UNIX timestamp // converts Date or number to a fractional UNIX timestamp
function toUnixTimestamp(time) { function toUnixTimestamp(time, name = 'time') {
// eslint-disable-next-line eqeqeq // eslint-disable-next-line eqeqeq
if (typeof time === 'string' && +time == time) { if (typeof time === 'string' && +time == time) {
return +time; return +time;
@ -1316,10 +1316,10 @@ function toUnixTimestamp(time) {
// convert to 123.456 UNIX timestamp // convert to 123.456 UNIX timestamp
return time.getTime() / 1000; return time.getTime() / 1000;
} }
throw new errors.Error('ERR_INVALID_ARG_TYPE', throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'time', name,
['Date', 'Time in seconds'], ['Date', 'Time in seconds'],
time); time);
} }
// exported for unit tests, not for public consumption // exported for unit tests, not for public consumption
@ -1347,16 +1347,24 @@ fs.utimesSync = function(path, atime, mtime) {
}; };
fs.futimes = function(fd, atime, mtime, callback) { fs.futimes = function(fd, atime, mtime, callback) {
atime = toUnixTimestamp(atime); if (!Number.isInteger(fd))
mtime = toUnixTimestamp(mtime); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
var req = new FSReqWrap(); if (fd < 0 || fd > 0xFFFFFFFF)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
atime = toUnixTimestamp(atime, 'atime');
mtime = toUnixTimestamp(mtime, 'mtime');
const req = new FSReqWrap();
req.oncomplete = makeCallback(callback); req.oncomplete = makeCallback(callback);
binding.futimes(fd, atime, mtime, req); binding.futimes(fd, atime, mtime, req);
}; };
fs.futimesSync = function(fd, atime, mtime) { fs.futimesSync = function(fd, atime, mtime) {
atime = toUnixTimestamp(atime); if (!Number.isInteger(fd))
mtime = toUnixTimestamp(mtime); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
if (fd < 0 || fd > 0xFFFFFFFF)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
atime = toUnixTimestamp(atime, 'atime');
mtime = toUnixTimestamp(mtime, 'mtime');
binding.futimes(fd, atime, mtime); binding.futimes(fd, atime, mtime);
}; };

View File

@ -1353,19 +1353,9 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
static void FUTimes(const FunctionCallbackInfo<Value>& args) { static void FUTimes(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
int len = args.Length(); CHECK(args[0]->IsInt32());
if (len < 1) CHECK(args[1]->IsNumber());
return TYPE_ERROR("fd required"); CHECK(args[2]->IsNumber());
if (len < 2)
return TYPE_ERROR("atime required");
if (len < 3)
return TYPE_ERROR("mtime required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be an int");
if (!args[1]->IsNumber())
return TYPE_ERROR("atime must be a number");
if (!args[2]->IsNumber())
return TYPE_ERROR("mtime must be a number");
const int fd = args[0]->Int32Value(); const int fd = args[0]->Int32Value();
const double atime = static_cast<double>(args[1]->NumberValue()); const double atime = static_cast<double>(args[1]->NumberValue());

View File

@ -97,12 +97,14 @@ function testIt(atime, mtime, callback) {
tests_run++; tests_run++;
err = undefined; err = undefined;
try { common.expectsError(
fs.futimesSync(-1, atime, mtime); () => fs.futimesSync(-1, atime, mtime),
} catch (ex) { {
err = ex; code: 'ERR_OUT_OF_RANGE',
} type: RangeError,
expect_errno('futimesSync', -1, err, 'EBADF'); message: 'The "fd" argument is out of range'
}
);
tests_run++; tests_run++;
} }
@ -125,11 +127,17 @@ function testIt(atime, mtime, callback) {
fs.futimes(fd, atime, mtime, common.mustCall(function(err) { fs.futimes(fd, atime, mtime, common.mustCall(function(err) {
expect_ok('futimes', fd, err, atime, mtime); expect_ok('futimes', fd, err, atime, mtime);
fs.futimes(-1, atime, mtime, common.mustCall(function(err) { common.expectsError(
expect_errno('futimes', -1, err, 'EBADF'); () => fs.futimes(-1, atime, mtime, common.mustNotCall()),
syncTests(); {
callback(); code: 'ERR_OUT_OF_RANGE',
})); type: RangeError,
message: 'The "fd" argument is out of range'
}
);
syncTests();
tests_run++; tests_run++;
})); }));
tests_run++; tests_run++;
@ -142,7 +150,7 @@ function testIt(atime, mtime, callback) {
const stats = fs.statSync(common.tmpDir); const stats = fs.statSync(common.tmpDir);
// run tests // run tests
const runTest = common.mustCall(testIt, 6); const runTest = common.mustCall(testIt, 1);
runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() { runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
runTest(new Date(), new Date(), function() { runTest(new Date(), new Date(), function() {
@ -163,7 +171,7 @@ runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
}); });
process.on('exit', function() { process.on('exit', function() {
assert.strictEqual(tests_ok, tests_run); assert.strictEqual(tests_ok, tests_run - 2);
}); });