Skip to content

Commit

Permalink
Use fs.chmod[Sync] when target is not a symlink
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Bargiel committed Oct 18, 2018
1 parent 2ded52b commit 0717c79
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions chmodr.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const dirMode = mode => {
return mode
}


const chmodrKid = (p, child, mode, cb) => {
if (typeof child === 'string')
return fs.lstat(path.resolve(p, child), (er, stats) => {
Expand All @@ -43,7 +44,7 @@ const chmodrKid = (p, child, mode, cb) => {
fs.chmod(path.resolve(p, child.name), dirMode(mode), cb)
})
} else
fs[LCHMOD](path.resolve(p, child.name), mode, cb)
fs[child.isSymbolicLink() ? LCHMOD : 'chmod'](path.resolve(p, child.name), mode, cb)
}


Expand All @@ -52,7 +53,7 @@ const chmodr = (p, mode, 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) return fs[LCHMOD](p, mode, cb)
if (er) return chmodrKid(p, '.', mode, cb)
if (!children.length) return fs.chmod(p, dirMode(mode), cb)

let len = children.length
Expand All @@ -78,15 +79,15 @@ const chmodrKidSync = (p, child, mode) => {
chmodrSync(path.resolve(p, child.name), mode)
fs.chmodSync(path.resolve(p, child.name), dirMode(mode))
} else
fs[LCHMODSYNC](path.resolve(p, child.name), mode)
fs[child.isSymbolicLink() ? LCHMODSYNC : 'chmodSync'](path.resolve(p, child.name), mode)
}

const chmodrSync = (p, mode) => {
let children
try {
children = readdirSync(p, { withFileTypes: true })
} catch (er) {
if (er && er.code === 'ENOTDIR') return fs[LCHMODSYNC](p, mode)
if (er && er.code === 'ENOTDIR') return chmodrKidSync(p, '.', mode)
throw er
}

Expand Down

0 comments on commit 0717c79

Please sign in to comment.