basicfs_copy_range_copyfilerange.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 := withFileDescriptors(src, dst, func(srcFd, dstFd uintptr) (int, error) {
  27. return unix.CopyFileRange(int(srcFd), &srcOffset, int(dstFd), &dstOffset, int(size), 0)
  28. })
  29. if n == 0 && err == nil {
  30. return io.ErrUnexpectedEOF
  31. }
  32. if err != nil && err != syscall.EAGAIN {
  33. return err
  34. }
  35. // Handle case where err == EAGAIN and n == -1 (it's not clear if that can happen)
  36. if n > 0 {
  37. size -= int64(n)
  38. }
  39. }
  40. return nil
  41. }