Skip to content

Commit

Permalink
use lchown instead of chown, if available
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Sep 15, 2018
1 parent cdd4ce7 commit a631d84
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions chownr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const chownr = (p, uid, gid, cb) => {
// any error other than ENOTDIR means it's not readable, or
// doesn't exist. give up.
if (er && er.code !== 'ENOTDIR') return cb(er)
if (er || !children.length) return fs.chown(p, uid, gid, cb)
if (er || !children.length) return fs.lchown(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.chown(p, uid, gid, cb)
if (-- len === 0) return fs.lchown(p, uid, gid, cb)
}

children.forEach(child => {
Expand All @@ -36,10 +36,10 @@ const chownrSync = (p, uid, gid) => {
try {
children = fs.readdirSync(p)
} catch (er) {
if (er && er.code === 'ENOTDIR') return fs.chownSync(p, uid, gid)
if (er && er.code === 'ENOTDIR') return fs.lchownSync(p, uid, gid)
throw er
}
if (!children.length) return fs.chownSync(p, uid, gid)
if (!children.length) return fs.lchownSync(p, uid, gid)

children.forEach(child => {
const pathChild = path.resolve(p, child)
Expand All @@ -48,7 +48,7 @@ const chownrSync = (p, uid, gid) => {
chownrSync(pathChild, uid, gid)
})

return fs.chownSync(p, uid, gid)
return fs.lchownSync(p, uid, gid)
}

module.exports = chownr
Expand Down

0 comments on commit a631d84

Please # to comment.