fs: extract out validateFd function

PR-URL: https://github.com/nodejs/node/pull/17682
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Jon Moss 2017-12-13 17:24:56 -05:00 committed by Joyee Cheung
parent 6e3818f2a2
commit b9b8294dda
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C

View File

@ -153,6 +153,18 @@ function makeCallback(cb) {
}; };
} }
function validateFd(fd) {
let err;
if (!isUint32(fd))
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (err !== undefined) {
Error.captureStackTrace(err, validateFd);
throw err;
}
}
// Special case of `makeCallback()` that is specific to async `*stat()` calls as // Special case of `makeCallback()` that is specific to async `*stat()` calls as
// an optimization, since the data passed back to the callback needs to be // an optimization, since the data passed back to the callback needs to be
// transformed anyway. // transformed anyway.
@ -649,17 +661,14 @@ fs.readFileSync = function(path, options) {
}; };
fs.close = function(fd, callback) { fs.close = function(fd, callback) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
const req = new FSReqWrap(); const req = new FSReqWrap();
req.oncomplete = makeCallback(callback); req.oncomplete = makeCallback(callback);
binding.close(fd, req); binding.close(fd, req);
}; };
fs.closeSync = function(fd) { fs.closeSync = function(fd) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
const ctx = {}; const ctx = {};
binding.close(fd, undefined, ctx); binding.close(fd, undefined, ctx);
@ -720,8 +729,7 @@ fs.openSync = function(path, flags, mode) {
}; };
fs.read = function(fd, buffer, offset, length, position, callback) { fs.read = function(fd, buffer, offset, length, position, callback) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (!isUint8Array(buffer)) if (!isUint8Array(buffer))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer', throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
['Buffer', 'Uint8Array']); ['Buffer', 'Uint8Array']);
@ -759,8 +767,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
{ value: ['bytesRead', 'buffer'], enumerable: false }); { value: ['bytesRead', 'buffer'], enumerable: false });
fs.readSync = function(fd, buffer, offset, length, position) { fs.readSync = function(fd, buffer, offset, length, position) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (!isUint8Array(buffer)) if (!isUint8Array(buffer))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer', throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
['Buffer', 'Uint8Array']); ['Buffer', 'Uint8Array']);
@ -794,8 +801,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
callback(err, written || 0, buffer); callback(err, written || 0, buffer);
} }
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
const req = new FSReqWrap(); const req = new FSReqWrap();
req.oncomplete = wrapper; req.oncomplete = wrapper;
@ -839,8 +845,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs,
// OR // OR
// fs.writeSync(fd, string[, position[, encoding]]); // fs.writeSync(fd, string[, position[, encoding]]);
fs.writeSync = function(fd, buffer, offset, length, position) { fs.writeSync = function(fd, buffer, offset, length, position) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (isUint8Array(buffer)) { if (isUint8Array(buffer)) {
if (position === undefined) if (position === undefined)
position = null; position = null;
@ -956,8 +961,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
callback = len; callback = len;
len = 0; len = 0;
} }
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (!isInt32(len)) if (!isInt32(len))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
len = Math.max(0, len); len = Math.max(0, len);
@ -967,8 +971,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
}; };
fs.ftruncateSync = function(fd, len = 0) { fs.ftruncateSync = function(fd, len = 0) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (!isInt32(len)) if (!isInt32(len))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
len = Math.max(0, len); len = Math.max(0, len);
@ -1000,30 +1003,26 @@ fs.rmdirSync = function(path) {
}; };
fs.fdatasync = function(fd, callback) { fs.fdatasync = function(fd, callback) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
const req = new FSReqWrap(); const req = new FSReqWrap();
req.oncomplete = makeCallback(callback); req.oncomplete = makeCallback(callback);
binding.fdatasync(fd, req); binding.fdatasync(fd, req);
}; };
fs.fdatasyncSync = function(fd) { fs.fdatasyncSync = function(fd) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
return binding.fdatasync(fd); return binding.fdatasync(fd);
}; };
fs.fsync = function(fd, callback) { fs.fsync = function(fd, callback) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
const req = new FSReqWrap(); const req = new FSReqWrap();
req.oncomplete = makeCallback(callback); req.oncomplete = makeCallback(callback);
binding.fsync(fd, req); binding.fsync(fd, req);
}; };
fs.fsyncSync = function(fd) { fs.fsyncSync = function(fd) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
return binding.fsync(fd); return binding.fsync(fd);
}; };
@ -1089,8 +1088,7 @@ fs.readdirSync = function(path, options) {
}; };
fs.fstat = function(fd, callback) { fs.fstat = function(fd, callback) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
const req = new FSReqWrap(); const req = new FSReqWrap();
req.oncomplete = makeStatsCallback(callback); req.oncomplete = makeStatsCallback(callback);
binding.fstat(fd, req); binding.fstat(fd, req);
@ -1125,8 +1123,7 @@ fs.stat = function(path, callback) {
}; };
fs.fstatSync = function(fd) { fs.fstatSync = function(fd) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
binding.fstat(fd); binding.fstat(fd);
return statsFromValues(); return statsFromValues();
}; };
@ -1336,8 +1333,7 @@ fs.unlinkSync = function(path) {
fs.fchmod = function(fd, mode, callback) { fs.fchmod = function(fd, mode, callback) {
mode = modeNum(mode); mode = modeNum(mode);
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (!isUint32(mode)) if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
if (mode < 0 || mode > 0o777) if (mode < 0 || mode > 0o777)
@ -1350,8 +1346,7 @@ fs.fchmod = function(fd, mode, callback) {
fs.fchmodSync = function(fd, mode) { fs.fchmodSync = function(fd, mode) {
mode = modeNum(mode); mode = modeNum(mode);
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (!isUint32(mode)) if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
if (mode < 0 || mode > 0o777) if (mode < 0 || mode > 0o777)
@ -1448,8 +1443,7 @@ if (constants.O_SYMLINK !== undefined) {
} }
fs.fchown = function(fd, uid, gid, callback) { fs.fchown = function(fd, uid, gid, callback) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (!isUint32(uid)) if (!isUint32(uid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
if (!isUint32(gid)) if (!isUint32(gid))
@ -1461,8 +1455,7 @@ fs.fchown = function(fd, uid, gid, callback) {
}; };
fs.fchownSync = function(fd, uid, gid) { fs.fchownSync = function(fd, uid, gid) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (!isUint32(uid)) if (!isUint32(uid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
if (!isUint32(gid)) if (!isUint32(gid))
@ -1562,8 +1555,7 @@ fs.utimesSync = function(path, atime, mtime) {
}; };
fs.futimes = function(fd, atime, mtime, callback) { fs.futimes = function(fd, atime, mtime, callback) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
atime = toUnixTimestamp(atime, 'atime'); atime = toUnixTimestamp(atime, 'atime');
mtime = toUnixTimestamp(mtime, 'mtime'); mtime = toUnixTimestamp(mtime, 'mtime');
const req = new FSReqWrap(); const req = new FSReqWrap();
@ -1572,8 +1564,7 @@ fs.futimes = function(fd, atime, mtime, callback) {
}; };
fs.futimesSync = function(fd, atime, mtime) { fs.futimesSync = function(fd, atime, mtime) {
if (!isUint32(fd)) validateFd(fd);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
atime = toUnixTimestamp(atime, 'atime'); atime = toUnixTimestamp(atime, 'atime');
mtime = toUnixTimestamp(mtime, 'mtime'); mtime = toUnixTimestamp(mtime, 'mtime');
binding.futimes(fd, atime, mtime); binding.futimes(fd, atime, mtime);