From f6164d5ddd072eabdf2237f1694a31efd746eb1d Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 3 Jul 2019 14:35:25 -0700 Subject: [PATCH] chownr@1.1.2 This handles an EISDIR error from cacache on Darwin in Node versions prior to 10.6. Fix: [npm.community#7901](https://npm.community/t/7901/) Fix: [npm.community#8203](https://npm.community/t/8203/) Fix: https://github.com/isaacs/chownr/issues/20 PR-URL: https://github.com/isaacs/chownr/pull/21 --- node_modules/chownr/chownr.js | 57 ++++++++++++++++++++++++++------ node_modules/chownr/package.json | 26 ++++++++------- package-lock.json | 6 ++-- package.json | 2 +- 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/node_modules/chownr/chownr.js b/node_modules/chownr/chownr.js index 7e63928827e2c..b056912ee1583 100644 --- a/node_modules/chownr/chownr.js +++ b/node_modules/chownr/chownr.js @@ -7,6 +7,36 @@ const LCHOWN = fs.lchown ? 'lchown' : 'chown' /* istanbul ignore next */ const LCHOWNSYNC = fs.lchownSync ? 'lchownSync' : 'chownSync' +const needEISDIRHandled = fs.lchown && + !process.version.match(/v1[1-9]+\./) && + !process.version.match(/v10\.[6-9]/) + +/* istanbul ignore next */ +const handleEISDIR = + needEISDIRHandled ? (path, uid, gid, cb) => er => { + // Node prior to v10 had a very questionable implementation of + // fs.lchown, which would always try to call fs.open on a directory + // Fall back to fs.chown in those cases. + if (!er || er.code !== 'EISDIR') + cb(er) + else + fs.chown(path, uid, gid, cb) + } + : (_, __, ___, cb) => cb + +/* istanbul ignore next */ +const handleEISDirSync = + needEISDIRHandled ? (path, uid, gid) => { + try { + return fs[LCHOWNSYNC](path, uid, gid) + } catch (er) { + if (er.code !== 'EISDIR') + throw er + fs.chownSync(path, uid, gid) + } + } + : fs[LCHOWNSYNC] + // fs.readdir could only accept an options object as of node v6 const nodeVersion = process.version let readdir = (path, options, cb) => fs.readdir(path, options, cb) @@ -28,10 +58,13 @@ const chownrKid = (p, child, uid, gid, cb) => { chownr(path.resolve(p, child.name), uid, gid, er => { if (er) return cb(er) - fs[LCHOWN](path.resolve(p, child.name), uid, gid, cb) + const cpath = path.resolve(p, child.name) + fs[LCHOWN](cpath, uid, gid, handleEISDIR(cpath, uid, gid, cb)) }) - } else - fs[LCHOWN](path.resolve(p, child.name), uid, gid, cb) + } else { + const cpath = path.resolve(p, child.name) + fs[LCHOWN](cpath, uid, gid, handleEISDIR(cpath, uid, gid, cb)) + } } @@ -41,14 +74,18 @@ const chownr = (p, uid, gid, cb) => { // or doesn't exist. give up. if (er && er.code !== 'ENOTDIR' && er.code !== 'ENOTSUP') return cb(er) - if (er || !children.length) return fs[LCHOWN](p, uid, gid, cb) + if (er || !children.length) + return fs[LCHOWN](p, uid, gid, handleEISDIR(p, uid, gid, cb)) let len = children.length let errState = null const then = er => { - if (errState) return - if (er) return cb(errState = er) - if (-- len === 0) return fs[LCHOWN](p, uid, gid, cb) + if (errState) + return + if (er) + return cb(errState = er) + if (-- len === 0) + return fs[LCHOWN](p, uid, gid, handleEISDIR(p, uid, gid, cb)) } children.forEach(child => chownrKid(p, child, uid, gid, then)) @@ -65,7 +102,7 @@ const chownrKidSync = (p, child, uid, gid) => { if (child.isDirectory()) chownrSync(path.resolve(p, child.name), uid, gid) - fs[LCHOWNSYNC](path.resolve(p, child.name), uid, gid) + handleEISDirSync(path.resolve(p, child.name), uid, gid) } const chownrSync = (p, uid, gid) => { @@ -74,14 +111,14 @@ const chownrSync = (p, uid, gid) => { children = readdirSync(p, { withFileTypes: true }) } catch (er) { if (er && er.code === 'ENOTDIR' && er.code !== 'ENOTSUP') - return fs[LCHOWNSYNC](p, uid, gid) + return handleEISDirSync(p, uid, gid) throw er } if (children.length) children.forEach(child => chownrKidSync(p, child, uid, gid)) - return fs[LCHOWNSYNC](p, uid, gid) + return handleEISDirSync(p, uid, gid) } module.exports = chownr diff --git a/node_modules/chownr/package.json b/node_modules/chownr/package.json index 0004fa0e1eb00..f7b9c81c84ca3 100644 --- a/node_modules/chownr/package.json +++ b/node_modules/chownr/package.json @@ -1,28 +1,30 @@ { - "_from": "chownr@1.1.1", - "_id": "chownr@1.1.1", + "_from": "chownr@1.1.2", + "_id": "chownr@1.1.2", "_inBundle": false, - "_integrity": "sha512-j38EvO5+LHX84jlo6h4UzmOwi0UgW61WRyPtJz4qaadK5eY3BTS5TY/S1Stc3Uk2lIM6TPevAlULiEJwie860g==", + "_integrity": "sha512-GkfeAQh+QNy3wquu9oIZr6SS5x7wGdSgNQvD10X3r+AZr1Oys22HW8kAmDMvNg2+Dm0TeGaEuO8gFwdBXxwO8A==", "_location": "/chownr", "_phantomChildren": {}, "_requested": { "type": "version", "registry": true, - "raw": "chownr@1.1.1", + "raw": "chownr@1.1.2", "name": "chownr", "escapedName": "chownr", - "rawSpec": "1.1.1", + "rawSpec": "1.1.2", "saveSpec": null, - "fetchSpec": "1.1.1" + "fetchSpec": "1.1.2" }, "_requiredBy": [ "#USER", - "/" + "/", + "/cacache", + "/tar" ], - "_resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.1.tgz", - "_shasum": "54726b8b8fff4df053c42187e801fb4412df1494", - "_spec": "chownr@1.1.1", - "_where": "/Users/aeschright/code/cli", + "_resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.2.tgz", + "_shasum": "a18f1e0b269c8a6a5d3c86eb298beb14c3dd7bf6", + "_spec": "chownr@1.1.2", + "_where": "/Users/isaacs/dev/npm/cli", "author": { "name": "Isaac Z. Schlueter", "email": "i@izs.me", @@ -56,5 +58,5 @@ "preversion": "npm test", "test": "tap test/*.js --cov" }, - "version": "1.1.1" + "version": "1.1.2" } diff --git a/package-lock.json b/package-lock.json index fe0528a820d43..710e141acac56 100644 --- a/package-lock.json +++ b/package-lock.json @@ -675,9 +675,9 @@ "dev": true }, "chownr": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.1.tgz", - "integrity": "sha512-j38EvO5+LHX84jlo6h4UzmOwi0UgW61WRyPtJz4qaadK5eY3BTS5TY/S1Stc3Uk2lIM6TPevAlULiEJwie860g==" + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.2.tgz", + "integrity": "sha512-GkfeAQh+QNy3wquu9oIZr6SS5x7wGdSgNQvD10X3r+AZr1Oys22HW8kAmDMvNg2+Dm0TeGaEuO8gFwdBXxwO8A==" }, "ci-info": { "version": "2.0.0", diff --git a/package.json b/package.json index b042dd6ca1947..580c1bc8ad488 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "byte-size": "^5.0.1", "cacache": "^11.3.3", "call-limit": "^1.1.1", - "chownr": "^1.1.1", + "chownr": "^1.1.2", "ci-info": "^2.0.0", "cli-columns": "^3.1.2", "cli-table3": "^0.5.1",