• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var path = require('path')
3var fs = require('fs')
4var inflight = require('inflight')
5var accessError = require('./access-error.js')
6var andIgnoreErrors = require('./and-ignore-errors.js')
7var isFsAccessAvailable = require('./is-fs-access-available.js')
8
9if (isFsAccessAvailable) {
10  module.exports = fsAccessImplementation
11} else {
12  module.exports = fsOpenImplementation
13}
14
15// exposed only for testing purposes
16module.exports.fsAccessImplementation = fsAccessImplementation
17module.exports.fsOpenImplementation = fsOpenImplementation
18
19function fsAccessImplementation (dir, done) {
20  done = inflight('writable:' + dir, done)
21  if (!done) return
22  fs.access(dir, fs.W_OK, done)
23}
24
25function fsOpenImplementation (dir, done) {
26  done = inflight('writable:' + dir, done)
27  if (!done) return
28  var tmp = path.join(dir, '.npm.check.permissions')
29  fs.open(tmp, 'w', function (er, fd) {
30    if (er) return done(accessError(dir, er))
31    fs.close(fd, function () {
32      fs.unlink(tmp, andIgnoreErrors(done))
33    })
34  })
35}
36