check-files-permission.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var fs = require('fs')
  2. var path = require('path')
  3. var getUid = require('uid-number')
  4. var chain = require('slide').chain
  5. var log = require('npmlog')
  6. var npm = require('../npm.js')
  7. var fileCompletion = require('../utils/completion/file-completion.js')
  8. function checkFilesPermission (root, fmask, dmask, cb) {
  9. if (process.platform === 'win32') return cb(null, true)
  10. getUid(npm.config.get('user'), npm.config.get('group'), function (e, uid, gid) {
  11. var tracker = log.newItem('checkFilePermissions', 1)
  12. if (e) {
  13. tracker.finish()
  14. tracker.warn('checkFilePermissions', 'Error looking up user and group:', e)
  15. return cb(e)
  16. }
  17. tracker.info('checkFilePermissions', 'Building file list of ' + root)
  18. fileCompletion(root, '.', Infinity, function (e, files) {
  19. if (e) {
  20. tracker.warn('checkFilePermissions', 'Error building file list:', e)
  21. tracker.finish()
  22. return cb(e)
  23. }
  24. tracker.addWork(files.length)
  25. tracker.completeWork(1)
  26. chain(files.map(andCheckFile), function (er) {
  27. tracker.finish()
  28. cb(null, !er)
  29. })
  30. function andCheckFile (f) {
  31. return [checkFile, f]
  32. }
  33. function checkFile (f, next) {
  34. var file = path.join(root, f)
  35. tracker.silly('checkFilePermissions', f)
  36. fs.lstat(file, function (e, stat) {
  37. tracker.completeWork(1)
  38. if (e) return next(e)
  39. if (!stat.isDirectory() && !stat.isFile()) return next()
  40. // 6 = fs.constants.R_OK | fs.constants.W_OK
  41. // constants aren't available on v4
  42. fs.access(file, stat.isFile() ? fmask : dmask, (err) => {
  43. if (err) {
  44. tracker.error('checkFilePermissions', `Missing permissions on ${file}`)
  45. return next(new Error('Missing permissions for ' + file))
  46. } else {
  47. return next()
  48. }
  49. })
  50. })
  51. }
  52. })
  53. })
  54. }
  55. module.exports = checkFilesPermission