Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
feat: infer ownership of created dirs and links
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 14, 2019
1 parent a9840e8 commit 0dd2879
Show file tree
Hide file tree
Showing 5 changed files with 1,002 additions and 929 deletions.
24 changes: 24 additions & 0 deletions lib/chown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

// A module for chowning things we just created, to preserve
// ownership of new links and directories.

const chownr = require('chownr')

const selfOwner = {
uid: process.getuid && process.getuid(),
gid: process.getgid && process.getgid()
}

module.exports = (path, uid, gid, cb) => {
if (selfOwner.uid !== 0 ||
uid === undefined || gid === undefined ||
(selfOwner.uid === uid && selfOwner.gid === gid)) {
// don't need to, or can't chown anyway, so just leave it.
// this also handles platforms where process.getuid is undefined
return cb()
}
chownr(path, uid, gid, cb)
}

module.exports.selfOwner = selfOwner
30 changes: 19 additions & 11 deletions lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const path = require('path')
const fs = require('graceful-fs')
const chain = require('slide').chain
const mkdir = require('mkdirp')
const mkdir = require('./mkdir.js')
const rm = require('./rm.js')
const inferOwner = require('infer-owner')
const chown = require('./chown.js')

exports = module.exports = {
link: link,
Expand Down Expand Up @@ -53,14 +55,20 @@ function link (from, to, opts, cb) {
var relativeTarget = path.relative(opts.base, absTarget)
var target = opts.absolute ? absTarget : relativeTarget

chain(
[
[ensureFromIsNotSource, absTarget, to],
[fs, 'stat', absTarget],
[rm, to, opts],
[mkdir, path.dirname(to)],
[fs, 'symlink', target, to, 'junction']
],
cb
)
const tasks = [
[ensureFromIsNotSource, absTarget, to],
[fs, 'stat', absTarget],
[rm, to, opts],
[mkdir, path.dirname(to)],
[fs, 'symlink', target, to, 'junction']
]

if (chown.selfOwner !== 0) {
chain(tasks, cb)
} else {
inferOwner.then(owner => {
tasks.push([chown, to, owner.uid, owner.gid])
chain(tasks, cb)
})
}
}
22 changes: 22 additions & 0 deletions lib/mkdir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const mkdirp = require('mkdirp')
const inferOwner = require('infer-owner')
const chown = require('./chown.js')

module.exports = (path, cb) => {
// don't bother chowning if we can't anyway
if (process.platform === 'win32' || chown.selfOwner.uid !== 0) {
return mkdirp(path, cb)
}

inferOwner(path).then(owner => {
mkdirp(path, (er, made) => {
if (er) {
cb(er, made)
} else {
chown(made || path, owner.uid, owner.gid, cb)
}
})
}, cb)
}
Loading

0 comments on commit 0dd2879

Please sign in to comment.