basicfs_copy_range_copyfilerange.go 1.2 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. // +build linux
  7. package fs
  8. import (
  9. "io"
  10. "syscall"
  11. "golang.org/x/sys/unix"
  12. )
  13. func init() {
  14. registerCopyRangeImplementation(CopyRangeMethodCopyFileRange, copyRangeImplementationForBasicFile(copyRangeCopyFileRange))
  15. }
  16. func copyRangeCopyFileRange(src, dst basicFile, srcOffset, dstOffset, size int64) error {
  17. for size > 0 {
  18. // From MAN page:
  19. //
  20. // If off_in is not NULL, then off_in must point to a buffer that
  21. // specifies the starting offset where bytes from fd_in will be read.
  22. // The file offset of fd_in is not changed, but off_in is adjusted
  23. // appropriately.
  24. //
  25. // Also, even if explicitly not stated, the same is true for dstOffset
  26. n, err := unix.CopyFileRange(int(src.Fd()), &srcOffset, int(dst.Fd()), &dstOffset, int(size), 0)
  27. if n == 0 && err == nil {
  28. return io.ErrUnexpectedEOF
  29. }
  30. if err != nil && err != syscall.EAGAIN {
  31. return err
  32. }
  33. // Handle case where err == EAGAIN and n == -1 (it's not clear if that can happen)
  34. if n > 0 {
  35. size -= int64(n)
  36. }
  37. }
  38. return nil
  39. }