Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: replace duplicate conditions by a function #18717

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
24 changes: 12 additions & 12 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ function isFd(path) {

fs.Stats = Stats;

function isFileType(fileType) {
// Use stats array directly to avoid creating an fs.Stats instance just for
// our internal use.
return (statValues[1/*mode*/] & S_IFMT) === fileType;
}

// Don't allow mode to accidentally be overwritten.
Object.defineProperties(fs, {
F_OK: { enumerable: true, value: constants.F_OK || 0 },
Expand Down Expand Up @@ -371,10 +377,8 @@ function readFileAfterStat(err) {
if (err)
return context.close(err);

// Use stats array directly to avoid creating an fs.Stats instance just for
// our internal use.
var size;
if ((statValues[1/*mode*/] & S_IFMT) === S_IFREG)
if (isFileType(S_IFREG))
size = context.size = statValues[8];
else
size = context.size = 0;
Expand Down Expand Up @@ -486,10 +490,8 @@ fs.readFileSync = function(path, options) {
var fd = isUserFd ? path : fs.openSync(path, options.flag || 'r', 0o666);

tryStatSync(fd, isUserFd);
// Use stats array directly to avoid creating an fs.Stats instance just for
// our internal use.
var size;
if ((statValues[1/*mode*/] & S_IFMT) === S_IFREG)
if (isFileType(S_IFREG))
size = statValues[8];
else
size = 0;
Expand Down Expand Up @@ -1623,8 +1625,7 @@ fs.realpathSync = function realpathSync(p, options) {

// continue if not a symlink, break if a pipe/socket
if (knownHard[base] || (cache && cache.get(base) === base)) {
if ((statValues[1/*mode*/] & S_IFMT) === S_IFIFO ||
(statValues[1/*mode*/] & S_IFMT) === S_IFSOCK) {
if (isFileType(S_IFIFO) || isFileType(S_IFSOCK)) {
break;
}
continue;
Expand All @@ -1643,7 +1644,7 @@ fs.realpathSync = function realpathSync(p, options) {
binding.lstat(baseLong, undefined, ctx);
handleErrorFromBinding(ctx);

if ((statValues[1/*mode*/] & S_IFMT) !== S_IFLNK) {
if (!isFileType(S_IFLNK)) {
knownHard[base] = true;
if (cache) cache.set(base, base);
continue;
Expand Down Expand Up @@ -1766,8 +1767,7 @@ fs.realpath = function realpath(p, options, callback) {

// continue if not a symlink, break if a pipe/socket
if (knownHard[base]) {
if ((statValues[1/*mode*/] & S_IFMT) === S_IFIFO ||
(statValues[1/*mode*/] & S_IFMT) === S_IFSOCK) {
if (isFileType(S_IFIFO) || isFileType(S_IFSOCK)) {
return callback(null, encodeRealpathResult(p, options));
}
return process.nextTick(LOOP);
Expand All @@ -1783,7 +1783,7 @@ fs.realpath = function realpath(p, options, callback) {
// our internal use.

// if not a symlink, skip to the next path part
if ((statValues[1/*mode*/] & S_IFMT) !== S_IFLNK) {
if (!isFileType(S_IFLNK)) {
knownHard[base] = true;
return process.nextTick(LOOP);
}
Expand Down