Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

copy.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. 'use strict'
  2. const fs = require('graceful-fs')
  3. const path = require('path')
  4. const mkdirp = require('../mkdirs').mkdirs
  5. const pathExists = require('../path-exists').pathExists
  6. const utimes = require('../util/utimes').utimesMillis
  7. const stat = require('../util/stat')
  8. function copy (src, dest, opts, cb) {
  9. if (typeof opts === 'function' && !cb) {
  10. cb = opts
  11. opts = {}
  12. } else if (typeof opts === 'function') {
  13. opts = { filter: opts }
  14. }
  15. cb = cb || function () {}
  16. opts = opts || {}
  17. opts.clobber = 'clobber' in opts ? !!opts.clobber : true // default to true for now
  18. opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber // overwrite falls back to clobber
  19. // Warn about using preserveTimestamps on 32-bit node
  20. if (opts.preserveTimestamps && process.arch === 'ia32') {
  21. console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n
  22. see https://github.com/jprichardson/node-fs-extra/issues/269`)
  23. }
  24. stat.checkPaths(src, dest, 'copy', (err, stats) => {
  25. if (err) return cb(err)
  26. const { srcStat, destStat } = stats
  27. stat.checkParentPaths(src, srcStat, dest, 'copy', err => {
  28. if (err) return cb(err)
  29. if (opts.filter) return handleFilter(checkParentDir, destStat, src, dest, opts, cb)
  30. return checkParentDir(destStat, src, dest, opts, cb)
  31. })
  32. })
  33. }
  34. function checkParentDir (destStat, src, dest, opts, cb) {
  35. const destParent = path.dirname(dest)
  36. pathExists(destParent, (err, dirExists) => {
  37. if (err) return cb(err)
  38. if (dirExists) return startCopy(destStat, src, dest, opts, cb)
  39. mkdirp(destParent, err => {
  40. if (err) return cb(err)
  41. return startCopy(destStat, src, dest, opts, cb)
  42. })
  43. })
  44. }
  45. function handleFilter (onInclude, destStat, src, dest, opts, cb) {
  46. Promise.resolve(opts.filter(src, dest)).then(include => {
  47. if (include) return onInclude(destStat, src, dest, opts, cb)
  48. return cb()
  49. }, error => cb(error))
  50. }
  51. function startCopy (destStat, src, dest, opts, cb) {
  52. if (opts.filter) return handleFilter(getStats, destStat, src, dest, opts, cb)
  53. return getStats(destStat, src, dest, opts, cb)
  54. }
  55. function getStats (destStat, src, dest, opts, cb) {
  56. const stat = opts.dereference ? fs.stat : fs.lstat
  57. stat(src, (err, srcStat) => {
  58. if (err) return cb(err)
  59. if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts, cb)
  60. else if (srcStat.isFile() ||
  61. srcStat.isCharacterDevice() ||
  62. srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts, cb)
  63. else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts, cb)
  64. })
  65. }
  66. function onFile (srcStat, destStat, src, dest, opts, cb) {
  67. if (!destStat) return copyFile(srcStat, src, dest, opts, cb)
  68. return mayCopyFile(srcStat, src, dest, opts, cb)
  69. }
  70. function mayCopyFile (srcStat, src, dest, opts, cb) {
  71. if (opts.overwrite) {
  72. fs.unlink(dest, err => {
  73. if (err) return cb(err)
  74. return copyFile(srcStat, src, dest, opts, cb)
  75. })
  76. } else if (opts.errorOnExist) {
  77. return cb(new Error(`'${dest}' already exists`))
  78. } else return cb()
  79. }
  80. function copyFile (srcStat, src, dest, opts, cb) {
  81. if (typeof fs.copyFile === 'function') {
  82. return fs.copyFile(src, dest, err => {
  83. if (err) return cb(err)
  84. return setDestModeAndTimestamps(srcStat, dest, opts, cb)
  85. })
  86. }
  87. return copyFileFallback(srcStat, src, dest, opts, cb)
  88. }
  89. function copyFileFallback (srcStat, src, dest, opts, cb) {
  90. const rs = fs.createReadStream(src)
  91. rs.on('error', err => cb(err)).once('open', () => {
  92. const ws = fs.createWriteStream(dest, { mode: srcStat.mode })
  93. ws.on('error', err => cb(err))
  94. .on('open', () => rs.pipe(ws))
  95. .once('close', () => setDestModeAndTimestamps(srcStat, dest, opts, cb))
  96. })
  97. }
  98. function setDestModeAndTimestamps (srcStat, dest, opts, cb) {
  99. fs.chmod(dest, srcStat.mode, err => {
  100. if (err) return cb(err)
  101. if (opts.preserveTimestamps) {
  102. return utimes(dest, srcStat.atime, srcStat.mtime, cb)
  103. }
  104. return cb()
  105. })
  106. }
  107. function onDir (srcStat, destStat, src, dest, opts, cb) {
  108. if (!destStat) return mkDirAndCopy(srcStat, src, dest, opts, cb)
  109. if (destStat && !destStat.isDirectory()) {
  110. return cb(new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`))
  111. }
  112. return copyDir(src, dest, opts, cb)
  113. }
  114. function mkDirAndCopy (srcStat, src, dest, opts, cb) {
  115. fs.mkdir(dest, err => {
  116. if (err) return cb(err)
  117. copyDir(src, dest, opts, err => {
  118. if (err) return cb(err)
  119. return fs.chmod(dest, srcStat.mode, cb)
  120. })
  121. })
  122. }
  123. function copyDir (src, dest, opts, cb) {
  124. fs.readdir(src, (err, items) => {
  125. if (err) return cb(err)
  126. return copyDirItems(items, src, dest, opts, cb)
  127. })
  128. }
  129. function copyDirItems (items, src, dest, opts, cb) {
  130. const item = items.pop()
  131. if (!item) return cb()
  132. return copyDirItem(items, item, src, dest, opts, cb)
  133. }
  134. function copyDirItem (items, item, src, dest, opts, cb) {
  135. const srcItem = path.join(src, item)
  136. const destItem = path.join(dest, item)
  137. stat.checkPaths(srcItem, destItem, 'copy', (err, stats) => {
  138. if (err) return cb(err)
  139. const { destStat } = stats
  140. startCopy(destStat, srcItem, destItem, opts, err => {
  141. if (err) return cb(err)
  142. return copyDirItems(items, src, dest, opts, cb)
  143. })
  144. })
  145. }
  146. function onLink (destStat, src, dest, opts, cb) {
  147. fs.readlink(src, (err, resolvedSrc) => {
  148. if (err) return cb(err)
  149. if (opts.dereference) {
  150. resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
  151. }
  152. if (!destStat) {
  153. return fs.symlink(resolvedSrc, dest, cb)
  154. } else {
  155. fs.readlink(dest, (err, resolvedDest) => {
  156. if (err) {
  157. // dest exists and is a regular file or directory,
  158. // Windows may throw UNKNOWN error. If dest already exists,
  159. // fs throws error anyway, so no need to guard against it here.
  160. if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlink(resolvedSrc, dest, cb)
  161. return cb(err)
  162. }
  163. if (opts.dereference) {
  164. resolvedDest = path.resolve(process.cwd(), resolvedDest)
  165. }
  166. if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
  167. return cb(new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`))
  168. }
  169. // do not copy if src is a subdir of dest since unlinking
  170. // dest in this case would result in removing src contents
  171. // and therefore a broken symlink would be created.
  172. if (destStat.isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
  173. return cb(new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`))
  174. }
  175. return copyLink(resolvedSrc, dest, cb)
  176. })
  177. }
  178. })
  179. }
  180. function copyLink (resolvedSrc, dest, cb) {
  181. fs.unlink(dest, err => {
  182. if (err) return cb(err)
  183. return fs.symlink(resolvedSrc, dest, cb)
  184. })
  185. }
  186. module.exports = copy