react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / node_modules / commoner / node_modules / graceful-fs / polyfills.js
81164 viewsvar fs = require('./fs.js')1var constants = require('constants')23var origCwd = process.cwd4var cwd = null5process.cwd = function() {6if (!cwd)7cwd = origCwd.call(process)8return cwd9}10var chdir = process.chdir11process.chdir = function(d) {12cwd = null13chdir.call(process, d)14}1516// (re-)implement some things that are known busted or missing.1718// lchmod, broken prior to 0.6.219// back-port the fix here.20if (constants.hasOwnProperty('O_SYMLINK') &&21process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {22fs.lchmod = function (path, mode, callback) {23callback = callback || noop24fs.open( path25, constants.O_WRONLY | constants.O_SYMLINK26, mode27, function (err, fd) {28if (err) {29callback(err)30return31}32// prefer to return the chmod error, if one occurs,33// but still try to close, and report closing errors if they occur.34fs.fchmod(fd, mode, function (err) {35fs.close(fd, function(err2) {36callback(err || err2)37})38})39})40}4142fs.lchmodSync = function (path, mode) {43var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)4445// prefer to return the chmod error, if one occurs,46// but still try to close, and report closing errors if they occur.47var err, err248try {49var ret = fs.fchmodSync(fd, mode)50} catch (er) {51err = er52}53try {54fs.closeSync(fd)55} catch (er) {56err2 = er57}58if (err || err2) throw (err || err2)59return ret60}61}626364// lutimes implementation, or no-op65if (!fs.lutimes) {66if (constants.hasOwnProperty("O_SYMLINK")) {67fs.lutimes = function (path, at, mt, cb) {68fs.open(path, constants.O_SYMLINK, function (er, fd) {69cb = cb || noop70if (er) return cb(er)71fs.futimes(fd, at, mt, function (er) {72fs.close(fd, function (er2) {73return cb(er || er2)74})75})76})77}7879fs.lutimesSync = function (path, at, mt) {80var fd = fs.openSync(path, constants.O_SYMLINK)81, err82, err283, ret8485try {86var ret = fs.futimesSync(fd, at, mt)87} catch (er) {88err = er89}90try {91fs.closeSync(fd)92} catch (er) {93err2 = er94}95if (err || err2) throw (err || err2)96return ret97}9899} else if (fs.utimensat && constants.hasOwnProperty("AT_SYMLINK_NOFOLLOW")) {100// maybe utimensat will be bound soonish?101fs.lutimes = function (path, at, mt, cb) {102fs.utimensat(path, at, mt, constants.AT_SYMLINK_NOFOLLOW, cb)103}104105fs.lutimesSync = function (path, at, mt) {106return fs.utimensatSync(path, at, mt, constants.AT_SYMLINK_NOFOLLOW)107}108109} else {110fs.lutimes = function (_a, _b, _c, cb) { process.nextTick(cb) }111fs.lutimesSync = function () {}112}113}114115116// https://github.com/isaacs/node-graceful-fs/issues/4117// Chown should not fail on einval or eperm if non-root.118// It should not fail on enosys ever, as this just indicates119// that a fs doesn't support the intended operation.120121fs.chown = chownFix(fs.chown)122fs.fchown = chownFix(fs.fchown)123fs.lchown = chownFix(fs.lchown)124125fs.chmod = chownFix(fs.chmod)126fs.fchmod = chownFix(fs.fchmod)127fs.lchmod = chownFix(fs.lchmod)128129fs.chownSync = chownFixSync(fs.chownSync)130fs.fchownSync = chownFixSync(fs.fchownSync)131fs.lchownSync = chownFixSync(fs.lchownSync)132133fs.chmodSync = chownFix(fs.chmodSync)134fs.fchmodSync = chownFix(fs.fchmodSync)135fs.lchmodSync = chownFix(fs.lchmodSync)136137function chownFix (orig) {138if (!orig) return orig139return function (target, uid, gid, cb) {140return orig.call(fs, target, uid, gid, function (er, res) {141if (chownErOk(er)) er = null142cb(er, res)143})144}145}146147function chownFixSync (orig) {148if (!orig) return orig149return function (target, uid, gid) {150try {151return orig.call(fs, target, uid, gid)152} catch (er) {153if (!chownErOk(er)) throw er154}155}156}157158// ENOSYS means that the fs doesn't support the op. Just ignore159// that, because it doesn't matter.160//161// if there's no getuid, or if getuid() is something other162// than 0, and the error is EINVAL or EPERM, then just ignore163// it.164//165// This specific case is a silent failure in cp, install, tar,166// and most other unix tools that manage permissions.167//168// When running as root, or if other types of errors are169// encountered, then it's strict.170function chownErOk (er) {171if (!er)172return true173174if (er.code === "ENOSYS")175return true176177var nonroot = !process.getuid || process.getuid() !== 0178if (nonroot) {179if (er.code === "EINVAL" || er.code === "EPERM")180return true181}182183return false184}185186187// if lchmod/lchown do not exist, then make them no-ops188if (!fs.lchmod) {189fs.lchmod = function (path, mode, cb) {190process.nextTick(cb)191}192fs.lchmodSync = function () {}193}194if (!fs.lchown) {195fs.lchown = function (path, uid, gid, cb) {196process.nextTick(cb)197}198fs.lchownSync = function () {}199}200201202203// on Windows, A/V software can lock the directory, causing this204// to fail with an EACCES or EPERM if the directory contains newly205// created files. Try again on failure, for up to 1 second.206if (process.platform === "win32") {207var rename_ = fs.rename208fs.rename = function rename (from, to, cb) {209var start = Date.now()210rename_(from, to, function CB (er) {211if (er212&& (er.code === "EACCES" || er.code === "EPERM")213&& Date.now() - start < 1000) {214return rename_(from, to, CB)215}216if(cb) cb(er)217})218}219}220221222// if read() returns EAGAIN, then just try it again.223var read = fs.read224fs.read = function (fd, buffer, offset, length, position, callback_) {225var callback226if (callback_ && typeof callback_ === 'function') {227var eagCounter = 0228callback = function (er, _, __) {229if (er && er.code === 'EAGAIN' && eagCounter < 10) {230eagCounter ++231return read.call(fs, fd, buffer, offset, length, position, callback)232}233callback_.apply(this, arguments)234}235}236return read.call(fs, fd, buffer, offset, length, position, callback)237}238239var readSync = fs.readSync240fs.readSync = function (fd, buffer, offset, length, position) {241var eagCounter = 0242while (true) {243try {244return readSync.call(fs, fd, buffer, offset, length, position)245} catch (er) {246if (er.code === 'EAGAIN' && eagCounter < 10) {247eagCounter ++248continue249}250throw er251}252}253}254255256257