web 3d图形渲染器
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.

145 lines
3.4 KiB

  1. 'use strict'
  2. const util = require('util')
  3. const chownr = util.promisify(require('chownr'))
  4. const mkdirp = require('mkdirp')
  5. const inflight = require('promise-inflight')
  6. const inferOwner = require('infer-owner')
  7. // Memoize getuid()/getgid() calls.
  8. // patch process.setuid/setgid to invalidate cached value on change
  9. const self = { uid: null, gid: null }
  10. const getSelf = () => {
  11. if (typeof self.uid !== 'number') {
  12. self.uid = process.getuid()
  13. const setuid = process.setuid
  14. process.setuid = (uid) => {
  15. self.uid = null
  16. process.setuid = setuid
  17. return process.setuid(uid)
  18. }
  19. }
  20. if (typeof self.gid !== 'number') {
  21. self.gid = process.getgid()
  22. const setgid = process.setgid
  23. process.setgid = (gid) => {
  24. self.gid = null
  25. process.setgid = setgid
  26. return process.setgid(gid)
  27. }
  28. }
  29. }
  30. module.exports.chownr = fixOwner
  31. function fixOwner (cache, filepath) {
  32. if (!process.getuid) {
  33. // This platform doesn't need ownership fixing
  34. return Promise.resolve()
  35. }
  36. getSelf()
  37. if (self.uid !== 0) {
  38. // almost certainly can't chown anyway
  39. return Promise.resolve()
  40. }
  41. return Promise.resolve(inferOwner(cache)).then((owner) => {
  42. const { uid, gid } = owner
  43. // No need to override if it's already what we used.
  44. if (self.uid === uid && self.gid === gid) {
  45. return
  46. }
  47. return inflight('fixOwner: fixing ownership on ' + filepath, () =>
  48. chownr(
  49. filepath,
  50. typeof uid === 'number' ? uid : self.uid,
  51. typeof gid === 'number' ? gid : self.gid
  52. ).catch((err) => {
  53. if (err.code === 'ENOENT') {
  54. return null
  55. }
  56. throw err
  57. })
  58. )
  59. })
  60. }
  61. module.exports.chownr.sync = fixOwnerSync
  62. function fixOwnerSync (cache, filepath) {
  63. if (!process.getuid) {
  64. // This platform doesn't need ownership fixing
  65. return
  66. }
  67. const { uid, gid } = inferOwner.sync(cache)
  68. getSelf()
  69. if (self.uid !== 0) {
  70. // almost certainly can't chown anyway
  71. return
  72. }
  73. if (self.uid === uid && self.gid === gid) {
  74. // No need to override if it's already what we used.
  75. return
  76. }
  77. try {
  78. chownr.sync(
  79. filepath,
  80. typeof uid === 'number' ? uid : self.uid,
  81. typeof gid === 'number' ? gid : self.gid
  82. )
  83. } catch (err) {
  84. // only catch ENOENT, any other error is a problem.
  85. if (err.code === 'ENOENT') {
  86. return null
  87. }
  88. throw err
  89. }
  90. }
  91. module.exports.mkdirfix = mkdirfix
  92. function mkdirfix (cache, p, cb) {
  93. // we have to infer the owner _before_ making the directory, even though
  94. // we aren't going to use the results, since the cache itself might not
  95. // exist yet. If we mkdirp it, then our current uid/gid will be assumed
  96. // to be correct if it creates the cache folder in the process.
  97. return Promise.resolve(inferOwner(cache)).then(() => {
  98. return mkdirp(p)
  99. .then((made) => {
  100. if (made) {
  101. return fixOwner(cache, made).then(() => made)
  102. }
  103. })
  104. .catch((err) => {
  105. if (err.code === 'EEXIST') {
  106. return fixOwner(cache, p).then(() => null)
  107. }
  108. throw err
  109. })
  110. })
  111. }
  112. module.exports.mkdirfix.sync = mkdirfixSync
  113. function mkdirfixSync (cache, p) {
  114. try {
  115. inferOwner.sync(cache)
  116. const made = mkdirp.sync(p)
  117. if (made) {
  118. fixOwnerSync(cache, made)
  119. return made
  120. }
  121. } catch (err) {
  122. if (err.code === 'EEXIST') {
  123. fixOwnerSync(cache, p)
  124. return null
  125. } else {
  126. throw err
  127. }
  128. }
  129. }