filesystem_copy_range_standard.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. "errors"
  9. "io"
  10. )
  11. func init() {
  12. registerCopyRangeImplementation(CopyRangeMethodStandard, copyRangeStandard)
  13. }
  14. func copyRangeStandard(src, dst File, srcOffset, dstOffset, size int64) error {
  15. const bufSize = 4 << 20
  16. buf := make([]byte, bufSize)
  17. // TODO: In go 1.15, we should use file.ReadFrom that uses copy_file_range underneath.
  18. // ReadAt and WriteAt does not modify the position of the file.
  19. for size > 0 {
  20. if size < bufSize {
  21. buf = buf[:size]
  22. }
  23. n, err := src.ReadAt(buf, srcOffset)
  24. if err != nil {
  25. if errors.Is(err, io.EOF) {
  26. return io.ErrUnexpectedEOF
  27. }
  28. return err
  29. }
  30. if _, err = dst.WriteAt(buf[:n], dstOffset); err != nil {
  31. return err
  32. }
  33. srcOffset += int64(n)
  34. dstOffset += int64(n)
  35. size -= int64(n)
  36. }
  37. return nil
  38. }