copy-sync.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if (opts.filter && !opts.filter(src, dest)) return
  25. const destParent = path.dirname(dest)
  26. if (!fs.existsSync(destParent)) mkdirsSync(destParent)
  27. return getStats(destStat, src, dest, opts)
  28. }
  29. function getStats (destStat, src, dest, opts) {
  30. const statSync = opts.dereference ? fs.statSync : fs.lstatSync
  31. const srcStat = statSync(src)
  32. if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts)
  33. else if (srcStat.isFile() ||
  34. srcStat.isCharacterDevice() ||
  35. srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts)
  36. else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts)
  37. else if (srcStat.isSocket()) throw new Error(`Cannot copy a socket file: ${src}`)
  38. else if (srcStat.isFIFO()) throw new Error(`Cannot copy a FIFO pipe: ${src}`)
  39. throw new Error(`Unknown file: ${src}`)
  40. }
  41. function onFile (srcStat, destStat, src, dest, opts) {
  42. if (!destStat) return copyFile(srcStat, src, dest, opts)
  43. return mayCopyFile(srcStat, src, dest, opts)
  44. }
  45. function mayCopyFile (srcStat, src, dest, opts) {
  46. if (opts.overwrite) {
  47. fs.unlinkSync(dest)
  48. return copyFile(srcStat, src, dest, opts)
  49. } else if (opts.errorOnExist) {
  50. throw new Error(`'${dest}' already exists`)
  51. }
  52. }
  53. function copyFile (srcStat, src, dest, opts) {
  54. fs.copyFileSync(src, dest)
  55. if (opts.preserveTimestamps) handleTimestamps(srcStat.mode, src, dest)
  56. return setDestMode(dest, srcStat.mode)
  57. }
  58. function handleTimestamps (srcMode, src, dest) {
  59. // Make sure the file is writable before setting the timestamp
  60. // otherwise open fails with EPERM when invoked with 'r+'
  61. // (through utimes call)
  62. if (fileIsNotWritable(srcMode)) makeFileWritable(dest, srcMode)
  63. return setDestTimestamps(src, dest)
  64. }
  65. function fileIsNotWritable (srcMode) {
  66. return (srcMode & 0o200) === 0
  67. }
  68. function makeFileWritable (dest, srcMode) {
  69. return setDestMode(dest, srcMode | 0o200)
  70. }
  71. function setDestMode (dest, srcMode) {
  72. return fs.chmodSync(dest, srcMode)
  73. }
  74. function setDestTimestamps (src, dest) {
  75. // The initial srcStat.atime cannot be trusted
  76. // because it is modified by the read(2) system call
  77. // (See https://nodejs.org/api/fs.html#fs_stat_time_values)
  78. const updatedSrcStat = fs.statSync(src)
  79. return utimesMillisSync(dest, updatedSrcStat.atime, updatedSrcStat.mtime)
  80. }
  81. function onDir (srcStat, destStat, src, dest, opts) {
  82. if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts)
  83. return copyDir(src, dest, opts)
  84. }
  85. function mkDirAndCopy (srcMode, src, dest, opts) {
  86. fs.mkdirSync(dest)
  87. copyDir(src, dest, opts)
  88. return setDestMode(dest, srcMode)
  89. }
  90. function copyDir (src, dest, opts) {
  91. fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
  92. }
  93. function copyDirItem (item, src, dest, opts) {
  94. const srcItem = path.join(src, item)
  95. const destItem = path.join(dest, item)
  96. if (opts.filter && !opts.filter(srcItem, destItem)) return
  97. const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy', opts)
  98. return getStats(destStat, srcItem, destItem, opts)
  99. }
  100. function onLink (destStat, src, dest, opts) {
  101. let resolvedSrc = fs.readlinkSync(src)
  102. if (opts.dereference) {
  103. resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
  104. }
  105. if (!destStat) {
  106. return fs.symlinkSync(resolvedSrc, dest)
  107. } else {
  108. let resolvedDest
  109. try {
  110. resolvedDest = fs.readlinkSync(dest)
  111. } catch (err) {
  112. // dest exists and is a regular file or directory,
  113. // Windows may throw UNKNOWN error. If dest already exists,
  114. // fs throws error anyway, so no need to guard against it here.
  115. if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlinkSync(resolvedSrc, dest)
  116. throw err
  117. }
  118. if (opts.dereference) {
  119. resolvedDest = path.resolve(process.cwd(), resolvedDest)
  120. }
  121. if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
  122. throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
  123. }
  124. // prevent copy if src is a subdir of dest since unlinking
  125. // dest in this case would result in removing src contents
  126. // and therefore a broken symlink would be created.
  127. if (stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
  128. throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)
  129. }
  130. return copyLink(resolvedSrc, dest)
  131. }
  132. }
  133. function copyLink (resolvedSrc, dest) {
  134. fs.unlinkSync(dest)
  135. return fs.symlinkSync(resolvedSrc, dest)
  136. }
  137. module.exports = copySync