polyfills.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. var fs = require('./fs.js')
  2. var constants = require('constants')
  3. var origCwd = process.cwd
  4. var cwd = null
  5. process.cwd = function() {
  6. if (!cwd)
  7. cwd = origCwd.call(process)
  8. return cwd
  9. }
  10. try {
  11. process.cwd()
  12. } catch (er) {}
  13. var chdir = process.chdir
  14. process.chdir = function(d) {
  15. cwd = null
  16. chdir.call(process, d)
  17. }
  18. module.exports = patch
  19. function patch (fs) {
  20. // (re-)implement some things that are known busted or missing.
  21. // lchmod, broken prior to 0.6.2
  22. // back-port the fix here.
  23. if (constants.hasOwnProperty('O_SYMLINK') &&
  24. process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
  25. patchLchmod(fs)
  26. }
  27. // lutimes implementation, or no-op
  28. if (!fs.lutimes) {
  29. patchLutimes(fs)
  30. }
  31. // https://github.com/isaacs/node-graceful-fs/issues/4
  32. // Chown should not fail on einval or eperm if non-root.
  33. // It should not fail on enosys ever, as this just indicates
  34. // that a fs doesn't support the intended operation.
  35. fs.chown = chownFix(fs.chown)
  36. fs.fchown = chownFix(fs.fchown)
  37. fs.lchown = chownFix(fs.lchown)
  38. fs.chmod = chownFix(fs.chmod)
  39. fs.fchmod = chownFix(fs.fchmod)
  40. fs.lchmod = chownFix(fs.lchmod)
  41. fs.chownSync = chownFixSync(fs.chownSync)
  42. fs.fchownSync = chownFixSync(fs.fchownSync)
  43. fs.lchownSync = chownFixSync(fs.lchownSync)
  44. fs.chmodSync = chownFix(fs.chmodSync)
  45. fs.fchmodSync = chownFix(fs.fchmodSync)
  46. fs.lchmodSync = chownFix(fs.lchmodSync)
  47. // if lchmod/lchown do not exist, then make them no-ops
  48. if (!fs.lchmod) {
  49. fs.lchmod = function (path, mode, cb) {
  50. process.nextTick(cb)
  51. }
  52. fs.lchmodSync = function () {}
  53. }
  54. if (!fs.lchown) {
  55. fs.lchown = function (path, uid, gid, cb) {
  56. process.nextTick(cb)
  57. }
  58. fs.lchownSync = function () {}
  59. }
  60. // on Windows, A/V software can lock the directory, causing this
  61. // to fail with an EACCES or EPERM if the directory contains newly
  62. // created files. Try again on failure, for up to 1 second.
  63. if (process.platform === "win32") {
  64. fs.rename = (function (fs$rename) { return function (from, to, cb) {
  65. var start = Date.now()
  66. fs$rename(from, to, function CB (er) {
  67. if (er
  68. && (er.code === "EACCES" || er.code === "EPERM")
  69. && Date.now() - start < 1000) {
  70. return fs$rename(from, to, CB)
  71. }
  72. if (cb) cb(er)
  73. })
  74. }})(fs.rename)
  75. }
  76. // if read() returns EAGAIN, then just try it again.
  77. fs.read = (function (fs$read) { return function (fd, buffer, offset, length, position, callback_) {
  78. var callback
  79. if (callback_ && typeof callback_ === 'function') {
  80. var eagCounter = 0
  81. callback = function (er, _, __) {
  82. if (er && er.code === 'EAGAIN' && eagCounter < 10) {
  83. eagCounter ++
  84. return fs$read.call(fs, fd, buffer, offset, length, position, callback)
  85. }
  86. callback_.apply(this, arguments)
  87. }
  88. }
  89. return fs$read.call(fs, fd, buffer, offset, length, position, callback)
  90. }})(fs.read)
  91. fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
  92. var eagCounter = 0
  93. while (true) {
  94. try {
  95. return fs$readSync.call(fs, fd, buffer, offset, length, position)
  96. } catch (er) {
  97. if (er.code === 'EAGAIN' && eagCounter < 10) {
  98. eagCounter ++
  99. continue
  100. }
  101. throw er
  102. }
  103. }
  104. }})(fs.readSync)
  105. }
  106. function patchLchmod (fs) {
  107. fs.lchmod = function (path, mode, callback) {
  108. callback = callback || noop
  109. fs.open( path
  110. , constants.O_WRONLY | constants.O_SYMLINK
  111. , mode
  112. , function (err, fd) {
  113. if (err) {
  114. callback(err)
  115. return
  116. }
  117. // prefer to return the chmod error, if one occurs,
  118. // but still try to close, and report closing errors if they occur.
  119. fs.fchmod(fd, mode, function (err) {
  120. fs.close(fd, function(err2) {
  121. callback(err || err2)
  122. })
  123. })
  124. })
  125. }
  126. fs.lchmodSync = function (path, mode) {
  127. var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
  128. // prefer to return the chmod error, if one occurs,
  129. // but still try to close, and report closing errors if they occur.
  130. var threw = true
  131. var ret
  132. try {
  133. ret = fs.fchmodSync(fd, mode)
  134. threw = false
  135. } finally {
  136. if (threw) {
  137. try {
  138. fs.closeSync(fd)
  139. } catch (er) {}
  140. } else {
  141. fs.closeSync(fd)
  142. }
  143. }
  144. return ret
  145. }
  146. }
  147. function patchLutimes (fs) {
  148. if (constants.hasOwnProperty("O_SYMLINK")) {
  149. fs.lutimes = function (path, at, mt, cb) {
  150. fs.open(path, constants.O_SYMLINK, function (er, fd) {
  151. cb = cb || noop
  152. if (er) return cb(er)
  153. fs.futimes(fd, at, mt, function (er) {
  154. fs.close(fd, function (er2) {
  155. return cb(er || er2)
  156. })
  157. })
  158. })
  159. }
  160. fs.lutimesSync = function (path, at, mt) {
  161. var fd = fs.openSync(path, constants.O_SYMLINK)
  162. var ret
  163. var threw = true
  164. try {
  165. ret = fs.futimesSync(fd, at, mt)
  166. threw = false
  167. } finally {
  168. if (threw) {
  169. try {
  170. fs.closeSync(fd)
  171. } catch (er) {}
  172. } else {
  173. fs.closeSync(fd)
  174. }
  175. }
  176. return ret
  177. }
  178. } else {
  179. fs.lutimes = function (_a, _b, _c, cb) { process.nextTick(cb) }
  180. fs.lutimesSync = function () {}
  181. }
  182. }
  183. function chownFix (orig) {
  184. if (!orig) return orig
  185. return function (target, uid, gid, cb) {
  186. return orig.call(fs, target, uid, gid, function (er, res) {
  187. if (chownErOk(er)) er = null
  188. cb(er, res)
  189. })
  190. }
  191. }
  192. function chownFixSync (orig) {
  193. if (!orig) return orig
  194. return function (target, uid, gid) {
  195. try {
  196. return orig.call(fs, target, uid, gid)
  197. } catch (er) {
  198. if (!chownErOk(er)) throw er
  199. }
  200. }
  201. }
  202. // ENOSYS means that the fs doesn't support the op. Just ignore
  203. // that, because it doesn't matter.
  204. //
  205. // if there's no getuid, or if getuid() is something other
  206. // than 0, and the error is EINVAL or EPERM, then just ignore
  207. // it.
  208. //
  209. // This specific case is a silent failure in cp, install, tar,
  210. // and most other unix tools that manage permissions.
  211. //
  212. // When running as root, or if other types of errors are
  213. // encountered, then it's strict.
  214. function chownErOk (er) {
  215. if (!er)
  216. return true
  217. if (er.code === "ENOSYS")
  218. return true
  219. var nonroot = !process.getuid || process.getuid() !== 0
  220. if (nonroot) {
  221. if (er.code === "EINVAL" || er.code === "EPERM")
  222. return true
  223. }
  224. return false
  225. }