chownr.js
1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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) {
var pathChild = path.resolve(p, child);
fs.lstat(pathChild, function(er, stats) {
if (er)
return cb(er)
if (!stats.isSymbolicLink())
chownr(pathChild, uid, gid, then)
else
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) {
var pathChild = path.resolve(p, child)
var stats = fs.lstatSync(pathChild)
if (!stats.isSymbolicLink())
chownrSync(pathChild, uid, gid)
})
return fs.chownSync(p, uid, gid)
}