|
|
var constants = require('constants') |
|
|
|
|
|
var origCwd = process.cwd |
|
|
var cwd = null |
|
|
|
|
|
var platform = process.env.GRACEFUL_FS_PLATFORM || process.platform |
|
|
|
|
|
process.cwd = function() { |
|
|
if (!cwd) |
|
|
cwd = origCwd.call(process) |
|
|
return cwd |
|
|
} |
|
|
try { |
|
|
process.cwd() |
|
|
} catch (er) {} |
|
|
|
|
|
|
|
|
if (typeof process.chdir === 'function') { |
|
|
var chdir = process.chdir |
|
|
process.chdir = function (d) { |
|
|
cwd = null |
|
|
chdir.call(process, d) |
|
|
} |
|
|
if (Object.setPrototypeOf) Object.setPrototypeOf(process.chdir, chdir) |
|
|
} |
|
|
|
|
|
module.exports = patch |
|
|
|
|
|
function patch (fs) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (constants.hasOwnProperty('O_SYMLINK') && |
|
|
process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) { |
|
|
patchLchmod(fs) |
|
|
} |
|
|
|
|
|
|
|
|
if (!fs.lutimes) { |
|
|
patchLutimes(fs) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.chown = chownFix(fs.chown) |
|
|
fs.fchown = chownFix(fs.fchown) |
|
|
fs.lchown = chownFix(fs.lchown) |
|
|
|
|
|
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 = chmodFixSync(fs.chmodSync) |
|
|
fs.fchmodSync = chmodFixSync(fs.fchmodSync) |
|
|
fs.lchmodSync = chmodFixSync(fs.lchmodSync) |
|
|
|
|
|
fs.stat = statFix(fs.stat) |
|
|
fs.fstat = statFix(fs.fstat) |
|
|
fs.lstat = statFix(fs.lstat) |
|
|
|
|
|
fs.statSync = statFixSync(fs.statSync) |
|
|
fs.fstatSync = statFixSync(fs.fstatSync) |
|
|
fs.lstatSync = statFixSync(fs.lstatSync) |
|
|
|
|
|
|
|
|
if (fs.chmod && !fs.lchmod) { |
|
|
fs.lchmod = function (path, mode, cb) { |
|
|
if (cb) process.nextTick(cb) |
|
|
} |
|
|
fs.lchmodSync = function () {} |
|
|
} |
|
|
if (fs.chown && !fs.lchown) { |
|
|
fs.lchown = function (path, uid, gid, cb) { |
|
|
if (cb) process.nextTick(cb) |
|
|
} |
|
|
fs.lchownSync = function () {} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (platform === "win32") { |
|
|
fs.rename = typeof fs.rename !== 'function' ? fs.rename |
|
|
: (function (fs$rename) { |
|
|
function rename (from, to, cb) { |
|
|
var start = Date.now() |
|
|
var backoff = 0; |
|
|
fs$rename(from, to, function CB (er) { |
|
|
if (er |
|
|
&& (er.code === "EACCES" || er.code === "EPERM" || er.code === "EBUSY") |
|
|
&& Date.now() - start < 60000) { |
|
|
setTimeout(function() { |
|
|
fs.stat(to, function (stater, st) { |
|
|
if (stater && stater.code === "ENOENT") |
|
|
fs$rename(from, to, CB); |
|
|
else |
|
|
cb(er) |
|
|
}) |
|
|
}, backoff) |
|
|
if (backoff < 100) |
|
|
backoff += 10; |
|
|
return; |
|
|
} |
|
|
if (cb) cb(er) |
|
|
}) |
|
|
} |
|
|
if (Object.setPrototypeOf) Object.setPrototypeOf(rename, fs$rename) |
|
|
return rename |
|
|
})(fs.rename) |
|
|
} |
|
|
|
|
|
|
|
|
fs.read = typeof fs.read !== 'function' ? fs.read |
|
|
: (function (fs$read) { |
|
|
function read (fd, buffer, offset, length, position, callback_) { |
|
|
var callback |
|
|
if (callback_ && typeof callback_ === 'function') { |
|
|
var eagCounter = 0 |
|
|
callback = function (er, _, __) { |
|
|
if (er && er.code === 'EAGAIN' && eagCounter < 10) { |
|
|
eagCounter ++ |
|
|
return fs$read.call(fs, fd, buffer, offset, length, position, callback) |
|
|
} |
|
|
callback_.apply(this, arguments) |
|
|
} |
|
|
} |
|
|
return fs$read.call(fs, fd, buffer, offset, length, position, callback) |
|
|
} |
|
|
|
|
|
|
|
|
if (Object.setPrototypeOf) Object.setPrototypeOf(read, fs$read) |
|
|
return read |
|
|
})(fs.read) |
|
|
|
|
|
fs.readSync = typeof fs.readSync !== 'function' ? fs.readSync |
|
|
: (function (fs$readSync) { return function (fd, buffer, offset, length, position) { |
|
|
var eagCounter = 0 |
|
|
while (true) { |
|
|
try { |
|
|
return fs$readSync.call(fs, fd, buffer, offset, length, position) |
|
|
} catch (er) { |
|
|
if (er.code === 'EAGAIN' && eagCounter < 10) { |
|
|
eagCounter ++ |
|
|
continue |
|
|
} |
|
|
throw er |
|
|
} |
|
|
} |
|
|
}})(fs.readSync) |
|
|
|
|
|
function patchLchmod (fs) { |
|
|
fs.lchmod = function (path, mode, callback) { |
|
|
fs.open( path |
|
|
, constants.O_WRONLY | constants.O_SYMLINK |
|
|
, mode |
|
|
, function (err, fd) { |
|
|
if (err) { |
|
|
if (callback) callback(err) |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
fs.fchmod(fd, mode, function (err) { |
|
|
fs.close(fd, function(err2) { |
|
|
if (callback) callback(err || err2) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
|
|
|
fs.lchmodSync = function (path, mode) { |
|
|
var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode) |
|
|
|
|
|
|
|
|
|
|
|
var threw = true |
|
|
var ret |
|
|
try { |
|
|
ret = fs.fchmodSync(fd, mode) |
|
|
threw = false |
|
|
} finally { |
|
|
if (threw) { |
|
|
try { |
|
|
fs.closeSync(fd) |
|
|
} catch (er) {} |
|
|
} else { |
|
|
fs.closeSync(fd) |
|
|
} |
|
|
} |
|
|
return ret |
|
|
} |
|
|
} |
|
|
|
|
|
function patchLutimes (fs) { |
|
|
if (constants.hasOwnProperty("O_SYMLINK") && fs.futimes) { |
|
|
fs.lutimes = function (path, at, mt, cb) { |
|
|
fs.open(path, constants.O_SYMLINK, function (er, fd) { |
|
|
if (er) { |
|
|
if (cb) cb(er) |
|
|
return |
|
|
} |
|
|
fs.futimes(fd, at, mt, function (er) { |
|
|
fs.close(fd, function (er2) { |
|
|
if (cb) cb(er || er2) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
|
|
|
fs.lutimesSync = function (path, at, mt) { |
|
|
var fd = fs.openSync(path, constants.O_SYMLINK) |
|
|
var ret |
|
|
var threw = true |
|
|
try { |
|
|
ret = fs.futimesSync(fd, at, mt) |
|
|
threw = false |
|
|
} finally { |
|
|
if (threw) { |
|
|
try { |
|
|
fs.closeSync(fd) |
|
|
} catch (er) {} |
|
|
} else { |
|
|
fs.closeSync(fd) |
|
|
} |
|
|
} |
|
|
return ret |
|
|
} |
|
|
|
|
|
} else if (fs.futimes) { |
|
|
fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) } |
|
|
fs.lutimesSync = function () {} |
|
|
} |
|
|
} |
|
|
|
|
|
function chmodFix (orig) { |
|
|
if (!orig) return orig |
|
|
return function (target, mode, cb) { |
|
|
return orig.call(fs, target, mode, function (er) { |
|
|
if (chownErOk(er)) er = null |
|
|
if (cb) cb.apply(this, arguments) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
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) { |
|
|
return orig.call(fs, target, uid, gid, function (er) { |
|
|
if (chownErOk(er)) er = null |
|
|
if (cb) cb.apply(this, arguments) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
function chownFixSync (orig) { |
|
|
if (!orig) return orig |
|
|
return function (target, uid, gid) { |
|
|
try { |
|
|
return orig.call(fs, target, uid, gid) |
|
|
} catch (er) { |
|
|
if (!chownErOk(er)) throw er |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
function statFix (orig) { |
|
|
if (!orig) return orig |
|
|
|
|
|
|
|
|
return function (target, options, cb) { |
|
|
if (typeof options === 'function') { |
|
|
cb = options |
|
|
options = null |
|
|
} |
|
|
function callback (er, stats) { |
|
|
if (stats) { |
|
|
if (stats.uid < 0) stats.uid += 0x100000000 |
|
|
if (stats.gid < 0) stats.gid += 0x100000000 |
|
|
} |
|
|
if (cb) cb.apply(this, arguments) |
|
|
} |
|
|
return options ? orig.call(fs, target, options, callback) |
|
|
: orig.call(fs, target, callback) |
|
|
} |
|
|
} |
|
|
|
|
|
function statFixSync (orig) { |
|
|
if (!orig) return orig |
|
|
|
|
|
|
|
|
return function (target, options) { |
|
|
var stats = options ? orig.call(fs, target, options) |
|
|
: orig.call(fs, target) |
|
|
if (stats) { |
|
|
if (stats.uid < 0) stats.uid += 0x100000000 |
|
|
if (stats.gid < 0) stats.gid += 0x100000000 |
|
|
} |
|
|
return stats; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function chownErOk (er) { |
|
|
if (!er) |
|
|
return true |
|
|
|
|
|
if (er.code === "ENOSYS") |
|
|
return true |
|
|
|
|
|
var nonroot = !process.getuid || process.getuid() !== 0 |
|
|
if (nonroot) { |
|
|
if (er.code === "EINVAL" || er.code === "EPERM") |
|
|
return true |
|
|
} |
|
|
|
|
|
return false |
|
|
} |
|
|
} |
|
|
|