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.

136 lines
3.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package filesys
  2. import (
  3. "bazil.org/fuse"
  4. "bazil.org/fuse/fs"
  5. "context"
  6. "github.com/chrislusf/seaweedfs/weed/filer2"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "os"
  10. "path/filepath"
  11. "time"
  12. )
  13. var _ = fs.Node(&File{})
  14. var _ = fs.NodeOpener(&File{})
  15. var _ = fs.NodeFsyncer(&File{})
  16. var _ = fs.NodeSetattrer(&File{})
  17. type File struct {
  18. Chunks []*filer_pb.FileChunk
  19. Name string
  20. dir *Dir
  21. wfs *WFS
  22. attributes *filer_pb.FuseAttributes
  23. isOpen bool
  24. }
  25. func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
  26. fullPath := filepath.Join(file.dir.Path, file.Name)
  27. if file.attributes == nil || !file.isOpen {
  28. item := file.wfs.listDirectoryEntriesCache.Get(fullPath)
  29. if item != nil {
  30. entry := item.Value().(*filer_pb.Entry)
  31. file.Chunks = entry.Chunks
  32. file.attributes = entry.Attributes
  33. glog.V(1).Infof("file attr read cached %v attributes", file.Name)
  34. } else {
  35. err := file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  36. request := &filer_pb.GetEntryAttributesRequest{
  37. Name: file.Name,
  38. ParentDir: file.dir.Path,
  39. }
  40. resp, err := client.GetEntryAttributes(context, request)
  41. if err != nil {
  42. glog.V(0).Infof("file attr read file %v: %v", request, err)
  43. return err
  44. }
  45. file.attributes = resp.Attributes
  46. file.Chunks = resp.Chunks
  47. glog.V(1).Infof("file attr %v %+v: %d", fullPath, file.attributes, filer2.TotalSize(file.Chunks))
  48. return nil
  49. })
  50. if err != nil {
  51. return err
  52. }
  53. }
  54. }
  55. attr.Mode = os.FileMode(file.attributes.FileMode)
  56. attr.Size = filer2.TotalSize(file.Chunks)
  57. attr.Mtime = time.Unix(file.attributes.Mtime, 0)
  58. attr.Gid = file.attributes.Gid
  59. attr.Uid = file.attributes.Uid
  60. return nil
  61. }
  62. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  63. fullPath := filepath.Join(file.dir.Path, file.Name)
  64. glog.V(3).Infof("%v file open %+v", fullPath, req)
  65. file.isOpen = true
  66. return &FileHandle{
  67. f: file,
  68. dirtyPages: newDirtyPages(file),
  69. RequestId: req.Header.ID,
  70. NodeId: req.Header.Node,
  71. Uid: req.Uid,
  72. Gid: req.Gid,
  73. }, nil
  74. }
  75. func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  76. fullPath := filepath.Join(file.dir.Path, file.Name)
  77. glog.V(3).Infof("%v file setattr %+v", fullPath, req)
  78. if req.Valid.Size() {
  79. glog.V(3).Infof("%v file setattr set size=%v", fullPath, req.Size)
  80. if req.Size == 0 {
  81. // fmt.Printf("truncate %v \n", fullPath)
  82. file.Chunks = nil
  83. }
  84. file.attributes.FileSize = req.Size
  85. }
  86. if req.Valid.Mode() {
  87. file.attributes.FileMode = uint32(req.Mode)
  88. }
  89. if req.Valid.Uid() {
  90. file.attributes.Uid = req.Uid
  91. }
  92. if req.Valid.Gid() {
  93. file.attributes.Gid = req.Gid
  94. }
  95. if req.Valid.Mtime() {
  96. file.attributes.Mtime = req.Mtime.Unix()
  97. }
  98. return nil
  99. }
  100. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  101. // fsync works at OS level
  102. // write the file chunks to the filer
  103. glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
  104. return nil
  105. }