From 3dcf86fceeef72648fd8693c0df4390b150b4660 Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 19 Jul 2019 15:42:10 -0700 Subject: [PATCH] correct-mkdir: use infer-owner module --- lib/utils/correct-mkdir.js | 39 +++++--------------------------------- test/tap/correct-mkdir.js | 5 +++-- 2 files changed, 8 insertions(+), 36 deletions(-) diff --git a/lib/utils/correct-mkdir.js b/lib/utils/correct-mkdir.js index 0c92201128080..2558de66f5b6f 100644 --- a/lib/utils/correct-mkdir.js +++ b/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 @@ -22,12 +21,7 @@ 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) @@ -35,31 +29,8 @@ module.exports = function correctMkdir (path, cb) { } 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) }) } diff --git a/test/tap/correct-mkdir.js b/test/tap/correct-mkdir.js index a03448a6ac9fc..5c2e9771dfc08 100644 --- a/test/tap/correct-mkdir.js +++ b/test/tap/correct-mkdir.js @@ -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 } @@ -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)