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.

135 lines
3.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "bazil.org/fuse"
  5. "bazil.org/fuse/fs"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "path/filepath"
  9. "os"
  10. "time"
  11. "github.com/chrislusf/seaweedfs/weed/filer2"
  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. RequestId: req.Header.ID,
  69. NodeId: req.Header.Node,
  70. Uid: req.Uid,
  71. Gid: req.Gid,
  72. }, nil
  73. }
  74. func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  75. fullPath := filepath.Join(file.dir.Path, file.Name)
  76. glog.V(3).Infof("%v file setattr %+v", fullPath, req)
  77. if req.Valid.Size() {
  78. glog.V(3).Infof("%v file setattr set size=%v", fullPath, req.Size)
  79. if req.Size == 0 {
  80. // fmt.Printf("truncate %v \n", fullPath)
  81. file.Chunks = nil
  82. }
  83. file.attributes.FileSize = req.Size
  84. }
  85. if req.Valid.Mode() {
  86. file.attributes.FileMode = uint32(req.Mode)
  87. }
  88. if req.Valid.Uid() {
  89. file.attributes.Uid = req.Uid
  90. }
  91. if req.Valid.Gid() {
  92. file.attributes.Gid = req.Gid
  93. }
  94. if req.Valid.Mtime() {
  95. file.attributes.Mtime = req.Mtime.Unix()
  96. }
  97. return nil
  98. }
  99. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  100. // fsync works at OS level
  101. // write the file chunks to the filer
  102. glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
  103. return nil
  104. }