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)
11 var chdir = process.chdir
12 process.chdir = function(d) {
14 chdir.call(process, d)
17 // (re-)implement some things that are known busted or missing.
19 // lchmod, broken prior to 0.6.2
20 // back-port the fix here.
21 if (constants.hasOwnProperty('O_SYMLINK') &&
22 process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
23 fs.lchmod = function (path, mode, callback) {
24 callback = callback || noop
26 , constants.O_WRONLY | constants.O_SYMLINK
28 , function (err, fd) {
33 // prefer to return the chmod error, if one occurs,
34 // but still try to close, and report closing errors if they occur.
35 fs.fchmod(fd, mode, function (err) {
36 fs.close(fd, function(err2) {
43 fs.lchmodSync = function (path, mode) {
44 var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
46 // prefer to return the chmod error, if one occurs,
47 // but still try to close, and report closing errors if they occur.
50 var ret = fs.fchmodSync(fd, mode)
59 if (err || err2) throw (err || err2)
65 // lutimes implementation, or no-op
67 if (constants.hasOwnProperty("O_SYMLINK")) {
68 fs.lutimes = function (path, at, mt, cb) {
69 fs.open(path, constants.O_SYMLINK, function (er, fd) {
72 fs.futimes(fd, at, mt, function (er) {
73 fs.close(fd, function (er2) {
80 fs.lutimesSync = function (path, at, mt) {
81 var fd = fs.openSync(path, constants.O_SYMLINK)
87 var ret = fs.futimesSync(fd, at, mt)
96 if (err || err2) throw (err || err2)
100 } else if (fs.utimensat && constants.hasOwnProperty("AT_SYMLINK_NOFOLLOW")) {
101 // maybe utimensat will be bound soonish?
102 fs.lutimes = function (path, at, mt, cb) {
103 fs.utimensat(path, at, mt, constants.AT_SYMLINK_NOFOLLOW, cb)
106 fs.lutimesSync = function (path, at, mt) {
107 return fs.utimensatSync(path, at, mt, constants.AT_SYMLINK_NOFOLLOW)
111 fs.lutimes = function (_a, _b, _c, cb) { process.nextTick(cb) }
112 fs.lutimesSync = function () {}
117 // https://github.com/isaacs/node-graceful-fs/issues/4
118 // Chown should not fail on einval or eperm if non-root.
119 // It should not fail on enosys ever, as this just indicates
120 // that a fs doesn't support the intended operation.
122 fs.chown = chownFix(fs.chown)
123 fs.fchown = chownFix(fs.fchown)
124 fs.lchown = chownFix(fs.lchown)
126 fs.chmod = chownFix(fs.chmod)
127 fs.fchmod = chownFix(fs.fchmod)
128 fs.lchmod = chownFix(fs.lchmod)
130 fs.chownSync = chownFixSync(fs.chownSync)
131 fs.fchownSync = chownFixSync(fs.fchownSync)
132 fs.lchownSync = chownFixSync(fs.lchownSync)
134 fs.chmodSync = chownFix(fs.chmodSync)
135 fs.fchmodSync = chownFix(fs.fchmodSync)
136 fs.lchmodSync = chownFix(fs.lchmodSync)
138 function chownFix (orig) {
139 if (!orig) return orig
140 return function (target, uid, gid, cb) {
141 return orig.call(fs, target, uid, gid, function (er, res) {
142 if (chownErOk(er)) er = null
148 function chownFixSync (orig) {
149 if (!orig) return orig
150 return function (target, uid, gid) {
152 return orig.call(fs, target, uid, gid)
154 if (!chownErOk(er)) throw er
159 // ENOSYS means that the fs doesn't support the op. Just ignore
160 // that, because it doesn't matter.
162 // if there's no getuid, or if getuid() is something other
163 // than 0, and the error is EINVAL or EPERM, then just ignore
166 // This specific case is a silent failure in cp, install, tar,
167 // and most other unix tools that manage permissions.
169 // When running as root, or if other types of errors are
170 // encountered, then it's strict.
171 function chownErOk (er) {
175 if (er.code === "ENOSYS")
178 var nonroot = !process.getuid || process.getuid() !== 0
180 if (er.code === "EINVAL" || er.code === "EPERM")
188 // if lchmod/lchown do not exist, then make them no-ops
190 fs.lchmod = function (path, mode, cb) {
193 fs.lchmodSync = function () {}
196 fs.lchown = function (path, uid, gid, cb) {
199 fs.lchownSync = function () {}
204 // on Windows, A/V software can lock the directory, causing this
205 // to fail with an EACCES or EPERM if the directory contains newly
206 // created files. Try again on failure, for up to 1 second.
207 if (process.platform === "win32") {
208 var rename_ = fs.rename
209 fs.rename = function rename (from, to, cb) {
210 var start = Date.now()
211 rename_(from, to, function CB (er) {
213 && (er.code === "EACCES" || er.code === "EPERM")
214 && Date.now() - start < 1000) {
215 return rename_(from, to, CB)
223 // if read() returns EAGAIN, then just try it again.
225 fs.read = function (fd, buffer, offset, length, position, callback_) {
227 if (callback_ && typeof callback_ === 'function') {
229 callback = function (er, _, __) {
230 if (er && er.code === 'EAGAIN' && eagCounter < 10) {
232 return read.call(fs, fd, buffer, offset, length, position, callback)
234 callback_.apply(this, arguments)
237 return read.call(fs, fd, buffer, offset, length, position, callback)
240 var readSync = fs.readSync
241 fs.readSync = function (fd, buffer, offset, length, position) {
245 return readSync.call(fs, fd, buffer, offset, length, position)
247 if (er.code === 'EAGAIN' && eagCounter < 10) {