writable.js 1.0 KB

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