Skip to content
Browse files

Never make directories non-listable

At least, not by parties who ought to be able to read them.
  • Loading branch information...
1 parent f2a7c22 commit 2e445ff567b98c2583e6bea00e190fc6bbfcaa66 @isaacs committed Mar 6, 2013
Showing with 19 additions and 6 deletions.
  1. +19 −6 chmodr.js
View
25 chmodr.js
@@ -8,18 +8,22 @@ function chmodr (p, mode, cb) {
fs.readdir(p, function (er, children) {
// 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.chmod(p, mode, cb)
+ if (er && er.code !== "ENOTDIR")
+ return cb(er)
+ var isDir = !er
+ var m = isDir ? dirMode(mode) : mode
+ if (er || !children.length)
+ return fs.chmod(p, m, cb)
var len = children.length
- , errState = null
+ var errState = null
children.forEach(function (child) {
chmodr(path.resolve(p, child), mode, then)
})
function then (er) {
if (errState) return
if (er) return cb(errState = er)
- if (-- len === 0) return fs.chmod(p, mode, cb)
+ if (-- len === 0) return fs.chmod(p, dirMode(mode), cb)
}
})
}
@@ -32,10 +36,19 @@ function chmodrSync (p, mode) {
if (er && er.code === "ENOTDIR") return fs.chmodSync(p, mode)
throw er
}
- if (!children.length) return fs.chmodSync(p, mode)
+ if (!children.length) return fs.chmodSync(p, dirMode(mode))
children.forEach(function (child) {
chmodrSync(path.resolve(p, child), mode)
})
- return fs.chmodSync(p, mode)
+ return fs.chmodSync(p, dirMode(mode))
+}
+
+// If a party has r, add x
+// so that dirs are listable
+function dirMode(mode) {
+ if (mode & 0400) mode |= 0100
+ if (mode & 040) mode |= 010
+ if (mode & 04) mode |= 01
+ return mode
}

0 comments on commit 2e445ff

Please sign in to comment.
Something went wrong with that request. Please try again.