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.

161 lines
3.7 KiB

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