fs: cleanup fd lchown and lchownSync

lchown and lchownSync were opening file descriptors without
closing them. Looks like it has been that way for 7 years.
Does anyone actually use these functions?

PR-URL: https://github.com/nodejs/node/pull/18329
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
James M Snell 2018-01-23 15:07:42 -08:00
parent fa33f026cc
commit 082f9525d9

View File

@ -1330,20 +1330,16 @@ if (constants.O_SYMLINK !== undefined) {
}; };
fs.lchmodSync = function(path, mode) { fs.lchmodSync = function(path, mode) {
var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK); const 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. // but still try to close, and report closing errors if they occur.
var ret; let ret;
try { try {
ret = fs.fchmodSync(fd, mode); ret = fs.fchmodSync(fd, mode);
} catch (err) { } finally {
try {
fs.closeSync(fd); fs.closeSync(fd);
} catch (ignore) {}
throw err;
} }
fs.closeSync(fd);
return ret; return ret;
}; };
} }
@ -1381,13 +1377,25 @@ if (constants.O_SYMLINK !== undefined) {
callback(err); callback(err);
return; return;
} }
fs.fchown(fd, uid, gid, callback); // Prefer to return the chown error, if one occurs,
// but still try to close, and report closing errors if they occur.
fs.fchown(fd, uid, gid, function(err) {
fs.close(fd, function(err2) {
callback(err || err2);
});
});
}); });
}; };
fs.lchownSync = function(path, uid, gid) { fs.lchownSync = function(path, uid, gid) {
var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK); const fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
return fs.fchownSync(fd, uid, gid); let ret;
try {
ret = fs.fchownSync(fd, uid, gid);
} finally {
fs.closeSync(fd);
}
return ret;
}; };
} }