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.

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