fileCopy.go 789 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package hybridBackup
  2. import (
  3. "errors"
  4. "io"
  5. "os"
  6. )
  7. func BufferedLargeFileCopy(src string, dst string, BUFFERSIZE int64) error {
  8. sourceFileStat, err := os.Stat(src)
  9. if err != nil {
  10. return err
  11. }
  12. if !sourceFileStat.Mode().IsRegular() {
  13. return errors.New("Invalid file source")
  14. }
  15. source, err := os.Open(src)
  16. if err != nil {
  17. return err
  18. }
  19. destination, err := os.Create(dst)
  20. if err != nil {
  21. return err
  22. }
  23. buf := make([]byte, BUFFERSIZE)
  24. for {
  25. n, err := source.Read(buf)
  26. if err != nil && err != io.EOF {
  27. source.Close()
  28. destination.Close()
  29. return err
  30. }
  31. if n == 0 {
  32. source.Close()
  33. destination.Close()
  34. break
  35. }
  36. if _, err := destination.Write(buf[:n]); err != nil {
  37. source.Close()
  38. destination.Close()
  39. return err
  40. }
  41. }
  42. return nil
  43. }