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