1 var fs = require('./fs.js')
2 var constants = require('constants')
4 var origCwd = process.cwd
6 process.cwd = function() {
8 cwd = origCwd.call(process)
15 var chdir = process.chdir
16 process.chdir = function(d) {
18 chdir.call(process, d)
21 module.exports = patch
24 // (re-)implement some things that are known busted or missing.
26 // lchmod, broken prior to 0.6.2
27 // back-port the fix here.
28 if (constants.hasOwnProperty('O_SYMLINK') &&
29 process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
33 // lutimes implementation, or no-op
38 // https://github.com/isaacs/node-graceful-fs/issues/4
39 // Chown should not fail on einval or eperm if non-root.
40 // It should not fail on enosys ever, as this just indicates
41 // that a fs doesn't support the intended operation.
43 fs.chown = chownFix(fs.chown)
44 fs.fchown = chownFix(fs.fchown)
45 fs.lchown = chownFix(fs.lchown)
47 fs.chmod = chmodFix(fs.chmod)
48 fs.fchmod = chmodFix(fs.fchmod)
49 fs.lchmod = chmodFix(fs.lchmod)
51 fs.chownSync = chownFixSync(fs.chownSync)
52 fs.fchownSync = chownFixSync(fs.fchownSync)
53 fs.lchownSync = chownFixSync(fs.lchownSync)
55 fs.chmodSync = chmodFixSync(fs.chmodSync)
56 fs.fchmodSync = chmodFixSync(fs.fchmodSync)
57 fs.lchmodSync = chmodFixSync(fs.lchmodSync)
59 // if lchmod/lchown do not exist, then make them no-ops
61 fs.lchmod = function (path, mode, cb) {
62 if (cb) process.nextTick(cb)
64 fs.lchmodSync = function () {}
67 fs.lchown = function (path, uid, gid, cb) {
68 if (cb) process.nextTick(cb)
70 fs.lchownSync = function () {}
73 // on Windows, A/V software can lock the directory, causing this
74 // to fail with an EACCES or EPERM if the directory contains newly
75 // created files. Try again on failure, for up to 1 second.
76 if (process.platform === "win32") {
77 fs.rename = (function (fs$rename) { return function (from, to, cb) {
78 var start = Date.now()
79 fs$rename(from, to, function CB (er) {
81 && (er.code === "EACCES" || er.code === "EPERM")
82 && Date.now() - start < 1000) {
83 return fs$rename(from, to, CB)
90 // if read() returns EAGAIN, then just try it again.
91 fs.read = (function (fs$read) { return function (fd, buffer, offset, length, position, callback_) {
93 if (callback_ && typeof callback_ === 'function') {
95 callback = function (er, _, __) {
96 if (er && er.code === 'EAGAIN' && eagCounter < 10) {
98 return fs$read.call(fs, fd, buffer, offset, length, position, callback)
100 callback_.apply(this, arguments)
103 return fs$read.call(fs, fd, buffer, offset, length, position, callback)
106 fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
110 return fs$readSync.call(fs, fd, buffer, offset, length, position)
112 if (er.code === 'EAGAIN' && eagCounter < 10) {
122 function patchLchmod (fs) {
123 fs.lchmod = function (path, mode, callback) {
125 , constants.O_WRONLY | constants.O_SYMLINK
127 , function (err, fd) {
129 if (callback) callback(err)
132 // prefer to return the chmod error, if one occurs,
133 // but still try to close, and report closing errors if they occur.
134 fs.fchmod(fd, mode, function (err) {
135 fs.close(fd, function(err2) {
136 if (callback) callback(err || err2)
142 fs.lchmodSync = function (path, mode) {
143 var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
145 // prefer to return the chmod error, if one occurs,
146 // but still try to close, and report closing errors if they occur.
150 ret = fs.fchmodSync(fd, mode)
165 function patchLutimes (fs) {
166 if (constants.hasOwnProperty("O_SYMLINK")) {
167 fs.lutimes = function (path, at, mt, cb) {
168 fs.open(path, constants.O_SYMLINK, function (er, fd) {
173 fs.futimes(fd, at, mt, function (er) {
174 fs.close(fd, function (er2) {
175 if (cb) cb(er || er2)
181 fs.lutimesSync = function (path, at, mt) {
182 var fd = fs.openSync(path, constants.O_SYMLINK)
186 ret = fs.futimesSync(fd, at, mt)
201 fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) }
202 fs.lutimesSync = function () {}
206 function chmodFix (orig) {
207 if (!orig) return orig
208 return function (target, mode, cb) {
209 return orig.call(fs, target, mode, function (er) {
210 if (chownErOk(er)) er = null
211 if (cb) cb.apply(this, arguments)
216 function chmodFixSync (orig) {
217 if (!orig) return orig
218 return function (target, mode) {
220 return orig.call(fs, target, mode)
222 if (!chownErOk(er)) throw er
228 function chownFix (orig) {
229 if (!orig) return orig
230 return function (target, uid, gid, cb) {
231 return orig.call(fs, target, uid, gid, function (er) {
232 if (chownErOk(er)) er = null
233 if (cb) cb.apply(this, arguments)
238 function chownFixSync (orig) {
239 if (!orig) return orig
240 return function (target, uid, gid) {
242 return orig.call(fs, target, uid, gid)
244 if (!chownErOk(er)) throw er
249 // ENOSYS means that the fs doesn't support the op. Just ignore
250 // that, because it doesn't matter.
252 // if there's no getuid, or if getuid() is something other
253 // than 0, and the error is EINVAL or EPERM, then just ignore
256 // This specific case is a silent failure in cp, install, tar,
257 // and most other unix tools that manage permissions.
259 // When running as root, or if other types of errors are
260 // encountered, then it's strict.
261 function chownErOk (er) {
265 if (er.code === "ENOSYS")
268 var nonroot = !process.getuid || process.getuid() !== 0
270 if (er.code === "EINVAL" || er.code === "EPERM")