Skip to content

Commit

Permalink
fs: two minor optimizations
Browse files Browse the repository at this point in the history
* tryStatSync should not return any value
  If the function threw, it would never reach that code path.

* only use try catch if necessary
  lchmodSync does not need the second try catch in most cases.

PR-URL: #14055
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
  • Loading branch information
BridgeAR authored and addaleax committed Jul 18, 2017
1 parent 9ee271d commit 2a02868
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,6 @@ function tryStatSync(fd, isUserFd) {
} finally {
if (threw && !isUserFd) fs.closeSync(fd);
}
return !threw;
}

function tryCreateBuffer(size, fd, isUserFd) {
Expand Down Expand Up @@ -553,10 +552,11 @@ fs.readFileSync = function(path, options) {
var isUserFd = isFd(path); // file descriptor ownership
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 (tryStatSync(fd, isUserFd) && (statValues[1/*mode*/] & S_IFMT) === S_IFREG)
if ((statValues[1/*mode*/] & S_IFMT) === S_IFREG)
size = statValues[8/*size*/];
else
size = 0;
Expand Down Expand Up @@ -1085,7 +1085,7 @@ if (constants.O_SYMLINK !== undefined) {
callback(err);
return;
}
// prefer to return the chmod error, if one occurs,
// Prefer to return the chmod error, if one occurs,
// but still try to close, and report closing errors if they occur.
fs.fchmod(fd, mode, function(err) {
fs.close(fd, function(err2) {
Expand All @@ -1098,20 +1098,18 @@ if (constants.O_SYMLINK !== undefined) {
fs.lchmodSync = function(path, mode) {
var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);

// prefer to return the chmod error, if one occurs,
// Prefer to return the chmod error, if one occurs,
// but still try to close, and report closing errors if they occur.
var err, err2, ret;
var ret;
try {
ret = fs.fchmodSync(fd, mode);
} catch (er) {
err = er;
}
try {
fs.closeSync(fd);
} catch (er) {
err2 = er;
} catch (err) {
try {
fs.closeSync(fd);
} catch (ignore) {}
throw err;
}
if (err || err2) throw (err || err2);
fs.closeSync(fd);
return ret;
};
}
Expand Down

0 comments on commit 2a02868

Please sign in to comment.