correct-mkdir.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // XXX this can probably be replaced with gentle-fs.mkdir everywhere it's used
  2. const chownr = require('chownr')
  3. const inflight = require('inflight')
  4. const log = require('npmlog')
  5. const mkdirp = require('mkdirp')
  6. const inferOwner = require('infer-owner')
  7. // retain ownership of the parent dir
  8. // this matches behavior in cacache to infer the cache ownership
  9. // based on the ownership of the cache folder or it is parent.
  10. module.exports = function correctMkdir (path, cb) {
  11. cb = inflight('correctMkdir: ' + path, cb)
  12. if (!cb) {
  13. return log.verbose('correctMkdir', path, 'correctMkdir already in flight; waiting')
  14. } else {
  15. log.verbose('correctMkdir', path, 'correctMkdir not in flight; initializing')
  16. }
  17. if (!process.getuid) {
  18. log.verbose('makeCacheDir', 'UID & GID are irrelevant on', process.platform)
  19. return mkdirp(path, (er, made) => cb(er, { uid: 0, gid: 0 }))
  20. }
  21. inferOwner(path).then(owner => {
  22. mkdirp(path, (er, made) => {
  23. if (er) {
  24. log.error('correctMkdir', 'failed to make directory %s', path)
  25. return cb(er)
  26. }
  27. chownr(made || path, owner.uid, owner.gid, (er) => cb(er, owner))
  28. })
  29. }, er => {
  30. log.error('correctMkdir', 'failed to infer path ownership %s', path)
  31. return cb(er)
  32. })
  33. }