diff options
author | Jon Moss <me@jonathanmoss.me> | 2017-12-13 17:24:56 -0500 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-01-11 09:03:36 +0800 |
commit | b9b8294ddad4baa7fbe565e5d18f18d815d7f967 (patch) | |
tree | fb561e16faf0549e28d396ca223ac0ca786f5afe /lib | |
parent | 6e3818f2a2881df49eec454b8d5a9882cb57cfa2 (diff) | |
download | android-node-v8-b9b8294ddad4baa7fbe565e5d18f18d815d7f967.tar.gz android-node-v8-b9b8294ddad4baa7fbe565e5d18f18d815d7f967.tar.bz2 android-node-v8-b9b8294ddad4baa7fbe565e5d18f18d815d7f967.zip |
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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/fs.js | 73 |
1 files changed, 32 insertions, 41 deletions
@@ -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 // an optimization, since the data passed back to the callback needs to be // transformed anyway. @@ -649,17 +661,14 @@ fs.readFileSync = function(path, options) { }; fs.close = function(fd, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); - + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = makeCallback(callback); binding.close(fd, req); }; fs.closeSync = function(fd) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const 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) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint8Array(buffer)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer', ['Buffer', 'Uint8Array']); @@ -759,8 +767,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs, { value: ['bytesRead', 'buffer'], enumerable: false }); fs.readSync = function(fd, buffer, offset, length, position) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint8Array(buffer)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer', ['Buffer', 'Uint8Array']); @@ -794,8 +801,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) { callback(err, written || 0, buffer); } - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = wrapper; @@ -839,8 +845,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs, // OR // fs.writeSync(fd, string[, position[, encoding]]); fs.writeSync = function(fd, buffer, offset, length, position) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (isUint8Array(buffer)) { if (position === undefined) position = null; @@ -956,8 +961,7 @@ fs.ftruncate = function(fd, len = 0, callback) { callback = len; len = 0; } - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isInt32(len)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer'); len = Math.max(0, len); @@ -967,8 +971,7 @@ fs.ftruncate = function(fd, len = 0, callback) { }; fs.ftruncateSync = function(fd, len = 0) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isInt32(len)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer'); len = Math.max(0, len); @@ -1000,30 +1003,26 @@ fs.rmdirSync = function(path) { }; fs.fdatasync = function(fd, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = makeCallback(callback); binding.fdatasync(fd, req); }; fs.fdatasyncSync = function(fd) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); return binding.fdatasync(fd); }; fs.fsync = function(fd, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = makeCallback(callback); binding.fsync(fd, req); }; fs.fsyncSync = function(fd) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); return binding.fsync(fd); }; @@ -1089,8 +1088,7 @@ fs.readdirSync = function(path, options) { }; fs.fstat = function(fd, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = makeStatsCallback(callback); binding.fstat(fd, req); @@ -1125,8 +1123,7 @@ fs.stat = function(path, callback) { }; fs.fstatSync = function(fd) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); binding.fstat(fd); return statsFromValues(); }; @@ -1336,8 +1333,7 @@ fs.unlinkSync = function(path) { fs.fchmod = function(fd, mode, callback) { mode = modeNum(mode); - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint32(mode)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer'); if (mode < 0 || mode > 0o777) @@ -1350,8 +1346,7 @@ fs.fchmod = function(fd, mode, callback) { fs.fchmodSync = function(fd, mode) { mode = modeNum(mode); - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint32(mode)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer'); if (mode < 0 || mode > 0o777) @@ -1448,8 +1443,7 @@ if (constants.O_SYMLINK !== undefined) { } fs.fchown = function(fd, uid, gid, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint32(uid)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer'); if (!isUint32(gid)) @@ -1461,8 +1455,7 @@ fs.fchown = function(fd, uid, gid, callback) { }; fs.fchownSync = function(fd, uid, gid) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint32(uid)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer'); if (!isUint32(gid)) @@ -1562,8 +1555,7 @@ fs.utimesSync = function(path, atime, mtime) { }; fs.futimes = function(fd, atime, mtime, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); atime = toUnixTimestamp(atime, 'atime'); mtime = toUnixTimestamp(mtime, 'mtime'); const req = new FSReqWrap(); @@ -1572,8 +1564,7 @@ fs.futimes = function(fd, atime, mtime, callback) { }; fs.futimesSync = function(fd, atime, mtime) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); atime = toUnixTimestamp(atime, 'atime'); mtime = toUnixTimestamp(mtime, 'mtime'); binding.futimes(fd, atime, mtime); |