polyfills.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. 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. // It should not fail on enosys ever, as this just indicates
  106. // that a fs doesn't support the intended operation.
  107. fs.chown = chownFix(fs.chown)
  108. fs.fchown = chownFix(fs.fchown)
  109. fs.lchown = chownFix(fs.lchown)
  110. fs.chmod = chownFix(fs.chmod)
  111. fs.fchmod = chownFix(fs.fchmod)
  112. fs.lchmod = chownFix(fs.lchmod)
  113. fs.chownSync = chownFixSync(fs.chownSync)
  114. fs.fchownSync = chownFixSync(fs.fchownSync)
  115. fs.lchownSync = chownFixSync(fs.lchownSync)
  116. fs.chmodSync = chownFix(fs.chmodSync)
  117. fs.fchmodSync = chownFix(fs.fchmodSync)
  118. fs.lchmodSync = chownFix(fs.lchmodSync)
  119. function chownFix (orig) {
  120. if (!orig) return orig
  121. return function (target, uid, gid, cb) {
  122. return orig.call(fs, target, uid, gid, function (er, res) {
  123. if (chownErOk(er)) er = null
  124. cb(er, res)
  125. })
  126. }
  127. }
  128. function chownFixSync (orig) {
  129. if (!orig) return orig
  130. return function (target, uid, gid) {
  131. try {
  132. return orig.call(fs, target, uid, gid)
  133. } catch (er) {
  134. if (!chownErOk(er)) throw er
  135. }
  136. }
  137. }
  138. // ENOSYS means that the fs doesn't support the op. Just ignore
  139. // that, because it doesn't matter.
  140. //
  141. // if there's no getuid, or if getuid() is something other
  142. // than 0, and the error is EINVAL or EPERM, then just ignore
  143. // it.
  144. //
  145. // This specific case is a silent failure in cp, install, tar,
  146. // and most other unix tools that manage permissions.
  147. //
  148. // When running as root, or if other types of errors are
  149. // encountered, then it's strict.
  150. function chownErOk (er) {
  151. if (!er)
  152. return true
  153. if (er.code === "ENOSYS")
  154. return true
  155. var nonroot = !process.getuid || process.getuid() !== 0
  156. if (nonroot) {
  157. if (er.code === "EINVAL" || er.code === "EPERM")
  158. return true
  159. }
  160. return false
  161. }
  162. // if lchmod/lchown do not exist, then make them no-ops
  163. if (!fs.lchmod) {
  164. fs.lchmod = function (path, mode, cb) {
  165. process.nextTick(cb)
  166. }
  167. fs.lchmodSync = function () {}
  168. }
  169. if (!fs.lchown) {
  170. fs.lchown = function (path, uid, gid, cb) {
  171. process.nextTick(cb)
  172. }
  173. fs.lchownSync = function () {}
  174. }
  175. // on Windows, A/V software can lock the directory, causing this
  176. // to fail with an EACCES or EPERM if the directory contains newly
  177. // created files. Try again on failure, for up to 1 second.
  178. if (process.platform === "win32") {
  179. var rename_ = fs.rename
  180. fs.rename = function rename (from, to, cb) {
  181. var start = Date.now()
  182. rename_(from, to, function CB (er) {
  183. if (er
  184. && (er.code === "EACCES" || er.code === "EPERM")
  185. && Date.now() - start < 1000) {
  186. return rename_(from, to, CB)
  187. }
  188. if(cb) cb(er)
  189. })
  190. }
  191. }
  192. // if read() returns EAGAIN, then just try it again.
  193. var read = fs.read
  194. fs.read = function (fd, buffer, offset, length, position, callback_) {
  195. var callback
  196. if (callback_ && typeof callback_ === 'function') {
  197. var eagCounter = 0
  198. callback = function (er, _, __) {
  199. if (er && er.code === 'EAGAIN' && eagCounter < 10) {
  200. eagCounter ++
  201. return read.call(fs, fd, buffer, offset, length, position, callback)
  202. }
  203. callback_.apply(this, arguments)
  204. }
  205. }
  206. return read.call(fs, fd, buffer, offset, length, position, callback)
  207. }
  208. var readSync = fs.readSync
  209. fs.readSync = function (fd, buffer, offset, length, position) {
  210. var eagCounter = 0
  211. while (true) {
  212. try {
  213. return readSync.call(fs, fd, buffer, offset, length, position)
  214. } catch (er) {
  215. if (er.code === 'EAGAIN' && eagCounter < 10) {
  216. eagCounter ++
  217. continue
  218. }
  219. throw er
  220. }
  221. }
  222. }