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.

134 lines
3.0 KiB

7 years ago
7 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "bazil.org/fuse"
  6. "bazil.org/fuse/fs"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "path/filepath"
  10. "os"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  13. )
  14. var _ = fs.Node(&File{})
  15. var _ = fs.NodeOpener(&File{})
  16. var _ = fs.NodeFsyncer(&File{})
  17. var _ = fs.NodeSetattrer(&File{})
  18. type File struct {
  19. Chunks []*filer_pb.FileChunk
  20. Name string
  21. dir *Dir
  22. wfs *WFS
  23. attributes *filer_pb.FuseAttributes
  24. }
  25. func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
  26. fullPath := filepath.Join(file.dir.Path, file.Name)
  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. attr.Mode = os.FileMode(file.attributes.FileMode)
  54. attr.Size = filer2.TotalSize(file.Chunks)
  55. attr.Mtime = time.Unix(file.attributes.Mtime, 0)
  56. attr.Gid = file.attributes.Gid
  57. attr.Uid = file.attributes.Uid
  58. return nil
  59. }
  60. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  61. fullPath := filepath.Join(file.dir.Path, file.Name)
  62. glog.V(3).Infof("%v file open %+v", fullPath, req)
  63. return &FileHandle{
  64. wfs: file.wfs,
  65. dirPath: file.dir.Path,
  66. name: file.Name,
  67. RequestId: req.Header.ID,
  68. NodeId: req.Header.Node,
  69. Uid: req.Uid,
  70. Gid: req.Gid,
  71. attributes: file.attributes,
  72. Chunks: file.Chunks,
  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. 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. }