You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

polyfills.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. var constants = require('constants')
  2. var origCwd = process.cwd
  3. var cwd = null
  4. var platform = process.env.GRACEFUL_FS_PLATFORM || process.platform
  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. fs.stat = statFix(fs.stat)
  48. fs.fstat = statFix(fs.fstat)
  49. fs.lstat = statFix(fs.lstat)
  50. fs.statSync = statFixSync(fs.statSync)
  51. fs.fstatSync = statFixSync(fs.fstatSync)
  52. fs.lstatSync = statFixSync(fs.lstatSync)
  53. // if lchmod/lchown do not exist, then make them no-ops
  54. if (!fs.lchmod) {
  55. fs.lchmod = function (path, mode, cb) {
  56. if (cb) process.nextTick(cb)
  57. }
  58. fs.lchmodSync = function () {}
  59. }
  60. if (!fs.lchown) {
  61. fs.lchown = function (path, uid, gid, cb) {
  62. if (cb) process.nextTick(cb)
  63. }
  64. fs.lchownSync = function () {}
  65. }
  66. // on Windows, A/V software can lock the directory, causing this
  67. // to fail with an EACCES or EPERM if the directory contains newly
  68. // created files. Try again on failure, for up to 60 seconds.
  69. // Set the timeout this long because some Windows Anti-Virus, such as Parity
  70. // bit9, may lock files for up to a minute, causing npm package install
  71. // failures. Also, take care to yield the scheduler. Windows scheduling gives
  72. // CPU to a busy looping process, which can cause the program causing the lock
  73. // contention to be starved of CPU by node, so the contention doesn't resolve.
  74. if (platform === "win32") {
  75. fs.rename = (function (fs$rename) { return function (from, to, cb) {
  76. var start = Date.now()
  77. var backoff = 0;
  78. fs$rename(from, to, function CB (er) {
  79. if (er
  80. && (er.code === "EACCES" || er.code === "EPERM")
  81. && Date.now() - start < 60000) {
  82. setTimeout(function() {
  83. fs.stat(to, function (stater, st) {
  84. if (stater && stater.code === "ENOENT")
  85. fs$rename(from, to, CB);
  86. else
  87. cb(er)
  88. })
  89. }, backoff)
  90. if (backoff < 100)
  91. backoff += 10;
  92. return;
  93. }
  94. if (cb) cb(er)
  95. })
  96. }})(fs.rename)
  97. }
  98. // if read() returns EAGAIN, then just try it again.
  99. fs.read = (function (fs$read) {
  100. function read (fd, buffer, offset, length, position, callback_) {
  101. var callback
  102. if (callback_ && typeof callback_ === 'function') {
  103. var eagCounter = 0
  104. callback = function (er, _, __) {
  105. if (er && er.code === 'EAGAIN' && eagCounter < 10) {
  106. eagCounter ++
  107. return fs$read.call(fs, fd, buffer, offset, length, position, callback)
  108. }
  109. callback_.apply(this, arguments)
  110. }
  111. }
  112. return fs$read.call(fs, fd, buffer, offset, length, position, callback)
  113. }
  114. // This ensures `util.promisify` works as it does for native `fs.read`.
  115. read.__proto__ = fs$read
  116. return read
  117. })(fs.read)
  118. fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
  119. var eagCounter = 0
  120. while (true) {
  121. try {
  122. return fs$readSync.call(fs, fd, buffer, offset, length, position)
  123. } catch (er) {
  124. if (er.code === 'EAGAIN' && eagCounter < 10) {
  125. eagCounter ++
  126. continue
  127. }
  128. throw er
  129. }
  130. }
  131. }})(fs.readSync)
  132. function patchLchmod (fs) {
  133. fs.lchmod = function (path, mode, callback) {
  134. fs.open( path
  135. , constants.O_WRONLY | constants.O_SYMLINK
  136. , mode
  137. , function (err, fd) {
  138. if (err) {
  139. if (callback) callback(err)
  140. return
  141. }
  142. // prefer to return the chmod error, if one occurs,
  143. // but still try to close, and report closing errors if they occur.
  144. fs.fchmod(fd, mode, function (err) {
  145. fs.close(fd, function(err2) {
  146. if (callback) callback(err || err2)
  147. })
  148. })
  149. })
  150. }
  151. fs.lchmodSync = function (path, mode) {
  152. var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
  153. // prefer to return the chmod error, if one occurs,
  154. // but still try to close, and report closing errors if they occur.
  155. var threw = true
  156. var ret
  157. try {
  158. ret = fs.fchmodSync(fd, mode)
  159. threw = false
  160. } finally {
  161. if (threw) {
  162. try {
  163. fs.closeSync(fd)
  164. } catch (er) {}
  165. } else {
  166. fs.closeSync(fd)
  167. }
  168. }
  169. return ret
  170. }
  171. }
  172. function patchLutimes (fs) {
  173. if (constants.hasOwnProperty("O_SYMLINK")) {
  174. fs.lutimes = function (path, at, mt, cb) {
  175. fs.open(path, constants.O_SYMLINK, function (er, fd) {
  176. if (er) {
  177. if (cb) cb(er)
  178. return
  179. }
  180. fs.futimes(fd, at, mt, function (er) {
  181. fs.close(fd, function (er2) {
  182. if (cb) cb(er || er2)
  183. })
  184. })
  185. })
  186. }
  187. fs.lutimesSync = function (path, at, mt) {
  188. var fd = fs.openSync(path, constants.O_SYMLINK)
  189. var ret
  190. var threw = true
  191. try {
  192. ret = fs.futimesSync(fd, at, mt)
  193. threw = false
  194. } finally {
  195. if (threw) {
  196. try {
  197. fs.closeSync(fd)
  198. } catch (er) {}
  199. } else {
  200. fs.closeSync(fd)
  201. }
  202. }
  203. return ret
  204. }
  205. } else {
  206. fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) }
  207. fs.lutimesSync = function () {}
  208. }
  209. }
  210. function chmodFix (orig) {
  211. if (!orig) return orig
  212. return function (target, mode, cb) {
  213. return orig.call(fs, target, mode, function (er) {
  214. if (chownErOk(er)) er = null
  215. if (cb) cb.apply(this, arguments)
  216. })
  217. }
  218. }
  219. function chmodFixSync (orig) {
  220. if (!orig) return orig
  221. return function (target, mode) {
  222. try {
  223. return orig.call(fs, target, mode)
  224. } catch (er) {
  225. if (!chownErOk(er)) throw er
  226. }
  227. }
  228. }
  229. function chownFix (orig) {
  230. if (!orig) return orig
  231. return function (target, uid, gid, cb) {
  232. return orig.call(fs, target, uid, gid, function (er) {
  233. if (chownErOk(er)) er = null
  234. if (cb) cb.apply(this, arguments)
  235. })
  236. }
  237. }
  238. function chownFixSync (orig) {
  239. if (!orig) return orig
  240. return function (target, uid, gid) {
  241. try {
  242. return orig.call(fs, target, uid, gid)
  243. } catch (er) {
  244. if (!chownErOk(er)) throw er
  245. }
  246. }
  247. }
  248. function statFix (orig) {
  249. if (!orig) return orig
  250. // Older versions of Node erroneously returned signed integers for
  251. // uid + gid.
  252. return function (target, options, cb) {
  253. if (typeof options === 'function') {
  254. cb = options
  255. options = null
  256. }
  257. function callback (er, stats) {
  258. if (stats) {
  259. if (stats.uid < 0) stats.uid += 0x100000000
  260. if (stats.gid < 0) stats.gid += 0x100000000
  261. }
  262. if (cb) cb.apply(this, arguments)
  263. }
  264. return options ? orig.call(fs, target, options, callback)
  265. : orig.call(fs, target, callback)
  266. }
  267. }
  268. function statFixSync (orig) {
  269. if (!orig) return orig
  270. // Older versions of Node erroneously returned signed integers for
  271. // uid + gid.
  272. return function (target, options) {
  273. var stats = options ? orig.call(fs, target, options)
  274. : orig.call(fs, target)
  275. if (stats.uid < 0) stats.uid += 0x100000000
  276. if (stats.gid < 0) stats.gid += 0x100000000
  277. return stats;
  278. }
  279. }
  280. // ENOSYS means that the fs doesn't support the op. Just ignore
  281. // that, because it doesn't matter.
  282. //
  283. // if there's no getuid, or if getuid() is something other
  284. // than 0, and the error is EINVAL or EPERM, then just ignore
  285. // it.
  286. //
  287. // This specific case is a silent failure in cp, install, tar,
  288. // and most other unix tools that manage permissions.
  289. //
  290. // When running as root, or if other types of errors are
  291. // encountered, then it's strict.
  292. function chownErOk (er) {
  293. if (!er)
  294. return true
  295. if (er.code === "ENOSYS")
  296. return true
  297. var nonroot = !process.getuid || process.getuid() !== 0
  298. if (nonroot) {
  299. if (er.code === "EINVAL" || er.code === "EPERM")
  300. return true
  301. }
  302. return false
  303. }
  304. }