Welcome to mirror list, hosted at ThFree Co, Russian Federation.

check-permissions.js « install « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f9e9c8f0e8f94bb45ff807b64ae9150c5549d89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict'
var path = require('path')
var fs = require('graceful-fs')
var log = require('npmlog')
var validate = require('aproba')
var uniq = require('lodash.uniq')
var asyncMap = require('slide').asyncMap
var npm = require('../npm.js')

module.exports = function (actions, next) {
  validate('AF', arguments)
  var errors = []
  asyncMap(actions, function (action, done) {
    var cmd = action[0]
    var pkg = action[1]
    switch (cmd) {
      case 'add':
        hasAnyWriteAccess(path.resolve(pkg.path, '..'), errors, done)
        break
      case 'update':
      case 'remove':
        hasWriteAccess(pkg.path, errors, andHasWriteAccess(path.resolve(pkg.path, '..'), errors, done))
        break
      case 'move':
        hasAnyWriteAccess(pkg.path, errors, andHasWriteAccess(path.resolve(pkg.fromPath, '..'), errors, done))
        break
      default:
        done()
    }
  }, function () {
    if (!errors.length) return next()
    uniq(errors.map(function (er) { return 'Missing write access to ' + er.path })).forEach(function (er) {
      log.warn('checkPermissions', er)
    })
    npm.config.get('force') ? next() : next(errors[0])
  })
}

function andHasWriteAccess (dir, errors, done) {
  validate('SAF', arguments)
  return function () {
    hasWriteAccess(dir, errors, done)
  }
}

function hasAnyWriteAccess (dir, errors, done) {
  validate('SAF', arguments)
  findNearestDir()
  function findNearestDir () {
    var nextDir = path.resolve(dir, '..')
    exists(dir, function (dirDoesntExist) {
      if (!dirDoesntExist || nextDir === dir) {
        return hasWriteAccess(dir, errors, done)
      } else {
        dir = nextDir
        findNearestDir()
      }
    })
  }
}

function hasWriteAccess (dir, errors, done) {
  validate('SAF', arguments)
  access(dir, function (er) {
    if (er) errors.push(er)
    done()
  })
}

var exists = fs.access
  ? function (dir, done) { fs.access(dir, fs.F_OK, done) }
  : function (dir, done) { fs.stat(dir, function (er) { done(accessError(dir, er)) }) }

var access = fs.access
  ? function (dir, done) { fs.access(dir, fs.W_OK, done) }
  : function (dir, done) {
      // FIXME: So it turns out that for larger installations on windows
      // this sometimes fails.  We need to make this work better.  Options
      // include:
      //   caching results
      //   inflighting results
      //   limiting concurrency
      // Maybe all three.
      // That this is going through asyncMap and then running ALL THIS
      // makes this failure completely unsurprising. 😕
      if (process.platform === 'win32') return done()
      var tmp = path.join(dir, '.npm.check.permissions')
      fs.open(tmp, 'w', function (er, fd) {
        if (er) return done(accessError(dir, er))
        fs.close(fd, function () {
          fs.unlink(tmp, function () { done() })
        })
      })
    }

function accessError (dir, er) {
  if (!er) return
  var accessEr = new Error("EACCES, access '" + dir + "'", -13)
  accessEr.code = 'EACCES'
  accessEr.path = dir
  return accessEr
}