copy-sync.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict'
  2. const fs = require('graceful-fs')
  3. const path = require('path')
  4. const mkdirsSync = require('../mkdirs').mkdirsSync
  5. const utimesMillisSync = require('../util/utimes').utimesMillisSync
  6. const stat = require('../util/stat')
  7. function copySync (src, dest, opts) {
  8. if (typeof opts === 'function') {
  9. opts = { filter: opts }
  10. }
  11. opts = opts || {}
  12. opts.clobber = 'clobber' in opts ? !!opts.clobber : true // default to true for now
  13. opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber // overwrite falls back to clobber
  14. // Warn about using preserveTimestamps on 32-bit node
  15. if (opts.preserveTimestamps && process.arch === 'ia32') {
  16. process.emitWarning(
  17. 'Using the preserveTimestamps option in 32-bit node is not recommended;\n\n' +
  18. '\tsee https://github.com/jprichardson/node-fs-extra/issues/269',
  19. 'Warning', 'fs-extra-WARN0002'
  20. )
  21. }
  22. const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy', opts)
  23. stat.checkParentPathsSync(src, srcStat, dest, 'copy')
  24. return handleFilterAndCopy(destStat, src, dest, opts)
  25. }
  26. function handleFilterAndCopy (destStat, src, dest, opts) {
  27. if (opts.filter && !opts.filter(src, dest)) return
  28. const destParent = path.dirname(dest)
  29. if (!fs.existsSync(destParent)) mkdirsSync(destParent)
  30. return getStats(destStat, src, dest, opts)
  31. }
  32. function startCopy (destStat, src, dest, opts) {
  33. if (opts.filter && !opts.filter(src, dest)) return
  34. return getStats(destStat, src, dest, opts)
  35. }
  36. function getStats (destStat, src, dest, opts) {
  37. const statSync = opts.dereference ? fs.statSync : fs.lstatSync
  38. const srcStat = statSync(src)
  39. if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts)
  40. else if (srcStat.isFile() ||
  41. srcStat.isCharacterDevice() ||
  42. srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts)
  43. else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts)
  44. else if (srcStat.isSocket()) throw new Error(`Cannot copy a socket file: ${src}`)
  45. else if (srcStat.isFIFO()) throw new Error(`Cannot copy a FIFO pipe: ${src}`)
  46. throw new Error(`Unknown file: ${src}`)
  47. }
  48. function onFile (srcStat, destStat, src, dest, opts) {
  49. if (!destStat) return copyFile(srcStat, src, dest, opts)
  50. return mayCopyFile(srcStat, src, dest, opts)
  51. }
  52. function mayCopyFile (srcStat, src, dest, opts) {
  53. if (opts.overwrite) {
  54. fs.unlinkSync(dest)
  55. return copyFile(srcStat, src, dest, opts)
  56. } else if (opts.errorOnExist) {
  57. throw new Error(`'${dest}' already exists`)
  58. }
  59. }
  60. function copyFile (srcStat, src, dest, opts) {
  61. fs.copyFileSync(src, dest)
  62. if (opts.preserveTimestamps) handleTimestamps(srcStat.mode, src, dest)
  63. return setDestMode(dest, srcStat.mode)
  64. }
  65. function handleTimestamps (srcMode, src, dest) {
  66. // Make sure the file is writable before setting the timestamp
  67. // otherwise open fails with EPERM when invoked with 'r+'
  68. // (through utimes call)
  69. if (fileIsNotWritable(srcMode)) makeFileWritable(dest, srcMode)
  70. return setDestTimestamps(src, dest)
  71. }
  72. function fileIsNotWritable (srcMode) {
  73. return (srcMode & 0o200) === 0
  74. }
  75. function makeFileWritable (dest, srcMode) {
  76. return setDestMode(dest, srcMode | 0o200)
  77. }
  78. function setDestMode (dest, srcMode) {
  79. return fs.chmodSync(dest, srcMode)
  80. }
  81. function setDestTimestamps (src, dest) {
  82. // The initial srcStat.atime cannot be trusted
  83. // because it is modified by the read(2) system call
  84. // (See https://nodejs.org/api/fs.html#fs_stat_time_values)
  85. const updatedSrcStat = fs.statSync(src)
  86. return utimesMillisSync(dest, updatedSrcStat.atime, updatedSrcStat.mtime)
  87. }
  88. function onDir (srcStat, destStat, src, dest, opts) {
  89. if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts)
  90. return copyDir(src, dest, opts)
  91. }
  92. function mkDirAndCopy (srcMode, src, dest, opts) {
  93. fs.mkdirSync(dest)
  94. copyDir(src, dest, opts)
  95. return setDestMode(dest, srcMode)
  96. }
  97. function copyDir (src, dest, opts) {
  98. fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
  99. }
  100. function copyDirItem (item, src, dest, opts) {
  101. const srcItem = path.join(src, item)
  102. const destItem = path.join(dest, item)
  103. const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy', opts)
  104. return startCopy(destStat, srcItem, destItem, opts)
  105. }
  106. function onLink (destStat, src, dest, opts) {
  107. let resolvedSrc = fs.readlinkSync(src)
  108. if (opts.dereference) {
  109. resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
  110. }
  111. if (!destStat) {
  112. return fs.symlinkSync(resolvedSrc, dest)
  113. } else {
  114. let resolvedDest
  115. try {
  116. resolvedDest = fs.readlinkSync(dest)
  117. } catch (err) {
  118. // dest exists and is a regular file or directory,
  119. // Windows may throw UNKNOWN error. If dest already exists,
  120. // fs throws error anyway, so no need to guard against it here.
  121. if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlinkSync(resolvedSrc, dest)
  122. throw err
  123. }
  124. if (opts.dereference) {
  125. resolvedDest = path.resolve(process.cwd(), resolvedDest)
  126. }
  127. if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
  128. throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
  129. }
  130. // prevent copy if src is a subdir of dest since unlinking
  131. // dest in this case would result in removing src contents
  132. // and therefore a broken symlink would be created.
  133. if (fs.statSync(dest).isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
  134. throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)
  135. }
  136. return copyLink(resolvedSrc, dest)
  137. }
  138. }
  139. function copyLink (resolvedSrc, dest) {
  140. fs.unlinkSync(dest)
  141. return fs.symlinkSync(resolvedSrc, dest)
  142. }
  143. module.exports = copySync