1
0

filesystem_copy_range_standard.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "io"
  9. )
  10. func init() {
  11. registerCopyRangeImplementation(CopyRangeMethodStandard, copyRangeStandard)
  12. }
  13. func copyRangeStandard(src, dst File, srcOffset, dstOffset, size int64) error {
  14. const bufSize = 4 << 20
  15. buf := make([]byte, bufSize)
  16. // TODO: In go 1.15, we should use file.ReadFrom that uses copy_file_range underneath.
  17. // ReadAt and WriteAt does not modify the position of the file.
  18. for size > 0 {
  19. if size < bufSize {
  20. buf = buf[:size]
  21. }
  22. n, err := src.ReadAt(buf, srcOffset)
  23. if err != nil {
  24. if err == io.EOF {
  25. return io.ErrUnexpectedEOF
  26. }
  27. return err
  28. }
  29. if _, err = dst.WriteAt(buf[:n], dstOffset); err != nil {
  30. return err
  31. }
  32. srcOffset += int64(n)
  33. dstOffset += int64(n)
  34. size -= int64(n)
  35. }
  36. return nil
  37. }