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