Skip to content

Commit 2a02868

Browse files
BridgeARaddaleax
authored andcommitted
fs: two minor optimizations
* 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]>
1 parent 9ee271d commit 2a02868

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

lib/fs.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,6 @@ function tryStatSync(fd, isUserFd) {
521521
} finally {
522522
if (threw && !isUserFd) fs.closeSync(fd);
523523
}
524-
return !threw;
525524
}
526525

527526
function tryCreateBuffer(size, fd, isUserFd) {
@@ -553,10 +552,11 @@ fs.readFileSync = function(path, options) {
553552
var isUserFd = isFd(path); // file descriptor ownership
554553
var fd = isUserFd ? path : fs.openSync(path, options.flag || 'r', 0o666);
555554

555+
tryStatSync(fd, isUserFd);
556556
// Use stats array directly to avoid creating an fs.Stats instance just for
557557
// our internal use.
558558
var size;
559-
if (tryStatSync(fd, isUserFd) && (statValues[1/*mode*/] & S_IFMT) === S_IFREG)
559+
if ((statValues[1/*mode*/] & S_IFMT) === S_IFREG)
560560
size = statValues[8/*size*/];
561561
else
562562
size = 0;
@@ -1085,7 +1085,7 @@ if (constants.O_SYMLINK !== undefined) {
10851085
callback(err);
10861086
return;
10871087
}
1088-
// prefer to return the chmod error, if one occurs,
1088+
// Prefer to return the chmod error, if one occurs,
10891089
// but still try to close, and report closing errors if they occur.
10901090
fs.fchmod(fd, mode, function(err) {
10911091
fs.close(fd, function(err2) {
@@ -1098,20 +1098,18 @@ if (constants.O_SYMLINK !== undefined) {
10981098
fs.lchmodSync = function(path, mode) {
10991099
var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
11001100

1101-
// prefer to return the chmod error, if one occurs,
1101+
// Prefer to return the chmod error, if one occurs,
11021102
// but still try to close, and report closing errors if they occur.
1103-
var err, err2, ret;
1103+
var ret;
11041104
try {
11051105
ret = fs.fchmodSync(fd, mode);
1106-
} catch (er) {
1107-
err = er;
1108-
}
1109-
try {
1110-
fs.closeSync(fd);
1111-
} catch (er) {
1112-
err2 = er;
1106+
} catch (err) {
1107+
try {
1108+
fs.closeSync(fd);
1109+
} catch (ignore) {}
1110+
throw err;
11131111
}
1114-
if (err || err2) throw (err || err2);
1112+
fs.closeSync(fd);
11151113
return ret;
11161114
};
11171115
}

0 commit comments

Comments
 (0)