Skip to content

Commit

Permalink
fs: extract out validateFd function
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
maclover7 authored and joyeecheung committed Jan 11, 2018
1 parent 6e3818f commit b9b8294
Showing 1 changed file with 32 additions and 41 deletions.
73 changes: 32 additions & 41 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
};
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit b9b8294

Please sign in to comment.