1 module.exports = chownr
2 chownr.sync = chownrSync
5 , path = require("path")
7 function chownr (p, uid, gid, cb) {
8 fs.readdir(p, function (er, children) {
9 // any error other than ENOTDIR means it's not readable, or
10 // doesn't exist. give up.
11 if (er && er.code !== "ENOTDIR") return cb(er)
12 if (er || !children.length) return fs.chown(p, uid, gid, cb)
14 var len = children.length
16 children.forEach(function (child) {
17 var pathChild = path.resolve(p, child);
18 fs.lstat(pathChild, function(er, stats) {
21 if (!stats.isSymbolicLink())
22 chownr(pathChild, uid, gid, then)
29 if (er) return cb(errState = er)
30 if (-- len === 0) return fs.chown(p, uid, gid, cb)
35 function chownrSync (p, uid, gid) {
38 children = fs.readdirSync(p)
40 if (er && er.code === "ENOTDIR") return fs.chownSync(p, uid, gid)
43 if (!children.length) return fs.chownSync(p, uid, gid)
45 children.forEach(function (child) {
46 var pathChild = path.resolve(p, child)
47 var stats = fs.lstatSync(pathChild)
48 if (!stats.isSymbolicLink())
49 chownrSync(pathChild, uid, gid)
51 return fs.chownSync(p, uid, gid)