Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var fs = require('fs');
  2. var ncp = require('ncp').ncp;
  3. var path = require('path');
  4. var rimraf = require('rimraf');
  5. var mkdirp = require('mkdirp');
  6. module.exports = mv;
  7. function mv(source, dest, options, cb){
  8. if (typeof options === 'function') {
  9. cb = options;
  10. options = {};
  11. }
  12. var shouldMkdirp = !!options.mkdirp;
  13. var clobber = options.clobber !== false;
  14. var limit = options.limit || 16;
  15. if (shouldMkdirp) {
  16. mkdirs();
  17. } else {
  18. doRename();
  19. }
  20. function mkdirs() {
  21. mkdirp(path.dirname(dest), function(err) {
  22. if (err) return cb(err);
  23. doRename();
  24. });
  25. }
  26. function doRename() {
  27. if (clobber) {
  28. fs.rename(source, dest, function(err) {
  29. if (!err) return cb();
  30. if (err.code !== 'EXDEV') return cb(err);
  31. moveFileAcrossDevice(source, dest, clobber, limit, cb);
  32. });
  33. } else {
  34. fs.link(source, dest, function(err) {
  35. if (err) {
  36. if (err.code === 'EXDEV') {
  37. moveFileAcrossDevice(source, dest, clobber, limit, cb);
  38. return;
  39. }
  40. if (err.code === 'EISDIR' || err.code === 'EPERM') {
  41. moveDirAcrossDevice(source, dest, clobber, limit, cb);
  42. return;
  43. }
  44. cb(err);
  45. return;
  46. }
  47. fs.unlink(source, cb);
  48. });
  49. }
  50. }
  51. }
  52. function moveFileAcrossDevice(source, dest, clobber, limit, cb) {
  53. var outFlags = clobber ? 'w' : 'wx';
  54. var ins = fs.createReadStream(source);
  55. var outs = fs.createWriteStream(dest, {flags: outFlags});
  56. ins.on('error', function(err){
  57. ins.destroy();
  58. outs.destroy();
  59. outs.removeListener('close', onClose);
  60. if (err.code === 'EISDIR' || err.code === 'EPERM') {
  61. moveDirAcrossDevice(source, dest, clobber, limit, cb);
  62. } else {
  63. cb(err);
  64. }
  65. });
  66. outs.on('error', function(err){
  67. ins.destroy();
  68. outs.destroy();
  69. outs.removeListener('close', onClose);
  70. cb(err);
  71. });
  72. outs.once('close', onClose);
  73. ins.pipe(outs);
  74. function onClose(){
  75. fs.unlink(source, cb);
  76. }
  77. }
  78. function moveDirAcrossDevice(source, dest, clobber, limit, cb) {
  79. var options = {
  80. stopOnErr: true,
  81. clobber: false,
  82. limit: limit,
  83. };
  84. if (clobber) {
  85. rimraf(dest, { disableGlob: true }, function(err) {
  86. if (err) return cb(err);
  87. startNcp();
  88. });
  89. } else {
  90. startNcp();
  91. }
  92. function startNcp() {
  93. ncp(source, dest, options, function(errList) {
  94. if (errList) return cb(errList[0]);
  95. rimraf(source, { disableGlob: true }, cb);
  96. });
  97. }
  98. }