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.

fix-owner.js 3.2KB

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