polyfills.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 = chmodFix(fs.chmod)
  39. fs.fchmod = chmodFix(fs.fchmod)
  40. fs.lchmod = chmodFix(fs.lchmod)
  41. fs.chownSync = chownFixSync(fs.chownSync)
  42. fs.fchownSync = chownFixSync(fs.fchownSync)
  43. fs.lchownSync = chownFixSync(fs.lchownSync)
  44. fs.chmodSync = chmodFixSync(fs.chmodSync)
  45. fs.fchmodSync = chmodFixSync(fs.fchmodSync)
  46. fs.lchmodSync = chmodFixSync(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. if (cb) process.nextTick(cb)
  51. }
  52. fs.lchmodSync = function () {}
  53. }
  54. if (!fs.lchown) {
  55. fs.lchown = function (path, uid, gid, cb) {
  56. if (cb) 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. fs.open( path
  109. , constants.O_WRONLY | constants.O_SYMLINK
  110. , mode
  111. , function (err, fd) {
  112. if (err) {
  113. if (callback) callback(err)
  114. return
  115. }
  116. // prefer to return the chmod error, if one occurs,
  117. // but still try to close, and report closing errors if they occur.
  118. fs.fchmod(fd, mode, function (err) {
  119. fs.close(fd, function(err2) {
  120. if (callback) callback(err || err2)
  121. })
  122. })
  123. })
  124. }
  125. fs.lchmodSync = function (path, mode) {
  126. var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
  127. // prefer to return the chmod error, if one occurs,
  128. // but still try to close, and report closing errors if they occur.
  129. var threw = true
  130. var ret
  131. try {
  132. ret = fs.fchmodSync(fd, mode)
  133. threw = false
  134. } finally {
  135. if (threw) {
  136. try {
  137. fs.closeSync(fd)
  138. } catch (er) {}
  139. } else {
  140. fs.closeSync(fd)
  141. }
  142. }
  143. return ret
  144. }
  145. }
  146. function patchLutimes (fs) {
  147. if (constants.hasOwnProperty("O_SYMLINK")) {
  148. fs.lutimes = function (path, at, mt, cb) {
  149. fs.open(path, constants.O_SYMLINK, function (er, fd) {
  150. if (er) {
  151. if (cb) cb(er)
  152. return
  153. }
  154. fs.futimes(fd, at, mt, function (er) {
  155. fs.close(fd, function (er2) {
  156. if (cb) cb(er || er2)
  157. })
  158. })
  159. })
  160. }
  161. fs.lutimesSync = function (path, at, mt) {
  162. var fd = fs.openSync(path, constants.O_SYMLINK)
  163. var ret
  164. var threw = true
  165. try {
  166. ret = fs.futimesSync(fd, at, mt)
  167. threw = false
  168. } finally {
  169. if (threw) {
  170. try {
  171. fs.closeSync(fd)
  172. } catch (er) {}
  173. } else {
  174. fs.closeSync(fd)
  175. }
  176. }
  177. return ret
  178. }
  179. } else {
  180. fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) }
  181. fs.lutimesSync = function () {}
  182. }
  183. }
  184. function chmodFix (orig) {
  185. if (!orig) return orig
  186. return function (target, mode, cb) {
  187. return orig.call(fs, target, mode, function (er) {
  188. if (chownErOk(er)) er = null
  189. if (cb) cb.apply(this, arguments)
  190. })
  191. }
  192. }
  193. function chmodFixSync (orig) {
  194. if (!orig) return orig
  195. return function (target, mode) {
  196. try {
  197. return orig.call(fs, target, mode)
  198. } catch (er) {
  199. if (!chownErOk(er)) throw er
  200. }
  201. }
  202. }
  203. function chownFix (orig) {
  204. if (!orig) return orig
  205. return function (target, uid, gid, cb) {
  206. return orig.call(fs, target, uid, gid, function (er) {
  207. if (chownErOk(er)) er = null
  208. if (cb) cb.apply(this, arguments)
  209. })
  210. }
  211. }
  212. function chownFixSync (orig) {
  213. if (!orig) return orig
  214. return function (target, uid, gid) {
  215. try {
  216. return orig.call(fs, target, uid, gid)
  217. } catch (er) {
  218. if (!chownErOk(er)) throw er
  219. }
  220. }
  221. }
  222. // ENOSYS means that the fs doesn't support the op. Just ignore
  223. // that, because it doesn't matter.
  224. //
  225. // if there's no getuid, or if getuid() is something other
  226. // than 0, and the error is EINVAL or EPERM, then just ignore
  227. // it.
  228. //
  229. // This specific case is a silent failure in cp, install, tar,
  230. // and most other unix tools that manage permissions.
  231. //
  232. // When running as root, or if other types of errors are
  233. // encountered, then it's strict.
  234. function chownErOk (er) {
  235. if (!er)
  236. return true
  237. if (er.code === "ENOSYS")
  238. return true
  239. var nonroot = !process.getuid || process.getuid() !== 0
  240. if (nonroot) {
  241. if (er.code === "EINVAL" || er.code === "EPERM")
  242. return true
  243. }
  244. return false
  245. }