You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.8 KiB

  1. package mount
  2. import (
  3. "net/http"
  4. "github.com/hanwen/go-fuse/v2/fuse"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. )
  7. // CopyFileRange copies data from one file to another from and to specified offsets.
  8. //
  9. // See https://man7.org/linux/man-pages/man2/copy_file_range.2.html
  10. // See https://github.com/libfuse/libfuse/commit/fe4f9428fc403fa8b99051f52d84ea5bd13f3855
  11. /**
  12. * Copy a range of data from one file to another
  13. *
  14. * Niels de Vos: libfuse: add copy_file_range() support
  15. *
  16. * Performs an optimized copy between two file descriptors without the
  17. * additional cost of transferring data through the FUSE kernel module
  18. * to user space (glibc) and then back into the FUSE filesystem again.
  19. *
  20. * In case this method is not implemented, applications are expected to
  21. * fall back to a regular file copy. (Some glibc versions did this
  22. * emulation automatically, but the emulation has been removed from all
  23. * glibc release branches.)
  24. */
  25. func (wfs *WFS) CopyFileRange(cancel <-chan struct{}, in *fuse.CopyFileRangeIn) (written uint32, code fuse.Status) {
  26. // flags must equal 0 for this syscall as of now
  27. if in.Flags != 0 {
  28. return 0, fuse.EINVAL
  29. }
  30. // files must exist
  31. fhOut := wfs.GetHandle(FileHandleId(in.FhOut))
  32. if fhOut == nil {
  33. return 0, fuse.EBADF
  34. }
  35. fhIn := wfs.GetHandle(FileHandleId(in.FhIn))
  36. if fhIn == nil {
  37. return 0, fuse.EBADF
  38. }
  39. // lock source and target file handles
  40. fhOut.Lock()
  41. defer fhOut.Unlock()
  42. fhOut.entryLock.Lock()
  43. defer fhOut.entryLock.Unlock()
  44. if fhOut.entry == nil {
  45. return 0, fuse.ENOENT
  46. }
  47. if fhIn.fh != fhOut.fh {
  48. fhIn.Lock()
  49. defer fhIn.Unlock()
  50. fhIn.entryLock.Lock()
  51. defer fhIn.entryLock.Unlock()
  52. }
  53. // directories are not supported
  54. if fhIn.entry.IsDirectory || fhOut.entry.IsDirectory {
  55. return 0, fuse.EISDIR
  56. }
  57. glog.V(4).Infof(
  58. "CopyFileRange %s fhIn %d -> %s fhOut %d, [%d,%d) -> [%d,%d)",
  59. fhIn.FullPath(), fhIn.fh,
  60. fhOut.FullPath(), fhOut.fh,
  61. in.OffIn, in.OffIn+in.Len,
  62. in.OffOut, in.OffOut+in.Len,
  63. )
  64. data := make([]byte, in.Len)
  65. totalRead, err := readDataByFileHandle(data, fhIn, int64(in.OffIn))
  66. if err != nil {
  67. glog.Warningf("file handle read %s %d: %v", fhIn.FullPath(), totalRead, err)
  68. return 0, fuse.EIO
  69. }
  70. data = data[:totalRead]
  71. if totalRead == 0 {
  72. return 0, fuse.OK
  73. }
  74. // put data at the specified offset in target file
  75. fhOut.dirtyPages.writerPattern.MonitorWriteAt(int64(in.OffOut), int(in.Len))
  76. fhOut.entry.Content = nil
  77. fhOut.dirtyPages.AddPage(int64(in.OffOut), data, fhOut.dirtyPages.writerPattern.IsSequentialMode())
  78. fhOut.entry.Attributes.FileSize = uint64(max(int64(in.OffOut)+totalRead, int64(fhOut.entry.Attributes.FileSize)))
  79. fhOut.dirtyMetadata = true
  80. written = uint32(totalRead)
  81. // detect mime type
  82. if written > 0 && in.OffOut <= 512 {
  83. fhOut.contentType = http.DetectContentType(data)
  84. }
  85. return written, fuse.OK
  86. }