a zip code crypto-currency system good for red ONLY

copy-file-sync.js 982B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict'
  2. const fs = require('graceful-fs')
  3. const BUF_LENGTH = 64 * 1024
  4. const _buff = require('../util/buffer')(BUF_LENGTH)
  5. function copyFileSync (srcFile, destFile, options) {
  6. const overwrite = options.overwrite
  7. const errorOnExist = options.errorOnExist
  8. const preserveTimestamps = options.preserveTimestamps
  9. if (fs.existsSync(destFile)) {
  10. if (overwrite) {
  11. fs.unlinkSync(destFile)
  12. } else if (errorOnExist) {
  13. throw new Error(`${destFile} already exists`)
  14. } else return
  15. }
  16. const fdr = fs.openSync(srcFile, 'r')
  17. const stat = fs.fstatSync(fdr)
  18. const fdw = fs.openSync(destFile, 'w', stat.mode)
  19. let bytesRead = 1
  20. let pos = 0
  21. while (bytesRead > 0) {
  22. bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos)
  23. fs.writeSync(fdw, _buff, 0, bytesRead)
  24. pos += bytesRead
  25. }
  26. if (preserveTimestamps) {
  27. fs.futimesSync(fdw, stat.atime, stat.mtime)
  28. }
  29. fs.closeSync(fdr)
  30. fs.closeSync(fdw)
  31. }
  32. module.exports = copyFileSync