This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: infer ownership of created dirs and links
- Loading branch information
Showing
5 changed files
with
1,002 additions
and
929 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.