]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/chownr/chownr.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / chownr / chownr.js
1 module.exports = chownr
2 chownr.sync = chownrSync
3
4 var fs = require("fs")
5 , path = require("path")
6
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)
13
14     var len = children.length
15     , errState = null
16     children.forEach(function (child) {
17       var pathChild = path.resolve(p, child);
18       fs.lstat(pathChild, function(er, stats) {
19         if (er)
20           return cb(er)
21         if (!stats.isSymbolicLink())
22           chownr(pathChild, uid, gid, then)
23         else
24           then()
25         })
26     })
27     function then (er) {
28       if (errState) return
29       if (er) return cb(errState = er)
30       if (-- len === 0) return fs.chown(p, uid, gid, cb)
31     }
32   })
33 }
34
35 function chownrSync (p, uid, gid) {
36   var children
37   try {
38     children = fs.readdirSync(p)
39   } catch (er) {
40     if (er && er.code === "ENOTDIR") return fs.chownSync(p, uid, gid)
41     throw er
42   }
43   if (!children.length) return fs.chownSync(p, uid, gid)
44
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)
50   })
51   return fs.chownSync(p, uid, gid)
52 }