polyfills.js 5.8 KB

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