Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

copy-sync.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use strict'
  2. const fs = require('graceful-fs')
  3. const path = require('path')
  4. const mkdirpSync = require('../mkdirs').mkdirsSync
  5. const utimesSync = require('../util/utimes.js').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. console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n
  17. see https://github.com/jprichardson/node-fs-extra/issues/269`)
  18. }
  19. const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy')
  20. stat.checkParentPathsSync(src, srcStat, dest, 'copy')
  21. return handleFilterAndCopy(destStat, src, dest, opts)
  22. }
  23. function handleFilterAndCopy (destStat, src, dest, opts) {
  24. if (opts.filter && !opts.filter(src, dest)) return
  25. const destParent = path.dirname(dest)
  26. if (!fs.existsSync(destParent)) mkdirpSync(destParent)
  27. return startCopy(destStat, src, dest, opts)
  28. }
  29. function startCopy (destStat, src, dest, opts) {
  30. if (opts.filter && !opts.filter(src, dest)) return
  31. return getStats(destStat, src, dest, opts)
  32. }
  33. function getStats (destStat, src, dest, opts) {
  34. const statSync = opts.dereference ? fs.statSync : fs.lstatSync
  35. const srcStat = statSync(src)
  36. if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts)
  37. else if (srcStat.isFile() ||
  38. srcStat.isCharacterDevice() ||
  39. srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts)
  40. else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts)
  41. }
  42. function onFile (srcStat, destStat, src, dest, opts) {
  43. if (!destStat) return copyFile(srcStat, src, dest, opts)
  44. return mayCopyFile(srcStat, src, dest, opts)
  45. }
  46. function mayCopyFile (srcStat, src, dest, opts) {
  47. if (opts.overwrite) {
  48. fs.unlinkSync(dest)
  49. return copyFile(srcStat, src, dest, opts)
  50. } else if (opts.errorOnExist) {
  51. throw new Error(`'${dest}' already exists`)
  52. }
  53. }
  54. function copyFile (srcStat, src, dest, opts) {
  55. if (typeof fs.copyFileSync === 'function') {
  56. fs.copyFileSync(src, dest)
  57. fs.chmodSync(dest, srcStat.mode)
  58. if (opts.preserveTimestamps) {
  59. return utimesSync(dest, srcStat.atime, srcStat.mtime)
  60. }
  61. return
  62. }
  63. return copyFileFallback(srcStat, src, dest, opts)
  64. }
  65. function copyFileFallback (srcStat, src, dest, opts) {
  66. const BUF_LENGTH = 64 * 1024
  67. const _buff = require('../util/buffer')(BUF_LENGTH)
  68. const fdr = fs.openSync(src, 'r')
  69. const fdw = fs.openSync(dest, 'w', srcStat.mode)
  70. let pos = 0
  71. while (pos < srcStat.size) {
  72. const bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos)
  73. fs.writeSync(fdw, _buff, 0, bytesRead)
  74. pos += bytesRead
  75. }
  76. if (opts.preserveTimestamps) fs.futimesSync(fdw, srcStat.atime, srcStat.mtime)
  77. fs.closeSync(fdr)
  78. fs.closeSync(fdw)
  79. }
  80. function onDir (srcStat, destStat, src, dest, opts) {
  81. if (!destStat) return mkDirAndCopy(srcStat, src, dest, opts)
  82. if (destStat && !destStat.isDirectory()) {
  83. throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
  84. }
  85. return copyDir(src, dest, opts)
  86. }
  87. function mkDirAndCopy (srcStat, src, dest, opts) {
  88. fs.mkdirSync(dest)
  89. copyDir(src, dest, opts)
  90. return fs.chmodSync(dest, srcStat.mode)
  91. }
  92. function copyDir (src, dest, opts) {
  93. fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
  94. }
  95. function copyDirItem (item, src, dest, opts) {
  96. const srcItem = path.join(src, item)
  97. const destItem = path.join(dest, item)
  98. const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy')
  99. return startCopy(destStat, srcItem, destItem, opts)
  100. }
  101. function onLink (destStat, src, dest, opts) {
  102. let resolvedSrc = fs.readlinkSync(src)
  103. if (opts.dereference) {
  104. resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
  105. }
  106. if (!destStat) {
  107. return fs.symlinkSync(resolvedSrc, dest)
  108. } else {
  109. let resolvedDest
  110. try {
  111. resolvedDest = fs.readlinkSync(dest)
  112. } catch (err) {
  113. // dest exists and is a regular file or directory,
  114. // Windows may throw UNKNOWN error. If dest already exists,
  115. // fs throws error anyway, so no need to guard against it here.
  116. if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlinkSync(resolvedSrc, dest)
  117. throw err
  118. }
  119. if (opts.dereference) {
  120. resolvedDest = path.resolve(process.cwd(), resolvedDest)
  121. }
  122. if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
  123. throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
  124. }
  125. // prevent copy if src is a subdir of dest since unlinking
  126. // dest in this case would result in removing src contents
  127. // and therefore a broken symlink would be created.
  128. if (fs.statSync(dest).isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
  129. throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)
  130. }
  131. return copyLink(resolvedSrc, dest)
  132. }
  133. }
  134. function copyLink (resolvedSrc, dest) {
  135. fs.unlinkSync(dest)
  136. return fs.symlinkSync(resolvedSrc, dest)
  137. }
  138. module.exports = copySync