• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1module.exports = isexe
2isexe.sync = sync
3
4var fs = require('fs')
5
6function isexe (path, options, cb) {
7  fs.stat(path, function (er, stat) {
8    cb(er, er ? false : checkStat(stat, options))
9  })
10}
11
12function sync (path, options) {
13  return checkStat(fs.statSync(path), options)
14}
15
16function checkStat (stat, options) {
17  return stat.isFile() && checkMode(stat, options)
18}
19
20function checkMode (stat, options) {
21  var mod = stat.mode
22  var uid = stat.uid
23  var gid = stat.gid
24
25  var myUid = options.uid !== undefined ?
26    options.uid : process.getuid && process.getuid()
27  var myGid = options.gid !== undefined ?
28    options.gid : process.getgid && process.getgid()
29
30  var u = parseInt('100', 8)
31  var g = parseInt('010', 8)
32  var o = parseInt('001', 8)
33  var ug = u | g
34
35  var ret = (mod & o) ||
36    (mod & g) && gid === myGid ||
37    (mod & u) && uid === myUid ||
38    (mod & ug) && myUid === 0
39
40  return ret
41}
42