basicfs_copy_range_sendfile.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 solaris
  7. package fs
  8. import (
  9. "io"
  10. "syscall"
  11. )
  12. func init() {
  13. registerCopyRangeImplementation(CopyRangeMethodSendFile, copyRangeImplementationForBasicFile(copyRangeSendFile))
  14. }
  15. func copyRangeSendFile(src, dst basicFile, srcOffset, dstOffset, size int64) error {
  16. // Check that the destination file has sufficient space
  17. if fi, err := dst.Stat(); err != nil {
  18. return err
  19. } else if fi.Size() < dstOffset+size {
  20. if err := dst.Truncate(dstOffset + size); err != nil {
  21. return err
  22. }
  23. }
  24. // Record old dst offset.
  25. oldDstOffset, err := dst.Seek(0, io.SeekCurrent)
  26. if err != nil {
  27. return err
  28. }
  29. defer func() { _, _ = dst.Seek(oldDstOffset, io.SeekStart) }()
  30. // Seek to the offset we expect to write
  31. if oldDstOffset != dstOffset {
  32. if n, err := dst.Seek(dstOffset, io.SeekStart); err != nil {
  33. return err
  34. } else if n != dstOffset {
  35. return io.ErrUnexpectedEOF
  36. }
  37. }
  38. for size > 0 {
  39. // From the MAN page:
  40. //
  41. // If offset is not NULL, then it points to a variable holding the file offset from which sendfile() will start
  42. // reading data from in_fd. When sendfile() returns, this variable will be set to the offset of the byte
  43. // following the last byte that was read. If offset is not NULL, then sendfile() does not modify the current
  44. // file offset of in_fd; otherwise the current file offset is adjusted to reflect the number of bytes read from
  45. // in_fd.
  46. n, err := withFileDescriptors(dst, src, func(dstFd, srcFd uintptr) (int, error) {
  47. return syscall.Sendfile(int(dstFd), int(srcFd), &srcOffset, int(size))
  48. })
  49. if n == 0 && err == nil {
  50. err = io.ErrUnexpectedEOF
  51. }
  52. if err != nil && err != syscall.EAGAIN {
  53. return err
  54. }
  55. // Handle case where err == EAGAIN and n == -1 (it's not clear if that can happen)
  56. if n > 0 {
  57. size -= int64(n)
  58. }
  59. }
  60. _, err = dst.Seek(oldDstOffset, io.SeekStart)
  61. return err
  62. }