From 785ee4988b783600790c4b8763ae4c1fdd0170ec Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Sat, 23 Jul 2016 10:12:43 +0530 Subject: [PATCH 1/2] fix chmod function arguments `*chmod*` functions accept only `path` and `mode`. As we were using `chownFix`, it misinterprets callback function passed to `chmod` as `gid`. --- polyfills.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/polyfills.js b/polyfills.js index 5e4f480..ffba98e 100644 --- a/polyfills.js +++ b/polyfills.js @@ -44,17 +44,17 @@ function patch (fs) { fs.fchown = chownFix(fs.fchown) fs.lchown = chownFix(fs.lchown) - fs.chmod = chownFix(fs.chmod) - fs.fchmod = chownFix(fs.fchmod) - fs.lchmod = chownFix(fs.lchmod) + fs.chmod = chmodFix(fs.chmod) + fs.fchmod = chmodFix(fs.fchmod) + fs.lchmod = chmodFix(fs.lchmod) fs.chownSync = chownFixSync(fs.chownSync) fs.fchownSync = chownFixSync(fs.fchownSync) fs.lchownSync = chownFixSync(fs.lchownSync) - fs.chmodSync = chownFix(fs.chmodSync) - fs.fchmodSync = chownFix(fs.fchmodSync) - fs.lchmodSync = chownFix(fs.lchmodSync) + fs.chmodSync = chmodFixSync(fs.chmodSync) + fs.fchmodSync = chmodFixSync(fs.fchmodSync) + fs.lchmodSync = chmodFixSync(fs.lchmodSync) // if lchmod/lchown do not exist, then make them no-ops if (!fs.lchmod) { @@ -202,6 +202,28 @@ function patchLutimes (fs) { } } +function chmodFix (orig) { + if (!orig) return orig + return function (target, mode, cb) { + return orig.call(fs, target, mode, function (er, res) { + if (chownErOk(er)) er = null + cb(er, res) + }) + } +} + +function chmodFixSync (orig) { + if (!orig) return orig + return function (target, mode) { + try { + return orig.call(fs, target, mode) + } catch (er) { + if (!chownErOk(er)) throw er + } + } +} + + function chownFix (orig) { if (!orig) return orig return function (target, uid, gid, cb) { From 73a73fc9395912e6dd0922dd602727a7309c5a6e Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Tue, 26 Jul 2016 11:07:38 +0530 Subject: [PATCH 2/2] use callback only if it is available --- polyfills.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/polyfills.js b/polyfills.js index ffba98e..1cc74c8 100644 --- a/polyfills.js +++ b/polyfills.js @@ -59,13 +59,13 @@ function patch (fs) { // if lchmod/lchown do not exist, then make them no-ops if (!fs.lchmod) { fs.lchmod = function (path, mode, cb) { - process.nextTick(cb) + if (cb) process.nextTick(cb) } fs.lchmodSync = function () {} } if (!fs.lchown) { fs.lchown = function (path, uid, gid, cb) { - process.nextTick(cb) + if (cb) process.nextTick(cb) } fs.lchownSync = function () {} } @@ -121,20 +121,19 @@ function patch (fs) { function patchLchmod (fs) { fs.lchmod = function (path, mode, callback) { - callback = callback || noop fs.open( path , constants.O_WRONLY | constants.O_SYMLINK , mode , function (err, fd) { if (err) { - callback(err) + if (callback) callback(err) return } // 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) { - callback(err || err2) + if (callback) callback(err || err2) }) }) }) @@ -167,11 +166,13 @@ function patchLutimes (fs) { if (constants.hasOwnProperty("O_SYMLINK")) { fs.lutimes = function (path, at, mt, cb) { fs.open(path, constants.O_SYMLINK, function (er, fd) { - cb = cb || noop - if (er) return cb(er) + if (er) { + if (cb) cb(er) + return + } fs.futimes(fd, at, mt, function (er) { fs.close(fd, function (er2) { - return cb(er || er2) + if (cb) cb(er || er2) }) }) }) @@ -197,7 +198,7 @@ function patchLutimes (fs) { } } else { - fs.lutimes = function (_a, _b, _c, cb) { process.nextTick(cb) } + fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) } fs.lutimesSync = function () {} } } @@ -207,7 +208,7 @@ function chmodFix (orig) { return function (target, mode, cb) { return orig.call(fs, target, mode, function (er, res) { if (chownErOk(er)) er = null - cb(er, res) + if (cb) cb(er, res) }) } } @@ -229,7 +230,7 @@ function chownFix (orig) { return function (target, uid, gid, cb) { return orig.call(fs, target, uid, gid, function (er, res) { if (chownErOk(er)) er = null - cb(er, res) + if (cb) cb(er, res) }) } }