polyfills.js 9.5 KB

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