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.

132 lines
3.0 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. }
  24. func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
  25. fullPath := filepath.Join(file.dir.Path, file.Name)
  26. if file.attributes == nil {
  27. item := file.wfs.listDirectoryEntriesCache.Get(fullPath)
  28. if item != nil {
  29. entry := item.Value().(*filer_pb.Entry)
  30. file.Chunks = entry.Chunks
  31. file.attributes = entry.Attributes
  32. glog.V(1).Infof("file attr read cached %v attributes", file.Name)
  33. } else {
  34. err := file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  35. request := &filer_pb.GetEntryAttributesRequest{
  36. Name: file.Name,
  37. ParentDir: file.dir.Path,
  38. }
  39. resp, err := client.GetEntryAttributes(context, request)
  40. if err != nil {
  41. glog.V(0).Infof("file attr read file %v: %v", request, err)
  42. return err
  43. }
  44. file.attributes = resp.Attributes
  45. file.Chunks = resp.Chunks
  46. glog.V(1).Infof("file attr %v %+v: %d", fullPath, file.attributes, filer2.TotalSize(file.Chunks))
  47. return nil
  48. })
  49. if err != nil {
  50. return err
  51. }
  52. }
  53. }
  54. attr.Mode = os.FileMode(file.attributes.FileMode)
  55. attr.Size = filer2.TotalSize(file.Chunks)
  56. attr.Mtime = time.Unix(file.attributes.Mtime, 0)
  57. attr.Gid = file.attributes.Gid
  58. attr.Uid = file.attributes.Uid
  59. return nil
  60. }
  61. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  62. fullPath := filepath.Join(file.dir.Path, file.Name)
  63. glog.V(3).Infof("%v file open %+v", fullPath, req)
  64. return &FileHandle{
  65. f: file,
  66. RequestId: req.Header.ID,
  67. NodeId: req.Header.Node,
  68. Uid: req.Uid,
  69. Gid: req.Gid,
  70. }, nil
  71. }
  72. func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  73. fullPath := filepath.Join(file.dir.Path, file.Name)
  74. glog.V(3).Infof("%v file setattr %+v", fullPath, req)
  75. if req.Valid.Size() {
  76. glog.V(3).Infof("%v file setattr set size=%v", fullPath, req.Size)
  77. if req.Size == 0 {
  78. // fmt.Printf("truncate %v \n", fullPath)
  79. file.Chunks = nil
  80. }
  81. file.attributes.FileSize = req.Size
  82. }
  83. if req.Valid.Mode() {
  84. file.attributes.FileMode = uint32(req.Mode)
  85. }
  86. if req.Valid.Uid() {
  87. file.attributes.Uid = req.Uid
  88. }
  89. if req.Valid.Gid() {
  90. file.attributes.Gid = req.Gid
  91. }
  92. if req.Valid.Mtime() {
  93. file.attributes.Mtime = req.Mtime.Unix()
  94. }
  95. return nil
  96. }
  97. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  98. // fsync works at OS level
  99. // write the file chunks to the filer
  100. glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
  101. return nil
  102. }