weedfs_file_read.go 1.8 KB

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