mode.js 814 B

12345678910111213141516171819202122232425262728293031323334353637
  1. module.exports = isexe
  2. isexe.sync = sync
  3. var fs = require('fs')
  4. function isexe (path, options, cb) {
  5. fs.stat(path, function (er, st) {
  6. cb(er, er ? false : checkMode(st, options))
  7. })
  8. }
  9. function sync (path, options) {
  10. return checkMode(fs.statSync(path), options)
  11. }
  12. function checkMode (stat, options) {
  13. var mod = stat.mode
  14. var uid = stat.uid
  15. var gid = stat.gid
  16. var myUid = options.uid !== undefined ?
  17. options.uid : process.getuid && process.getuid()
  18. var myGid = options.gid !== undefined ?
  19. options.gid : process.getgid && process.getgid()
  20. var u = parseInt('100', 8)
  21. var g = parseInt('010', 8)
  22. var o = parseInt('001', 8)
  23. var ug = u | g
  24. var ret = (mod & o) ||
  25. (mod & g) && gid === myGid ||
  26. (mod & u) && uid === myUid ||
  27. (mod & ug) && myUid === 0
  28. return ret
  29. }