Ohm-Management - Projektarbeit B-ME
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 8.9KB

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