weedfs_file_read.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package mount
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/hanwen/go-fuse/v2/fuse"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. )
  9. /**
  10. * Read data
  11. *
  12. * Read should send exactly the number of bytes requested except
  13. * on EOF or error, otherwise the rest of the data will be
  14. * substituted with zeroes. An exception to this is when the file
  15. * has been opened in 'direct_io' mode, in which case the return
  16. * value of the read system call will reflect the return value of
  17. * this operation.
  18. *
  19. * fi->fh will contain the value set by the open method, or will
  20. * be undefined if the open method didn't set any value.
  21. *
  22. * Valid replies:
  23. * fuse_reply_buf
  24. * fuse_reply_iov
  25. * fuse_reply_data
  26. * fuse_reply_err
  27. *
  28. * @param req request handle
  29. * @param ino the inode number
  30. * @param size number of bytes to read
  31. * @param off offset to read from
  32. * @param fi file information
  33. */
  34. func (wfs *WFS) Read(cancel <-chan struct{}, in *fuse.ReadIn, buff []byte) (fuse.ReadResult, fuse.Status) {
  35. fh := wfs.GetHandle(FileHandleId(in.Fh))
  36. if fh == nil {
  37. return nil, fuse.ENOENT
  38. }
  39. fh.Lock()
  40. defer fh.Unlock()
  41. offset := int64(in.Offset)
  42. totalRead, err := readDataByFileHandle(buff, fh, offset)
  43. if err != nil {
  44. glog.Warningf("file handle read %s %d: %v", fh.FullPath(), totalRead, err)
  45. return nil, fuse.EIO
  46. }
  47. if IsDebugFileReadWrite {
  48. // print(".")
  49. mirrorData := make([]byte, totalRead)
  50. fh.mirrorFile.ReadAt(mirrorData, offset)
  51. if bytes.Compare(mirrorData, buff[:totalRead]) != 0 {
  52. againBuff := make([]byte, len(buff))
  53. againRead, _ := readDataByFileHandle(buff, fh, offset)
  54. againCorrect := bytes.Compare(mirrorData, againBuff[:againRead]) == 0
  55. againSame := bytes.Compare(buff[:totalRead], againBuff[:againRead]) == 0
  56. fmt.Printf("\ncompare %v [%d,%d) size:%d againSame:%v againCorrect:%v\n", fh.mirrorFile.Name(), offset, offset+totalRead, totalRead, againSame, againCorrect)
  57. //fmt.Printf("read mirrow data: %v\n", mirrorData)
  58. //fmt.Printf("read actual data: %v\n", buff[:totalRead])
  59. }
  60. }
  61. return fuse.ReadResultData(buff[:totalRead]), fuse.OK
  62. }
  63. func readDataByFileHandle(buff []byte, fhIn *FileHandle, offset int64) (int64, error) {
  64. // read data from source file
  65. size := len(buff)
  66. fhIn.lockForRead(offset, size)
  67. defer fhIn.unlockForRead(offset, size)
  68. n, tsNs, err := fhIn.readFromChunks(buff, offset)
  69. if err == nil || err == io.EOF {
  70. maxStop := fhIn.readFromDirtyPages(buff, offset, tsNs)
  71. n = max(maxStop-offset, n)
  72. }
  73. if err == io.EOF {
  74. err = nil
  75. }
  76. return n, err
  77. }