basicfs_copy_range_sendfile.go 2.1 KB

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