polyfills.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.chmod && !fs.lchmod) {
  59. fs.lchmod = function (path, mode, cb) {
  60. if (cb) process.nextTick(cb)
  61. }
  62. fs.lchmodSync = function () {}
  63. }
  64. if (fs.chown && !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 = typeof fs.rename !== 'function' ? fs.rename
  80. : (function (fs$rename) {
  81. function rename (from, to, cb) {
  82. var start = Date.now()
  83. var backoff = 0;
  84. fs$rename(from, to, function CB (er) {
  85. if (er
  86. && (er.code === "EACCES" || er.code === "EPERM" || er.code === "EBUSY")
  87. && Date.now() - start < 60000) {
  88. setTimeout(function() {
  89. fs.stat(to, function (stater, st) {
  90. if (stater && stater.code === "ENOENT")
  91. fs$rename(from, to, CB);
  92. else
  93. cb(er)
  94. })
  95. }, backoff)
  96. if (backoff < 100)
  97. backoff += 10;
  98. return;
  99. }
  100. if (cb) cb(er)
  101. })
  102. }
  103. if (Object.setPrototypeOf) Object.setPrototypeOf(rename, fs$rename)
  104. return rename
  105. })(fs.rename)
  106. }
  107. // if read() returns EAGAIN, then just try it again.
  108. fs.read = typeof fs.read !== 'function' ? fs.read
  109. : (function (fs$read) {
  110. function read (fd, buffer, offset, length, position, callback_) {
  111. var callback
  112. if (callback_ && typeof callback_ === 'function') {
  113. var eagCounter = 0
  114. callback = function (er, _, __) {
  115. if (er && er.code === 'EAGAIN' && eagCounter < 10) {
  116. eagCounter ++
  117. return fs$read.call(fs, fd, buffer, offset, length, position, callback)
  118. }
  119. callback_.apply(this, arguments)
  120. }
  121. }
  122. return fs$read.call(fs, fd, buffer, offset, length, position, callback)
  123. }
  124. // This ensures `util.promisify` works as it does for native `fs.read`.
  125. if (Object.setPrototypeOf) Object.setPrototypeOf(read, fs$read)
  126. return read
  127. })(fs.read)
  128. fs.readSync = typeof fs.readSync !== 'function' ? fs.readSync
  129. : (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
  130. var eagCounter = 0
  131. while (true) {
  132. try {
  133. return fs$readSync.call(fs, fd, buffer, offset, length, position)
  134. } catch (er) {
  135. if (er.code === 'EAGAIN' && eagCounter < 10) {
  136. eagCounter ++
  137. continue
  138. }
  139. throw er
  140. }
  141. }
  142. }})(fs.readSync)
  143. function patchLchmod (fs) {
  144. fs.lchmod = function (path, mode, callback) {
  145. fs.open( path
  146. , constants.O_WRONLY | constants.O_SYMLINK
  147. , mode
  148. , function (err, fd) {
  149. if (err) {
  150. if (callback) callback(err)
  151. return
  152. }
  153. // prefer to return the chmod error, if one occurs,
  154. // but still try to close, and report closing errors if they occur.
  155. fs.fchmod(fd, mode, function (err) {
  156. fs.close(fd, function(err2) {
  157. if (callback) callback(err || err2)
  158. })
  159. })
  160. })
  161. }
  162. fs.lchmodSync = function (path, mode) {
  163. var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
  164. // prefer to return the chmod error, if one occurs,
  165. // but still try to close, and report closing errors if they occur.
  166. var threw = true
  167. var ret
  168. try {
  169. ret = fs.fchmodSync(fd, mode)
  170. threw = false
  171. } finally {
  172. if (threw) {
  173. try {
  174. fs.closeSync(fd)
  175. } catch (er) {}
  176. } else {
  177. fs.closeSync(fd)
  178. }
  179. }
  180. return ret
  181. }
  182. }
  183. function patchLutimes (fs) {
  184. if (constants.hasOwnProperty("O_SYMLINK") && fs.futimes) {
  185. fs.lutimes = function (path, at, mt, cb) {
  186. fs.open(path, constants.O_SYMLINK, function (er, fd) {
  187. if (er) {
  188. if (cb) cb(er)
  189. return
  190. }
  191. fs.futimes(fd, at, mt, function (er) {
  192. fs.close(fd, function (er2) {
  193. if (cb) cb(er || er2)
  194. })
  195. })
  196. })
  197. }
  198. fs.lutimesSync = function (path, at, mt) {
  199. var fd = fs.openSync(path, constants.O_SYMLINK)
  200. var ret
  201. var threw = true
  202. try {
  203. ret = fs.futimesSync(fd, at, mt)
  204. threw = false
  205. } finally {
  206. if (threw) {
  207. try {
  208. fs.closeSync(fd)
  209. } catch (er) {}
  210. } else {
  211. fs.closeSync(fd)
  212. }
  213. }
  214. return ret
  215. }
  216. } else if (fs.futimes) {
  217. fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) }
  218. fs.lutimesSync = function () {}
  219. }
  220. }
  221. function chmodFix (orig) {
  222. if (!orig) return orig
  223. return function (target, mode, cb) {
  224. return orig.call(fs, target, mode, function (er) {
  225. if (chownErOk(er)) er = null
  226. if (cb) cb.apply(this, arguments)
  227. })
  228. }
  229. }
  230. function chmodFixSync (orig) {
  231. if (!orig) return orig
  232. return function (target, mode) {
  233. try {
  234. return orig.call(fs, target, mode)
  235. } catch (er) {
  236. if (!chownErOk(er)) throw er
  237. }
  238. }
  239. }
  240. function chownFix (orig) {
  241. if (!orig) return orig
  242. return function (target, uid, gid, cb) {
  243. return orig.call(fs, target, uid, gid, function (er) {
  244. if (chownErOk(er)) er = null
  245. if (cb) cb.apply(this, arguments)
  246. })
  247. }
  248. }
  249. function chownFixSync (orig) {
  250. if (!orig) return orig
  251. return function (target, uid, gid) {
  252. try {
  253. return orig.call(fs, target, uid, gid)
  254. } catch (er) {
  255. if (!chownErOk(er)) throw er
  256. }
  257. }
  258. }
  259. function statFix (orig) {
  260. if (!orig) return orig
  261. // Older versions of Node erroneously returned signed integers for
  262. // uid + gid.
  263. return function (target, options, cb) {
  264. if (typeof options === 'function') {
  265. cb = options
  266. options = null
  267. }
  268. function callback (er, stats) {
  269. if (stats) {
  270. if (stats.uid < 0) stats.uid += 0x100000000
  271. if (stats.gid < 0) stats.gid += 0x100000000
  272. }
  273. if (cb) cb.apply(this, arguments)
  274. }
  275. return options ? orig.call(fs, target, options, callback)
  276. : orig.call(fs, target, callback)
  277. }
  278. }
  279. function statFixSync (orig) {
  280. if (!orig) return orig
  281. // Older versions of Node erroneously returned signed integers for
  282. // uid + gid.
  283. return function (target, options) {
  284. var stats = options ? orig.call(fs, target, options)
  285. : orig.call(fs, target)
  286. if (stats) {
  287. if (stats.uid < 0) stats.uid += 0x100000000
  288. if (stats.gid < 0) stats.gid += 0x100000000
  289. }
  290. return stats;
  291. }
  292. }
  293. // ENOSYS means that the fs doesn't support the op. Just ignore
  294. // that, because it doesn't matter.
  295. //
  296. // if there's no getuid, or if getuid() is something other
  297. // than 0, and the error is EINVAL or EPERM, then just ignore
  298. // it.
  299. //
  300. // This specific case is a silent failure in cp, install, tar,
  301. // and most other unix tools that manage permissions.
  302. //
  303. // When running as root, or if other types of errors are
  304. // encountered, then it's strict.
  305. function chownErOk (er) {
  306. if (!er)
  307. return true
  308. if (er.code === "ENOSYS")
  309. return true
  310. var nonroot = !process.getuid || process.getuid() !== 0
  311. if (nonroot) {
  312. if (er.code === "EINVAL" || er.code === "EPERM")
  313. return true
  314. }
  315. return false
  316. }
  317. }