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.

133 lines
3.1 KiB

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. item := file.wfs.listDirectoryEntriesCache.Get(fullPath)
  27. if item != nil {
  28. entry := item.Value().(*filer_pb.Entry)
  29. file.Chunks = entry.Chunks
  30. file.attributes = entry.Attributes
  31. glog.V(1).Infof("file attr read cached %v attributes", file.Name)
  32. } else {
  33. err := file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  34. request := &filer_pb.GetEntryAttributesRequest{
  35. Name: file.Name,
  36. ParentDir: file.dir.Path,
  37. }
  38. resp, err := client.GetEntryAttributes(context, request)
  39. if err != nil {
  40. glog.V(0).Infof("file attr read file %v: %v", request, err)
  41. return err
  42. }
  43. file.attributes = resp.Attributes
  44. file.Chunks = resp.Chunks
  45. glog.V(1).Infof("file attr %v %+v: %d", fullPath, file.attributes, filer2.TotalSize(file.Chunks))
  46. return nil
  47. })
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. attr.Mode = os.FileMode(file.attributes.FileMode)
  53. attr.Size = filer2.TotalSize(file.Chunks)
  54. attr.Mtime = time.Unix(file.attributes.Mtime, 0)
  55. attr.Gid = file.attributes.Gid
  56. attr.Uid = file.attributes.Uid
  57. return nil
  58. }
  59. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  60. fullPath := filepath.Join(file.dir.Path, file.Name)
  61. glog.V(3).Infof("%v file open %+v", fullPath, req)
  62. return &FileHandle{
  63. wfs: file.wfs,
  64. dirPath: file.dir.Path,
  65. name: file.Name,
  66. RequestId: req.Header.ID,
  67. NodeId: req.Header.Node,
  68. Uid: req.Uid,
  69. Gid: req.Gid,
  70. attributes: file.attributes,
  71. Chunks: file.Chunks,
  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. }