1 var constants = require('constants')
3 var origCwd = process.cwd
6 var platform = process.env.GRACEFUL_FS_PLATFORM || process.platform
8 process.cwd = function() {
10 cwd = origCwd.call(process)
17 var chdir = process.chdir
18 process.chdir = function(d) {
20 chdir.call(process, d)
23 module.exports = patch
26 // (re-)implement some things that are known busted or missing.
28 // lchmod, broken prior to 0.6.2
29 // back-port the fix here.
30 if (constants.hasOwnProperty('O_SYMLINK') &&
31 process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
35 // lutimes implementation, or no-op
40 // https://github.com/isaacs/node-graceful-fs/issues/4
41 // Chown should not fail on einval or eperm if non-root.
42 // It should not fail on enosys ever, as this just indicates
43 // that a fs doesn't support the intended operation.
45 fs.chown = chownFix(fs.chown)
46 fs.fchown = chownFix(fs.fchown)
47 fs.lchown = chownFix(fs.lchown)
49 fs.chmod = chmodFix(fs.chmod)
50 fs.fchmod = chmodFix(fs.fchmod)
51 fs.lchmod = chmodFix(fs.lchmod)
53 fs.chownSync = chownFixSync(fs.chownSync)
54 fs.fchownSync = chownFixSync(fs.fchownSync)
55 fs.lchownSync = chownFixSync(fs.lchownSync)
57 fs.chmodSync = chmodFixSync(fs.chmodSync)
58 fs.fchmodSync = chmodFixSync(fs.fchmodSync)
59 fs.lchmodSync = chmodFixSync(fs.lchmodSync)
61 fs.stat = statFix(fs.stat)
62 fs.fstat = statFix(fs.fstat)
63 fs.lstat = statFix(fs.lstat)
65 fs.statSync = statFixSync(fs.statSync)
66 fs.fstatSync = statFixSync(fs.fstatSync)
67 fs.lstatSync = statFixSync(fs.lstatSync)
69 // if lchmod/lchown do not exist, then make them no-ops
71 fs.lchmod = function (path, mode, cb) {
72 if (cb) process.nextTick(cb)
74 fs.lchmodSync = function () {}
77 fs.lchown = function (path, uid, gid, cb) {
78 if (cb) process.nextTick(cb)
80 fs.lchownSync = function () {}
83 // on Windows, A/V software can lock the directory, causing this
84 // to fail with an EACCES or EPERM if the directory contains newly
85 // created files. Try again on failure, for up to 60 seconds.
87 // Set the timeout this long because some Windows Anti-Virus, such as Parity
88 // bit9, may lock files for up to a minute, causing npm package install
89 // failures. Also, take care to yield the scheduler. Windows scheduling gives
90 // CPU to a busy looping process, which can cause the program causing the lock
91 // contention to be starved of CPU by node, so the contention doesn't resolve.
92 if (platform === "win32") {
93 fs.rename = (function (fs$rename) { return function (from, to, cb) {
94 var start = Date.now()
96 fs$rename(from, to, function CB (er) {
98 && (er.code === "EACCES" || er.code === "EPERM")
99 && Date.now() - start < 60000) {
100 setTimeout(function() {
101 fs.stat(to, function (stater, st) {
102 if (stater && stater.code === "ENOENT")
103 fs$rename(from, to, CB);
117 // if read() returns EAGAIN, then just try it again.
118 fs.read = (function (fs$read) {
119 function read (fd, buffer, offset, length, position, callback_) {
121 if (callback_ && typeof callback_ === 'function') {
123 callback = function (er, _, __) {
124 if (er && er.code === 'EAGAIN' && eagCounter < 10) {
126 return fs$read.call(fs, fd, buffer, offset, length, position, callback)
128 callback_.apply(this, arguments)
131 return fs$read.call(fs, fd, buffer, offset, length, position, callback)
134 // This ensures `util.promisify` works as it does for native `fs.read`.
135 read.__proto__ = fs$read
139 fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
143 return fs$readSync.call(fs, fd, buffer, offset, length, position)
145 if (er.code === 'EAGAIN' && eagCounter < 10) {
154 function patchLchmod (fs) {
155 fs.lchmod = function (path, mode, callback) {
157 , constants.O_WRONLY | constants.O_SYMLINK
159 , function (err, fd) {
161 if (callback) callback(err)
164 // prefer to return the chmod error, if one occurs,
165 // but still try to close, and report closing errors if they occur.
166 fs.fchmod(fd, mode, function (err) {
167 fs.close(fd, function(err2) {
168 if (callback) callback(err || err2)
174 fs.lchmodSync = function (path, mode) {
175 var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
177 // prefer to return the chmod error, if one occurs,
178 // but still try to close, and report closing errors if they occur.
182 ret = fs.fchmodSync(fd, mode)
197 function patchLutimes (fs) {
198 if (constants.hasOwnProperty("O_SYMLINK")) {
199 fs.lutimes = function (path, at, mt, cb) {
200 fs.open(path, constants.O_SYMLINK, function (er, fd) {
205 fs.futimes(fd, at, mt, function (er) {
206 fs.close(fd, function (er2) {
207 if (cb) cb(er || er2)
213 fs.lutimesSync = function (path, at, mt) {
214 var fd = fs.openSync(path, constants.O_SYMLINK)
218 ret = fs.futimesSync(fd, at, mt)
233 fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) }
234 fs.lutimesSync = function () {}
238 function chmodFix (orig) {
239 if (!orig) return orig
240 return function (target, mode, cb) {
241 return orig.call(fs, target, mode, function (er) {
242 if (chownErOk(er)) er = null
243 if (cb) cb.apply(this, arguments)
248 function chmodFixSync (orig) {
249 if (!orig) return orig
250 return function (target, mode) {
252 return orig.call(fs, target, mode)
254 if (!chownErOk(er)) throw er
260 function chownFix (orig) {
261 if (!orig) return orig
262 return function (target, uid, gid, cb) {
263 return orig.call(fs, target, uid, gid, function (er) {
264 if (chownErOk(er)) er = null
265 if (cb) cb.apply(this, arguments)
270 function chownFixSync (orig) {
271 if (!orig) return orig
272 return function (target, uid, gid) {
274 return orig.call(fs, target, uid, gid)
276 if (!chownErOk(er)) throw er
281 function statFix (orig) {
282 if (!orig) return orig
283 // Older versions of Node erroneously returned signed integers for
285 return function (target, options, cb) {
286 if (typeof options === 'function') {
290 function callback (er, stats) {
292 if (stats.uid < 0) stats.uid += 0x100000000
293 if (stats.gid < 0) stats.gid += 0x100000000
295 if (cb) cb.apply(this, arguments)
297 return options ? orig.call(fs, target, options, callback)
298 : orig.call(fs, target, callback)
302 function statFixSync (orig) {
303 if (!orig) return orig
304 // Older versions of Node erroneously returned signed integers for
306 return function (target, options) {
307 var stats = options ? orig.call(fs, target, options)
308 : orig.call(fs, target)
309 if (stats.uid < 0) stats.uid += 0x100000000
310 if (stats.gid < 0) stats.gid += 0x100000000
315 // ENOSYS means that the fs doesn't support the op. Just ignore
316 // that, because it doesn't matter.
318 // if there's no getuid, or if getuid() is something other
319 // than 0, and the error is EINVAL or EPERM, then just ignore
322 // This specific case is a silent failure in cp, install, tar,
323 // and most other unix tools that manage permissions.
325 // When running as root, or if other types of errors are
326 // encountered, then it's strict.
327 function chownErOk (er) {
331 if (er.code === "ENOSYS")
334 var nonroot = !process.getuid || process.getuid() !== 0
336 if (er.code === "EINVAL" || er.code === "EPERM")