Skip to content

Commit

Permalink
correct-mkdir: use infer-owner module
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 21, 2019
1 parent 0fefdee commit 3dcf86f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 36 deletions.
39 changes: 5 additions & 34 deletions lib/utils/correct-mkdir.js
@@ -1,9 +1,8 @@
const chownr = require('chownr')
const fs = require('graceful-fs')
const inflight = require('inflight')
const log = require('npmlog')
const mkdirp = require('mkdirp')
const { dirname } = require('path')
const inferOwner = require('infer-owner')

// retain ownership of the parent dir
// this matches behavior in cacache to infer the cache ownership
Expand All @@ -22,44 +21,16 @@ module.exports = function correctMkdir (path, cb) {
return mkdirp(path, (er, made) => cb(er, { uid: 0, gid: 0 }))
}

inferOwner(path, (er, owner) => {
if (er) {
log.error('correctMkdir', 'failed to infer path ownership %s', path)
return cb(er)
}

inferOwner(path).then(owner => {
mkdirp(path, (er, made) => {
if (er) {
log.error('correctMkdir', 'failed to make directory %s', path)
return cb(er)
}
chownr(made || path, owner.uid, owner.gid, (er) => cb(er, owner))
})
})
}

const pathToOwner = new Map()
const inferOwner = (path, cb) => {
if (pathToOwner.has(path)) {
return cb(null, pathToOwner.get(path))
}

const parent = dirname(path)
fs.lstat(path, (er, st) => {
if (er && parent !== path) {
inferOwner(parent, (er, owner) => {
if (er) {
return cb(er)
}
pathToOwner.set(path, owner)
return cb(null, owner)
})
} else if (er) {
cb(er)
} else {
const owner = { uid: st.uid, gid: st.gid }
pathToOwner.set(path, owner)
cb(null, owner)
}
}, er => {
log.error('correctMkdir', 'failed to infer path ownership %s', path)
return cb(er)
})
}
5 changes: 3 additions & 2 deletions test/tap/correct-mkdir.js
Expand Up @@ -12,7 +12,7 @@ test('correct-mkdir: no race conditions', function (t) {
if (path === cache_dir) {
// Return a non-matching owner
cb(null, {
uid: +process.uid + 1,
uid: +process.getuid() + 1,
isDirectory: function () {
return true
}
Expand All @@ -35,7 +35,8 @@ test('correct-mkdir: no race conditions', function (t) {
}
var mocks = {
'graceful-fs': mock_fs,
'chownr': mock_chownr
'chownr': mock_chownr,
'infer-owner': requireInject('infer-owner', { fs: mock_fs })
}
var correctMkdir = requireInject('../../lib/utils/correct-mkdir.js', mocks)

Expand Down

0 comments on commit 3dcf86f

Please sign in to comment.