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.

175 lines
4.2 KiB

7 years ago
7 years ago
7 years ago
6 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/filer2"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/fuse"
  8. "github.com/seaweedfs/fuse/fs"
  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. entryViewCache []filer2.VisibleInterval
  24. isOpen bool
  25. }
  26. func (file *File) fullpath() string {
  27. return filepath.Join(file.dir.Path, file.Name)
  28. }
  29. func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error {
  30. if err := file.maybeLoadAttributes(ctx); err != nil {
  31. return err
  32. }
  33. attr.Mode = os.FileMode(file.entry.Attributes.FileMode)
  34. attr.Size = filer2.TotalSize(file.entry.Chunks)
  35. attr.Mtime = time.Unix(file.entry.Attributes.Mtime, 0)
  36. attr.Gid = file.entry.Attributes.Gid
  37. attr.Uid = file.entry.Attributes.Uid
  38. attr.Blocks = attr.Size/blockSize + 1
  39. attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit)
  40. return nil
  41. }
  42. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  43. glog.V(3).Infof("%v file open %+v", file.fullpath(), req)
  44. file.isOpen = true
  45. handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid)
  46. resp.Handle = fuse.HandleID(handle.handle)
  47. glog.V(3).Infof("%v file open handle id = %d", file.fullpath(), handle.handle)
  48. return handle, nil
  49. }
  50. func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  51. if err := file.maybeLoadAttributes(ctx); err != nil {
  52. return err
  53. }
  54. if file.isOpen {
  55. return nil
  56. }
  57. glog.V(3).Infof("%v file setattr %+v, old:%+v", file.fullpath(), req, file.entry.Attributes)
  58. if req.Valid.Size() {
  59. glog.V(3).Infof("%v file setattr set size=%v", file.fullpath(), req.Size)
  60. if req.Size == 0 {
  61. // fmt.Printf("truncate %v \n", fullPath)
  62. file.entry.Chunks = nil
  63. file.entryViewCache = nil
  64. }
  65. file.entry.Attributes.FileSize = req.Size
  66. }
  67. if req.Valid.Mode() {
  68. file.entry.Attributes.FileMode = uint32(req.Mode)
  69. }
  70. if req.Valid.Uid() {
  71. file.entry.Attributes.Uid = req.Uid
  72. }
  73. if req.Valid.Gid() {
  74. file.entry.Attributes.Gid = req.Gid
  75. }
  76. if req.Valid.Crtime() {
  77. file.entry.Attributes.Crtime = req.Crtime.Unix()
  78. }
  79. if req.Valid.Mtime() {
  80. file.entry.Attributes.Mtime = req.Mtime.Unix()
  81. }
  82. return file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  83. request := &filer_pb.UpdateEntryRequest{
  84. Directory: file.dir.Path,
  85. Entry: file.entry,
  86. }
  87. glog.V(1).Infof("set attr file entry: %v", request)
  88. _, err := client.UpdateEntry(ctx, request)
  89. if err != nil {
  90. glog.V(0).Infof("UpdateEntry file %s/%s: %v", file.dir.Path, file.Name, err)
  91. return fuse.EIO
  92. }
  93. return nil
  94. })
  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. }
  102. func (file *File) maybeLoadAttributes(ctx context.Context) error {
  103. if file.entry == nil || !file.isOpen {
  104. item := file.wfs.listDirectoryEntriesCache.Get(file.fullpath())
  105. if item != nil && !item.Expired() {
  106. entry := item.Value().(*filer_pb.Entry)
  107. file.entry = entry
  108. file.entryViewCache = nil
  109. // glog.V(1).Infof("file attr read cached %v attributes", file.Name)
  110. } else {
  111. err := file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  112. request := &filer_pb.LookupDirectoryEntryRequest{
  113. Name: file.Name,
  114. Directory: file.dir.Path,
  115. }
  116. resp, err := client.LookupDirectoryEntry(ctx, request)
  117. if err != nil {
  118. glog.V(3).Infof("file attr read file %v: %v", request, err)
  119. return fuse.ENOENT
  120. }
  121. file.entry = resp.Entry
  122. file.entryViewCache = nil
  123. glog.V(3).Infof("file attr %v %+v: %d", file.fullpath(), file.entry.Attributes, filer2.TotalSize(file.entry.Chunks))
  124. // file.wfs.listDirectoryEntriesCache.Set(file.fullpath(), file.entry, file.wfs.option.EntryCacheTtl)
  125. return nil
  126. })
  127. if err != nil {
  128. return err
  129. }
  130. }
  131. }
  132. return nil
  133. }