basicfs_copy_range_copyfilerange.go 1.3 KB

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