write-dir.js 927 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. var fs = require('graceful-fs');
  3. var mkdirp = require('fs-mkdirp-stream/mkdirp');
  4. var fo = require('../../file-operations');
  5. function writeDir(file, optResolver, onWritten) {
  6. mkdirp(file.path, file.stat.mode, onMkdirp);
  7. function onMkdirp(mkdirpErr) {
  8. if (mkdirpErr) {
  9. return onWritten(mkdirpErr);
  10. }
  11. fs.open(file.path, 'r', onOpen);
  12. }
  13. function onOpen(openErr, fd) {
  14. // If we don't have access, just move along
  15. if (isInaccessible(openErr)) {
  16. return fo.closeFd(null, fd, onWritten);
  17. }
  18. if (openErr) {
  19. return fo.closeFd(openErr, fd, onWritten);
  20. }
  21. fo.updateMetadata(fd, file, onUpdate);
  22. function onUpdate(updateErr) {
  23. fo.closeFd(updateErr, fd, onWritten);
  24. }
  25. }
  26. }
  27. function isInaccessible(err) {
  28. if (!err) {
  29. return false;
  30. }
  31. if (err.code === 'EACCES') {
  32. return true;
  33. }
  34. return false;
  35. }
  36. module.exports = writeDir;