Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fs: extract out validateFd function
  • Loading branch information
maclover7 committed Dec 31, 2017
commit df6ceef7f3cb168e672a4977547cc4b62dc6ead3
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