Skip to content
Browse files

code

  • Loading branch information...
1 parent bfc90da commit a3e283e5ad366b31fedd4b570cf5b67ad6308c47 @isaacs committed Mar 28, 2012
Showing with 41 additions and 0 deletions.
  1. +41 −0 chownr.js
View
41 chownr.js
@@ -0,0 +1,41 @@
+module.exports = chownr
+chownr.sync = chownrSync
+
+var fs = require("fs")
+, path = require("path")
+
+function chownr (p, uid, gid, 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.chown(p, uid, gid, cb)
+
+ var len = children.length
+ , errState = null
+ children.forEach(function (child) {
+ chownr(path.resolve(p, child), uid, gid, then)
+ })
+ function then (er) {
+ if (errState) return
+ if (er) return cb(errState = er)
+ if (-- len === 0) return fs.chown(p, uid, gid, cb)
+ }
+ })
+}
+
+function chownrSync (p, uid, gid) {
+ var children
+ try {
+ children = fs.readdirSync(p)
+ } catch (er) {
+ if (er && er.code === "ENOTDIR") return fs.chownSync(p, uid, gid)
+ throw er
+ }
+ if (!children.length) return fs.chownSync(p, uid, gid)
+
+ children.forEach(function (child) {
+ chownrSync(path.resolve(p, child), uid, gid)
+ })
+ return fs.chownSync(p, uid, gid)
+}

0 comments on commit a3e283e

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