fix-owner.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. return inflight('fixOwner: fixing ownership on ' + filepath, () =>
  47. chownr(
  48. filepath,
  49. typeof uid === 'number' ? uid : self.uid,
  50. typeof gid === 'number' ? gid : self.gid
  51. ).catch((err) => {
  52. if (err.code === 'ENOENT')
  53. return null
  54. throw err
  55. })
  56. )
  57. })
  58. }
  59. module.exports.chownr.sync = fixOwnerSync
  60. function fixOwnerSync (cache, filepath) {
  61. if (!process.getuid) {
  62. // This platform doesn't need ownership fixing
  63. return
  64. }
  65. const { uid, gid } = inferOwner.sync(cache)
  66. getSelf()
  67. if (self.uid !== 0) {
  68. // almost certainly can't chown anyway
  69. return
  70. }
  71. if (self.uid === uid && self.gid === gid) {
  72. // No need to override if it's already what we used.
  73. return
  74. }
  75. try {
  76. chownr.sync(
  77. filepath,
  78. typeof uid === 'number' ? uid : self.uid,
  79. typeof gid === 'number' ? gid : self.gid
  80. )
  81. } catch (err) {
  82. // only catch ENOENT, any other error is a problem.
  83. if (err.code === 'ENOENT')
  84. return null
  85. throw err
  86. }
  87. }
  88. module.exports.mkdirfix = mkdirfix
  89. function mkdirfix (cache, p, cb) {
  90. // we have to infer the owner _before_ making the directory, even though
  91. // we aren't going to use the results, since the cache itself might not
  92. // exist yet. If we mkdirp it, then our current uid/gid will be assumed
  93. // to be correct if it creates the cache folder in the process.
  94. return Promise.resolve(inferOwner(cache)).then(() => {
  95. return mkdirp(p)
  96. .then((made) => {
  97. if (made)
  98. return fixOwner(cache, made).then(() => made)
  99. })
  100. .catch((err) => {
  101. if (err.code === 'EEXIST')
  102. return fixOwner(cache, p).then(() => null)
  103. throw err
  104. })
  105. })
  106. }
  107. module.exports.mkdirfix.sync = mkdirfixSync
  108. function mkdirfixSync (cache, p) {
  109. try {
  110. inferOwner.sync(cache)
  111. const made = mkdirp.sync(p)
  112. if (made) {
  113. fixOwnerSync(cache, made)
  114. return made
  115. }
  116. } catch (err) {
  117. if (err.code === 'EEXIST') {
  118. fixOwnerSync(cache, p)
  119. return null
  120. } else
  121. throw err
  122. }
  123. }