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.1 KiB

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