a zip code crypto-currency system good for red ONLY

index.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use strict'
  2. // most of this code was written by Andrew Kelley
  3. // licensed under the BSD license: see
  4. // https://github.com/andrewrk/node-mv/blob/master/package.json
  5. // this needs a cleanup
  6. const u = require('universalify').fromCallback
  7. const fs = require('graceful-fs')
  8. const ncp = require('../copy/ncp')
  9. const path = require('path')
  10. const remove = require('../remove').remove
  11. const mkdirp = require('../mkdirs').mkdirs
  12. function move (src, dest, options, callback) {
  13. if (typeof options === 'function') {
  14. callback = options
  15. options = {}
  16. }
  17. const overwrite = options.overwrite || options.clobber || false
  18. isSrcSubdir(src, dest, (err, itIs) => {
  19. if (err) return callback(err)
  20. if (itIs) return callback(new Error(`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`))
  21. mkdirp(path.dirname(dest), err => {
  22. if (err) return callback(err)
  23. doRename()
  24. })
  25. })
  26. function doRename () {
  27. if (path.resolve(src) === path.resolve(dest)) {
  28. fs.access(src, callback)
  29. } else if (overwrite) {
  30. fs.rename(src, dest, err => {
  31. if (!err) return callback()
  32. if (err.code === 'ENOTEMPTY' || err.code === 'EEXIST') {
  33. remove(dest, err => {
  34. if (err) return callback(err)
  35. options.overwrite = false // just overwriteed it, no need to do it again
  36. move(src, dest, options, callback)
  37. })
  38. return
  39. }
  40. // weird Windows shit
  41. if (err.code === 'EPERM') {
  42. setTimeout(() => {
  43. remove(dest, err => {
  44. if (err) return callback(err)
  45. options.overwrite = false
  46. move(src, dest, options, callback)
  47. })
  48. }, 200)
  49. return
  50. }
  51. if (err.code !== 'EXDEV') return callback(err)
  52. moveAcrossDevice(src, dest, overwrite, callback)
  53. })
  54. } else {
  55. fs.link(src, dest, err => {
  56. if (err) {
  57. if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM' || err.code === 'ENOTSUP') {
  58. return moveAcrossDevice(src, dest, overwrite, callback)
  59. }
  60. return callback(err)
  61. }
  62. return fs.unlink(src, callback)
  63. })
  64. }
  65. }
  66. }
  67. function moveAcrossDevice (src, dest, overwrite, callback) {
  68. fs.stat(src, (err, stat) => {
  69. if (err) return callback(err)
  70. if (stat.isDirectory()) {
  71. moveDirAcrossDevice(src, dest, overwrite, callback)
  72. } else {
  73. moveFileAcrossDevice(src, dest, overwrite, callback)
  74. }
  75. })
  76. }
  77. function moveFileAcrossDevice (src, dest, overwrite, callback) {
  78. const flags = overwrite ? 'w' : 'wx'
  79. const ins = fs.createReadStream(src)
  80. const outs = fs.createWriteStream(dest, { flags })
  81. ins.on('error', err => {
  82. ins.destroy()
  83. outs.destroy()
  84. outs.removeListener('close', onClose)
  85. // may want to create a directory but `out` line above
  86. // creates an empty file for us: See #108
  87. // don't care about error here
  88. fs.unlink(dest, () => {
  89. // note: `err` here is from the input stream errror
  90. if (err.code === 'EISDIR' || err.code === 'EPERM') {
  91. moveDirAcrossDevice(src, dest, overwrite, callback)
  92. } else {
  93. callback(err)
  94. }
  95. })
  96. })
  97. outs.on('error', err => {
  98. ins.destroy()
  99. outs.destroy()
  100. outs.removeListener('close', onClose)
  101. callback(err)
  102. })
  103. outs.once('close', onClose)
  104. ins.pipe(outs)
  105. function onClose () {
  106. fs.unlink(src, callback)
  107. }
  108. }
  109. function moveDirAcrossDevice (src, dest, overwrite, callback) {
  110. const options = {
  111. overwrite: false
  112. }
  113. if (overwrite) {
  114. remove(dest, err => {
  115. if (err) return callback(err)
  116. startNcp()
  117. })
  118. } else {
  119. startNcp()
  120. }
  121. function startNcp () {
  122. ncp(src, dest, options, err => {
  123. if (err) return callback(err)
  124. remove(src, callback)
  125. })
  126. }
  127. }
  128. // return true if dest is a subdir of src, otherwise false.
  129. // extract dest base dir and check if that is the same as src basename
  130. function isSrcSubdir (src, dest, cb) {
  131. fs.stat(src, (err, st) => {
  132. if (err) return cb(err)
  133. if (st.isDirectory()) {
  134. const baseDir = dest.split(path.dirname(src) + path.sep)[1]
  135. if (baseDir) {
  136. const destBasename = baseDir.split(path.sep)[0]
  137. if (destBasename) return cb(null, src !== dest && dest.indexOf(src) > -1 && destBasename === path.basename(src))
  138. return cb(null, false)
  139. }
  140. return cb(null, false)
  141. }
  142. return cb(null, false)
  143. })
  144. }
  145. module.exports = {
  146. move: u(move)
  147. }