Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent improper clobbering of man/bin links #12

Merged
merged 2 commits into from Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions .travis.yml
@@ -1,7 +1,8 @@
language: node_js
sudo: false

node_js:
- "9"
- "8"
- "6"
- "4"
- node
- 12
- 10
- 8
- 6
30 changes: 21 additions & 9 deletions index.js
Expand Up @@ -3,8 +3,9 @@
const path = require('path')
const fs = require('graceful-fs')
const BB = require('bluebird')
const linkIfExists = BB.promisify(require('gentle-fs').linkIfExists)
const cmdShimIfExists = BB.promisify(require('cmd-shim').ifExists)
const gentleFs = require('gentle-fs')
const linkIfExists = BB.promisify(gentleFs.linkIfExists)
const gentleFsBinLink = BB.promisify(gentleFs.binLink)
const open = BB.promisify(fs.open)
const close = BB.promisify(fs.close)
const read = BB.promisify(fs.read, {multiArgs: true})
Expand Down Expand Up @@ -42,9 +43,9 @@ function isHashbangFile (file) {
return read(fileHandle, Buffer.alloc(2), 0, 2, 0).spread((_, buf) => {
if (!hasHashbang(buf)) return []
return read(fileHandle, Buffer.alloc(2048), 0, 2048, 0)
}).spread((_, buf) => buf && hasCR(buf), () => false)
}).spread((_, buf) => buf && hasCR(buf), /* istanbul ignore next */ () => false)
.finally(() => close(fileHandle))
}).catch(() => false)
}).catch(/* istanbul ignore next */ () => false)
}

function hasHashbang (buf) {
Expand Down Expand Up @@ -109,18 +110,19 @@ function linkBins (pkg, folder, parent, gtop, opts) {
opts.log.showProgress()
}
}).catch(err => {
/* istanbul ignore next */
if (err.code === 'ENOENT' && opts.ignoreScripts) return
throw err
})
})
}

function linkBin (from, to, opts) {
if (process.platform !== 'win32') {
return linkIfExists(from, to, opts)
} else {
return cmdShimIfExists(from, to)
// do not clobber global bins
if (opts.globalBin && to.indexOf(opts.globalBin) === 0) {
opts.clobberLinkGently = true
}
return gentleFsBinLink(from, to, opts)
}

function linkMans (pkg, folder, parent, gtop, opts) {
Expand All @@ -132,17 +134,22 @@ function linkMans (pkg, folder, parent, gtop, opts) {
// make sure that the mans are unique.
// otherwise, if there are dupes, it'll fail with EEXIST
var set = pkg.man.reduce(function (acc, man) {
if (typeof man !== 'string') {
return acc
}
const cleanMan = path.join('/', man).replace(/\\|:/g, '/').substr(1)
acc[path.basename(man)] = cleanMan
return acc
}, {})
var manpages = pkg.man.filter(function (man) {
if (typeof man !== 'string') {
return false
}
const cleanMan = path.join('/', man).replace(/\\|:/g, '/').substr(1)
return set[path.basename(man)] === cleanMan
})

return BB.map(manpages, man => {
if (typeof man !== 'string') return
opts.log.silly('linkMans', 'preparing to link', man)
var parseMan = man.match(/(.*\.([0-9]+)(\.gz)?)$/)
if (!parseMan) {
Expand All @@ -165,6 +172,11 @@ function linkMans (pkg, folder, parent, gtop, opts) {

var manDest = path.join(manRoot, 'man' + sxn, bn)

// man pages should always be clobbering gently, because they are
// only installed for top-level global packages, so never destroy
// a link if it doesn't point into the folder we're linking
opts.clobberLinkGently = true

return linkIfExists(manSrc, manDest, getLinkOpts(opts, gtop && folder))
})
}
36 changes: 30 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -6,9 +6,9 @@
"scripts": {
"prerelease": "npm t",
"postrelease": "npm publish && git push --follow-tags",
"pretest": "standard",
"posttest": "standard",
"release": "standard-version -s",
"test": "tap -J --nyc-arg=--all --coverage test/*.js",
"test": "tap -J --nyc-arg=--all --coverage test/*.js --100",
"update-coc": "weallbehave -o . && git add CODE_OF_CONDUCT.md && git commit -m 'docs(coc): updated CODE_OF_CONDUCT.md'",
"update-contrib": "weallcontribute -o . && git add CONTRIBUTING.md && git commit -m 'docs(contributing): updated CONTRIBUTING.md'"
},
Expand All @@ -30,7 +30,7 @@
"dependencies": {
"bluebird": "^3.5.3",
"cmd-shim": "^3.0.0",
"gentle-fs": "^2.0.1",
"gentle-fs": "^2.3.0",
"graceful-fs": "^4.1.15",
"npm-normalize-package-bin": "^1.0.0",
"write-file-atomic": "^2.3.0"
Expand Down