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.

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